Linux find command a real time saver

Hi Folks,

For anyone starting out in linux sooner or later you will find yourself using the command line, here is a list of functions you can perform with ‘find’

The most simple and obvious way of using ‘find’ is to just search for a file or files e.g.

find / -name somefile.txt


find / -name somefile.*

In the first example you are searching for a file called somefile.txt, in the second file you are searching for somefile.* the wildcard ‘*’ tells find that you want to find anything that starts with somefile. this would find anything start starts with ‘somefile.’ e.g. somefile.pdf,, somefile.txt etc etc

Perhaps you want to find a file in a specific directory you can use it in the same way but with a specified directory e.g.

find /home/john/documents/ -name somefile.txt

you can take this a step further and search for a file that has been modified in the last 24 hours with e.g.

find /home/user/Downloads/ -mtime 0

The last example will display a list of files in the /home/user/Downloads directory that have been modified in the last 24 hours.

Cool eh? well the fun has only just started.

Maybe you want specific details about those files. For example, if you wanted to find files which are writable by both their owner and their group you could use e.g.

find / -perm -444 -perm /222 ! -perm /111

You can use the find command in conjunction with other commands to perform tasks that could take an age to do manualy
for example cleaning up old files e.g.

Delete or move files Older Than ‘5′ days

find /path/to/files/* -mtime +5 -exec rm {} \;

Adding -f forces the delete e.g.

find /path/to/files/* -mtime +5 -exec rm -f {} \;

Moving instead of deleting e.g.

find /path/to/files/* -mtime +5 -exec mv {} /path/to/newfolder/ \;

in the above examples you are giving a target directory for the ‘find’ command to use ‘/path/to/files/*’ then telling it to look for files that are +5 days old with -mtime then passing the result to a new function, in the first example the -exec funtion passes the result to ‘rm’ (remove).

This command searches through the /usr/local directory for files that end with the extension .html. When these files are found, their permission is changed to mode 644 (rw-r–r–).

find /usr/local -name *.html -exec chmod 644 {} \;

Another good example would be if you wanted to convert a specific image from .jpg to .png you could use e.g.

find /home/john/pictures/ -name input.jpg -exec convert input.jpg output.png

Not that usefull on its own as you could just as easy browse to the directory and run the same command but its just as an example of passing files to other functions, I wont get in to using variables to do hundreds of images with 1 comand line unless anyone wants me to?

And finaly, one of my most regular uses is e.g.

$ find /mp3-collection -name ‘Metallica*’ -and -size +10000k
$ find /mp3-collection -size +10000k ! -name “Metallica*”
$ find /mp3-collection -name ‘Metallica*’ -or -size +10000k

The 1st command searches within the directory /mp3-collection for files that have their names beginning with ‘Metallica’ and whose size is greater than 10000 kilobytes (> 10 MB).
The 2nd command searches in the same directory as above case but only for files that are greater than 10MB, but they should not have ‘Metallica’ as the starting of their filenames.
The 3rd command searches in the same directory for files that begin with ‘Metallica’ in their names or all the files that are greater than 10 MB in size.

$ find / – name ‘Metallica*’ -exec ls -l {\}\ \;

This command would find all the files on your system that begin with the letters ‘Metallica’ and would then execute the ‘ls -l’ command on these files. So basically you would be able to see the details of the files that were returned according to your search criteria.

The words following the -exec option is the command that you want to execute i.e. ls -l in this case.
{\}\ is basically an indicator that the filenames returned by the search should be substituted here.
\; is the terminating string, and is required at the end of the command

 There are TON’s of other options and uses but I think the above should get you well on your way.

PLEASE comment and share your own uses or ask questions on how to do x? 😀

Happy finding!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: