Pages

Thursday, December 17, 2015

find command one liners - Part01

Home

To find files using name (considering case)
# find / -name “file_name"

To find files using name (ignoring case) 
# find / -iname "file_name"

To limit search to specific directory level using mindepth and maxdepth
# find / -name passwd

Find the passwd file under / level only i.e upto depth 1
# find / -maxdepth 1 -name passwd

Find the passwd file under / and one level down i.e. upto depth 2
# find / -maxdepth 2 -name passwd

Find the passwd file under / and two level down i.e. upto depth 3
# find / -maxdepth 3 -name passwd

Find the passwd file under / and three level down i.e. upto depth 4
# find / -maxdepth 4 -name passwd

Find the passwd file under / and four level down i.e. upto depth 5
# find / -maxdepth 5 -name passwd

Find the passwd file under / and five level down i.e. upto depth 6
# find / -maxdepth 6 -name passwd

Find the passwd file under 2 level down form / and till 5 level down from / i.e. from depth 2 to 5
# find / -mindepth 3 -maxdepth 6 -name passwd

To execute additional command on results found using find command 
Find md5sum of files with name passwd ignoring case
# find / -name "passwd" -exec md5sum {} \;
# find / -iname "passwd" -exec md5sum {} \;

Find size of files with name passwd ignoring case
# find / -name "passwd" -exec du -sh {} \;
# find / -iname "passwd" -exec du -sh {} \;

Inverting the match.
To find files or directories whose name are not file_name with maxdepth is 1
# find / -maxdepth 1 -not -iname "file_name"

To find files by its inode numbers 
Every file has a unique inode number, using that we can identify that file. Create two files with similar name. i.e one file with a space at the end.
# touch "test-file-name" "test-file-name " #Note: There is a space at the end
# ls -1 test*
test-file-name
test-file-name

From the ls output, you cannot identify which file has the space at the end. Using option -i, you can view the inode number of the file, which will be different for these two files.
# ls -lrti test*
31994 test-file-name
31995 test-file-name
# find -inum 31995
./test-file-name
# find -inum 31994
./test-file-name

You can specify inode number on a find command as shown below. In this example, find command renames a file using the inode number.
# find -inum 31994 -exec mv {} new-test-file-name \;
# ls -lrti *test*
31994 new-test-file-name
31995 test-file-name

This technique is useful, when you want to do some operation with the files which are named poorly as shown in the example below.
For example, the file with name 'file?.txt' has a special character in it. If you try to execute 'rm file?.txt' all the following three files will get removed. So, follow the steps below to delete only the 'file?.txt' file.
# ls -lrti file*
file1.txt
file2.txt
file?.txt

Find the inode numbers of each file.
# ls -lrti file*
31996 file1.txt
31997 file2.txt
31998 file?.txt

Use the inode number to remove the file that had special character in it as shown below.
# find -inum 31998 -exec rm {} \;
# ls -lrti file*
31996 file1.txt
31997 file2.txt
Note: The file with name "file?.txt" is now removed, normal rm will not operate on inode hence using it with find can do the trick

Find files which has read permission to group.
# find . -perm g=r -type f -exec ls -l {} \;

Find files which has read permission to user.
# find . -perm u=r -type f -exec ls -l {} \;

Find files which has read permission to others.
# find . -perm o=r -type f -exec ls -l {} \;

Find files which has read permission only to group using octal value
# find . -perm 040 -type f -exec ls -l {} \;

To find all empty files (zero byte file) in a directory and it’s subdirectory
# find / -empty

List all the empty files only in / directory i.e. upto depth 1
# find . -maxdepth 1 –empty

List only hidden empty files only in / directory.
# find / -empty -name ".*"

List only the non-hidden empty files only in / directory.
# find / -empty -not -name ".*"

To find top 5 Big files in size
# find /home -type f -exec ls -s {} \; | sort -n -r | head -5
# find /home -type f -exec du -sk {} \; | sort -n -r | head -5
Note: This command can be CPU intense sometimes as size calculation is required in command 

To find top 5 small files in size
# find /home -type f -exec ls -s {} \; | sort -n  | head -5
Note: This command can be CPU intense sometimes as size calculation is required in command 
In the above command, most probably you will get to see only the ZERO byte files (empty files). So, you can use the following command to list the smaller files other than the ZERO byte files.
# find /home -not -empty -type f -exec ls -s {} \; | sort -n  | head -5

Find only the socket files.
# find / -type s

Find all directories
# find / -maxdepth 1 -type d

Find only the normal files
# find / -maxdepth 2 -type f |head -10

Find all the hidden files
# find / -maxdepth 2 -type f -name ".*" |head -10

Find all the hidden directories
# find / -maxdepth 2 -type d -name ".*" |head -10

Find files by comparing with the modification time of other file
Show files which are modified after the specified file. The following find command displays all the files that are created/modified after ordinary_file.
# ls -lrt
total 24
----r----- 1 samual samual 0 Dec 17 08:06 others_can_only_read
-rw-r----- 1 samual samual 0 Dec 17 08:06 others_can_also_read
-rw------- 1 samual samual 0 Dec 17 08:06 ordinary_file
---------- 1 samual samual 0 Dec 17 08:41 no_for_all
-rw-r--r-- 1 samual samual 0 Dec 17 08:41 everybody_read
-rwxrwxrwx 1 samual samual 0 Dec 17 08:41 all_for_all

# find -newer ordinary_file
./everybody_read
./all_for_all
./no_for_all

Find files bigger than the given size
# find / -type f -size +100M
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/proc/kcore

Find files smaller than the given size
# find / -type f -size -100M|head
Note:  - means less than the give size,
Find files that matches the exact given size
# find / -size 100M
Note: no symbol means exact given size.
Find files smaller than the given size
# find / -type f -size -100M|head
Note: + means more than the given size,

Create Alias for Frequent Find Operations
If you find something as pretty useful, then you can make it as an alias. And execute it whenever you want.
Remove the files named a.out frequently.
# alias rmao="find . -iname a.out -exec rm {} \;"
# rmao

Remove files with name “core” generated by c program.
# alias rmc="find . -iname core -exec rm {} \;"
# rmc

Remove big archive *.zip files that are over 100M.
# find / -type f -name *.zip -size +100M -exec rm -i {} \;"

Remove all *.tar file that are over 100M using the alias rm100m (Remove 100M). Use the similar concepts and create alias like rm1g, rm2g, rm5g to remove file size greater than 1G, 2G and 5G respectively.
# alias rm100m="find / -type f -name *.tar -size +100M -exec rm -i {} \;"
# alias rm1g="find / -type f -name *.tar -size +1G -exec rm -i {} \;"
# alias rm2g="find / -type f -name *.tar -size +2G -exec rm -i {} \;"
# alias rm5g="find / -type f -name *.tar -size +5G -exec rm -i {} \;"
You are requested to use below commands to get desired results
# rm100m
# rm1g
# rm2g

# rm5g


Back To Top
Home

No comments:

Post a Comment