(Modified 2011 Nov 10)
Find
I've seen two syntaxes:
find dir_name/ string*ext -print
find "string*ext" -print
In the first, the directory name to be searched must be named explicitly. In the second, the directory names is optional, but the string must be enclosed in quotes. I'll be general and include both the directory name and enclose in quotes.
To search under the current directory:
find . -name "*pattern*" -print
To search for only directories under the current directory:
find . -name "*pattern*" -type d -print
To find normal files, change the d to f.
To search for only directories under the current directory:
find . -name "*pattern*" -type d -print
To search for files and them operate on them:
find . -name "*pattern*" -exec ls -l '{}' \;
An obvious use is to remove files, in which case, change the 'ls -l' above to 'rm -v' ('v' for verbose).
To search for directories and sort them:
find . -name "*pattern*" -type d -print | sort
To print the date:
find -name "*pattern*" -printf "%t %p\n"
where:
%t prints the time (where day of the week and month are written as text)
%p prints the full path (equivalent to %h/%f for directory/file)
\n adds a carriage return
To print the date as a number:
find -name "*pattern*" -printf "%T+ %p\n"
To sort the files by date:
find -name "*pattern*" -printf "%T+ %p\n" | sort
To print the file size:
find -name "*pattern*" -printf "%s bytes %p\n"
To sort the files by size:
find -name "*pattern*" -printf "%s bytes %p\n" | sort -n
Without the flag -n, the sort command would simply sort by the first digit, then by tiebreakers, even if the file sizes have different numbers of digits.
To sum the sizes of files:
find -name "*pattern*" -printf "%s bytes %p\n" | awk '{sum = sum + $1} END {print sum}'
This page is Lynx-enhanced