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!Related posts:
Leave a Reply
About Me
Tags
My Twitter
Categories
- Algorithms
- Bash
- BlackBerry
- Collaboration
- Command Line
- Cool Tricks
- Easter Eggs
- Ebooks
- Firefox
- Hardware
- Humor
- Linux
- Linux Development
- Linux Kernel
- Networks
- Open Knowledge
- Other
- Productivity
- Programming
- Science
- Security
- Shell Scripts
- Short Posts
- Thoughts
- Tools
- Vim
- Web Development
- Websites
Popular Posts
Calendar
Archives
- March 2010 (1)
- January 2010 (1)
- December 2009 (2)
- September 2009 (13)
- July 2009 (1)
- June 2009 (6)
- May 2009 (4)
- March 2009 (18)
- February 2009 (10)
- January 2009 (10)
- December 2008 (7)
- November 2008 (8)
- October 2008 (1)
- August 2008 (1)
- July 2008 (1)
- June 2008 (2)

Me @ Social Media