find – Linux usages

by mravin

1. Find files using names

# find -name “MyCProgram.c”

2. Find files using name & ignoring case

# find -iname “MyCProgram.c”

3. Limit search to specific directory level using mindepth & maxdepth

# find / -name passwd
# find / -maxdepth 2 -name passwd
# find / -mindepth 3 -maxdepth 5 -name passwd

4. Executing commands on the files found by Find

# find -iname “MyCProgram.c” -exec md5sum {} \;

Calculates md5sum of all the files. {} Is replacd by the current filename.

5. Inverting the match

# find / -not -iname “MyCProgram.c”

6. Finding files by inode number

#ls -il -> gives inode number
# find -inum 16187430 -exec mv {} new-filename \; -> renames the resultant files.

# find -inum 804180 -exec rm {} \;

7. Find files based on file permissions

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

8. Find empty files in home dir

# find ~ -empty
# find . -empty -not -name “.*” -> non hidden files.

9. Find top 5 big files

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

10. Find the top 5 small files

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

11. Find files based on file types

# find . -type s -> socket files
# find . -type d -> directory
# find . -type f -name “.*” -> hidden files
# find . -type d -name “.*” -> hidden directory

12. Find files modified after specific file

# ls -lrt
# find -newer

13. Find files by size

# find ~ -size +100M
# find ~ -size -100M
# find ~ -size 100M

14. Create alias for frequent find

# alias rmao=”find . -iname a.out -exec rm {} \;”
# rmao

15. Remove big archive files using find command

# find / -type f -name *.zip -size +10G -exec rm -i {} \;

16. Find files whose content got updated within last 1 hour

# find . -mmin -60

# find . -mtime -1 -> last 1 day

17. Find files which got accessed before 1 hour.

# find -amin -60
# find / -atime -1

18. Find files which got changed exactly 1 hour ago

# find . -cmin -60
# find . -ctime -1

19. Search only unhidden files using regular expression

# find . -mmin -15 \( | -regex “.*/\..*” \)

20. Find files which are accessed after modofication of a specified file.

# find -anewer /etc/hosts

21. Searching only in the current files system

# find / -name “*.log”

22. Using more than 1 {}

# find -name “*.txt” cp {} {}.bak \;

23. Redirectin errors to /dev/null

# find -name “*.txt” 2>>/dev/null

24. Substitute space with underscore

# find . -type f -name “*.mp3″ -exec rename “s/ /_/g” {} \;

About these ads