Tagged: find

Linux: Getting to know `find` command

The find command is one of most important and much used command in my opinion. It's very useful because it not only finds files and directories with detailed options but also can execute additional commands (ex: mv, rm, etc...) on found items.

I'm ashamed to say this but, on the other day, I accidentally corrupted my external USB hard drive that had all of my back-up files! I used TestDisk hoping to fix its partition table but it didn't work. So I used PhotoRec to recover photos.

If you have used PhotoRec before, you know this but it does not recover files with original names. Instead, it creates a directory, recup_dir.[number], and put files with a unique names like f1175051952.jpg. In my case, it created more than 3000 directories with image files scattered all over. The find command came in handy!

Disclaimer:
The information in this site is the result of my researches in the Internet and of my experiences. It is solely used for my purpose and may not be suitable for others. I will NOT take any responsibility of end result after following these steps (although I will try to help if you send me your questions/problems).

Finding all .jpg files and ignoring case:

Find all files whose name has .jpg extension in the current directory and below. $ find . -iname "*.jpg" -print

Finding and moving all .jpg files in one single pass:

Find all files whose name has .jpg extension in the current directory and below and move them to /mnt/jpg. $ find . -iname "*.jpg" -type f -exec mv {} /mnt/jpg \;

Finding and removing empty directories:

Find empty directory in the current directory and below and remove them. $ find . -type d -empty -exec rmdir {} \;

Finding files with no extensions:

Find files whose name does not contain extension in the current directory and below. $ find . -type f ! -name "*.*"

Finding files without .jpg extension:

Find files whose name does not have .jpg extension in the current directory and below. $ find . -type f ! -name "*.jpg"

Here is some other useful options.

Finding files with 777 permissions:

Find files whose permissions are 777 $ find . -type f ! -perm 0777 -print

Finding files based on user:

Find files which belong to user ubyt3m3 under /home directory. $ find /home -user ubyt3m3 -print

Finding accessed files in last 1 hour:

Find files which are accessed in last 1 hour under /var/log directory. $ find /var/log -amin -60 -print

Finding last 7-14 days modified files:

Find files which are modified in last 1 hour under /home/www directory. $ find /home/www -mtime +7 -mtime -14 -print

That's all!
-gibb