Find your files

In Linux you can use the command ‘find’ to find files and folders and then also do something with them on your system.

Find all files that ends with the .php extension in the folder /var/www/:
find /var/www/ -type f -name '*.php'

This will give you a list with a lot of files.
So, what? What is next?

Well, you can search for words in those files:
find /var/www/ -type f -name '*.php' | xargs grep 'foobar'

that will give files with file extension .php that has the word ‘foobar’ in it.


find /var/www/ -type f -iname '*.php' | xargs grep 'foobar'
Does the same right? Almost, only that it also search for also files with .PHP (iname means none-case-sensitive)


find /var/www/ -type d -name 'tmp' -exec ls -ald {} \;
will find all the folders under /var/www/ with the name “tmp” and list the information out about these folder.
Here is my output from my server:
find / -type d -name 'tmp' -exec ls -ald {} \;

drwxrwxrwt. 11 root root 4096 Oct 22 21:54 /tmp
drwxrwxrwt. 2 root root 4096 Oct 22 16:33 /var/tmp
drwxrwx–T. 2 root lp 4096 Sep 5 10:28 /var/spool/cups/tmp
drwxr-xr-x 2 root root 4096 May 9 10:30 /backup/db/tmp

Let us get to something more interesting:

Find all files that has the string rocket or ROCKET in their filename, and move them to the folder /tmp:
find / -type f -iname '*rocket*' -exec mv {} /tmp/ \;

Leave a Reply

Your email address will not be published. Required fields are marked *