find
is an extremely powerful command. It not only finds your files and directories, it can be combined with other shell commands to perform actions on those found files and directories.
This post will be an ever growing list of find
command one liners I have found useful.
Recursively find all files in current directory and chmod them to 644
find . -type f -exec chmod 644 {} \;
Recursively find all directories in current directory and chmod them to 755
find . -type d -exec chmod 755 {} \;
Recursively find all files in current directory modified in last minute
find . -type f -mmin -1
Recursively find all files in current directory accessed in the last 5 days and display the time accessed
find . -type f -atime -5 -exec ls -ltu {} \;
Recursively find all files in current directory and get the MD5 hash of each
find . -type f -print0 | xargs -0 md5
Recursively find the last 10 modified files in the current directory and display the time modified
Linux
find . -type f -printf '%[email protected] %p\n' | sort -n | tail -10
The command will return an elongated UNIX timestamp for each file. You can see the human readable time the file was last modified by running the following command:
date -r "<ABSOLUTE PATH OF FILE>"
BSD and OS X
find . -type f -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -10
The command will return a UNIX timestamp for each file. It can be converted to human readable time by using the following command:
date -r <UNIXTIMESTAMP>
Recursively find all files in the current directory and rename them all in lower case
for i in `find . -type f -print`
do
lc=`echo "$i" | tr '[A-Z]' '[a-z]'`
if [ $lc != $i ]; then
mv -i "$i" "$lc"
fi
done
Recursively find all directories in the current directory and rename them all in lower case
This command may have to be run multiple times to actually lower case all directories. The find command does not display the deepest directories first, so if the root directory is renamed before a child directory, the child directory rename will fail.
for i in `find . -type d -print`
do lc=`echo "$i" | tr '[A-Z]' '[a-z]'`
if [ $lc != $i ]; then
mv -i "$i" "$lc"
fi
done