Using Find Command to Find & Manipulate Files

The “find” command is a powerful command, which lets one search for files according to many criteria.

Below are a few examples. I found them here. In this post I just trimmed some of the text of the original post in order to have a quick reference without much explanations.

1. Find the file “myfile.txt” in the current directory and all its sub-directories:

find -name “myfile.txt”

2. Like above, but ignoring case:

find -iname “myfile.txt”

3.  Find the file “myfile.txt” in current directory and its sub-directories upto 3 directory levels down:

find -maxdepth 3 -name “myfile.txt”

4. Find the file “myfile.txt” in sub-directories of current directory, starting from directory level 3:

find -mindepth 3 -name “myfile.txt”

* Note: A combination of mindepth and maxdepth can be used to look for files from level N to level M

5. Find the file “myfile.txt” and apply the command “cat” on it:

find -name “myfile.txt” -exec cat {} \;

6. Find files in the current directory whose names are not myfile.txt:

find -maxdepth 1 -not -name “myfile.txt”

7. Look for the file with inode number 1234567 and rename it to newname.txt

find -inum 1234567 -exec mv {} “newname.txt” \;

8. Find files that have read permessions only to group:

find -perm g=r -type f -exec ls -l {} \;

OR

find -perm 040 -type f -exec ls -l {} \;

9. Find all empty (zero byte) files in home directory and all its sub-directories:

find ~ -empty

10. Find all non-hidden empty files in current directory:

find . -maxdepth 1 -empty -not -name “.*”

11. Find the top 5 largest files in current directory and its sub-directories:

find . -type f -exec ls -s {} \; | sort -n -r | head -5

12. Find top 5 smallest files in current directory and its sub-directories:

find . -type f -exec ls -s {} \; | sort -n | head -5

13. Find top 5 smallest files in current directory and sub-directories, excluding empty files:

find . -type f -not -empty -exec ls -s {} \; |sort -n | head -5

14. Find only socket files in current directory and its sub-directories:

find . -type s

* Nots: use ’s’ for sockets, ‘d’ for directories, ‘f’ for refular files.

15. Find files newer (modified later) than myfile.txt:

find -newer “myfile.txt”

16. Find files with size greater than 100M:

find . -size +100M

17. Find files smaller than 100M:

find . -size -100M

18. Find file whose size is exactly 100M:

find . -size 100M


Original article here.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Related posts:

  1. Renaming Files In Linux
  2. Navigate Efficiently To Your Most Used Directories With Autojump
  3. Create a Directory Tree With One Command
  4. Copy a Directory Along With Its Relative Path
  5. Edit The Command Line With Vim

Tags: , ,

Saturday, March 7th, 2009 Bash

Leave a Reply

my email
Already a member?
Login
Login using Facebook:
Last visitors
my photo
Hi,
My name is Amir Watad. I have a BSc. in biomedical engineering from The Biomedical Engineering school , Technion , Israel, and am currently studying for a BSc. in electrical engineering at The Electrical Engineering school , Technion , Israel.
I work at the verification dept. in Mellanox Technologies Ltd.
I love Linux, the Command Line and the OpenSource Community.
I used to write Poems (Arabic) when I was able to find time for this.
March 2009
S M T W T F S
« Feb   May »
1234567
891011121314
15161718192021
22232425262728
293031  
Designed for Linux. Vista Incapable
SEO Powered by Platinum SEO from Techblissonline

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.