Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

howmany

#!/usr/bin/perl
#==========================================================================================
# howmany -- a tool for determining how many different types of files are in a folder
#------------------------------------------------------------------------------------------
# Author: Elliot Winkler <elliot.winkler@gmail.com>
# Created: 11 Mar 2008
#==========================================================================================

my $dir = $ARGV[0] || ".";
my $cmd = "find $dir";
my @listing = sort grep { $_ } split /\n/, `$cmd`;

my %exts;
for (@listing) {
  my($ext) = /\.([a-z]+)$/;
  next unless $ext;
  $exts{lc $ext}++;
}

for (sort keys %exts) {
  print uc($_).": ".$exts{$_}."\n";
}

Example:

$ cd ~/docs
$ howmany
CSV: 3
DOC: 1
KEY: 1
PUB: 1
SQL: 1
TEXT: 1
TXT: 9
XCF: 2
XLS: 1
ZIP: 1
$ howmany ~/docs
CSV: 3
DOC: 1
KEY: 1
PUB: 1
SQL: 1
TEXT: 1
TXT: 9
XCF: 2
XLS: 1
ZIP: 1


Note: This only works on Linux/Unix, because it relies on a Linux/Unix-only command to pull up the list of files. A future update may include support for Windows, though it would be pretty easy to find out how to fix it.

Get file extension

function file_extension($filename)
{
    $path_info = pathinfo($filename);
    return $path_info['extension'];
}

Return the extension from a file name

// Return the extension from a file name

/(.*\.)(.*$)/.match(File.basename(file_name))[2]

Find files with a certain extension, where the files contain a certain search term

Searches the current directory and deeper for files ending with the 'php' extension, where the file itself contains 'search_term'. Useful if you're searching a large website with lots of images and you only want to find a certain function or variable.

find ./ -type f -name \*.php -exec grep -il "search_term" {} \;

Find files with certain extensions

Searches the current directory and deeper for several extensions. In this example both files mathing 'php', 'html' and 'tpl' match the search.

find ./ -regex ".*\(php\|html\|tpl\)$"


As an alternative you could also use:

find ./ -iname "*.php" -or -iname "*.tpl" -or -iname "*.html"

Find files with a certain extension

Searches the current directory and deeper for all files having an extension 'php'. Change this to the extension you're looking for.

find ./ -type f -name \*.php
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS