Wednesday, December 9, 2015

Most useful find commands for Linux SystemAdmin.

1 : Search the file aaa from current directory downwards and print it.

# find . -name aaa - print

2 : Find all files which begin with 'a' or 'b' from current directory.

# find . -name [ab]* -print

3 : Search directories called backup from /usr directory.

# find /usr -type d -name backup -print

4 : Search normal files called backup from /usr directory.

# find /usr -type f -name backup -print

5 : Search character special files called backup from /usr directory.

# find /usr -type c -name backup -print

6 : Search block special files called backup from /usr directory

# find /usr -type b -name backup -print

7 : Search all directories from /usr whose inode number is 1235 and print them.

# find /usr -inum 1235 -print

8 : Search in root directory for all files which have less than 2 links.

# find / -links -2 -print

9 : Search in current directory for all files whose owner is abc1 and group is grp1.

# find . \(-user abc1 -a -group grp1 \) -print

10 : Search in current directory for all files whose owner is abc1 or whose name is myfile1.

# find . \(-user abc1 -o -name myfile1 \) -print

11 : Search in current directory for all files which have permissions 777.

# find . -perm 777 -print

12 : Search in current directory for all files chose size is 10 blocks.

# find . -size 10 -print

13 : Search in current directory for all files whose size  10 bytes(characters).

# find . -size 10c -print

14 : Search in current directory for all files whose size is greater than 10 byets.

# find . -size +10c -print

15 : Search in current directory for all files which were accessed exactly 7 days back.

# find . -atime 7 -print

16 : Search in current directory for all files which have not been accessed since last 7 days (or in other words which were accessed more than 7 days ago.)

# find . -atime +7 -print

17 : Search in current directory for all files which have not been modified since last 7 days (or in other words which have been modified more than 7 days ago.)

# find . -mtime +7 -print

18 : Search in current directory for all files whose status has changed (on creation or modification ) more than 7 days ago.

# find . -ctime +7 -print

19 : Search in current directory for all files whose name is cog and instead of printing their names executes a command rm on the searched files.

# find . -name cog -exec rm {} \;

Here, the {} indicate that the searched files would become arguments for rm.The semicolon is necessary and it has to be preceded by a \ to take away its special meaning.

20 : Same as above except that this time it should ask for confirmation before executing rm command.

# find . -name cog -ok rm {} \;


No comments:

Post a Comment