Saturday, 21 October 2017

Linux Unix Find Command

                                    Find Command in Unix and Linux Examples

 

About find command  :

  • find is a command-line utility that searches one or more directory trees of a file system, locates files based on some user-specified criteria and applies a user-specified action on each matched file
  • findlocates files on your system. Within each directory tree specified by the given paths, it evaluates the given expression from left to right, according to the rules of precedence

 

 

Find Command Example

Find command Example Description

find . -name input.txt

Find all the files whose name is input.txt in a current working directory

find /home -name input.txt

Find all the files under /home directory with name input.txt

find /home -iname input.txt

Find Files Using Name and Ignoring Case

Find all the files whose name is input.txt and contains both capital and small letters in /home directory

 

find / -type d -name agoutam

Find all directories whose name is agoutam in / directory

find . -type f -name "*.php"

Find all php files in a directory

find . -type f -perm 0777 -print

Find all the files whose permissions are 777

find / -type f ! -perm 777

Find Files Without 777 Permissions

find / -perm 2644

Find SGID Files with 644 Permissions

find / -perm 1551

Find all the Sticky Bit set files whose permission are 551

find / -perm /u=s

Find all SUID set files

find / -perm /g=s

Find all SGID set files.

find / -perm /u=r

Find all Read Only files.

find / -perm /a=x

Find all Executable files

find / -type f -perm 0777 -print -exec chmod 644 {} \;

Find all 777 permission files and use chmod command to set permissions to 644.

find / -type d -perm 777 -print -exec chmod 755 {} \;

Find all 777 permission directories and use chmod command to set permissions to 755.

find . -type f -name " input.txt " -exec rm -f {} \;

 

To find a single file called input.txt and remove it.

# find . -type f -name "*.txt" -exec rm -f {} \;

OR

# find . -type f -name "*.mp3" -exec rm -f {} \;

 

To find and remove multiple files such as .mp3 or .txt, then use

find /tmp -type f -empty

To find all empty files under certain path.

find /tmp -type d -empty

To file all empty directories under certain path

find /tmp -type f -name ".*"

To find all hidden files, use below command.

find /home -user agoutam

To find all files that belongs to user agoutam under /home directory

find /home -group developer

 

To find all files that belongs to group Developer under /home directory

find /home -user agoutam -iname "*.txt"

To find all .txt files of user agoutam under /home directory.

find / -mtime 50

To find all the files which are modified 50 days back.

find / -atime 50

To find all the files which are accessed 50 days back.

find / -mtime +50 –mtime -100

To find all the files which are modified more than 50 days back and less than 100 days

find / -cmin -60

To find all the files which are changed in last 1 hour

find / -mmin -60

To find all the files which are modified in last 1 hour.

find / -amin -60

To find all the files which are accessed in last 1 hour.

find / -size 50M

To find all 50MB files, use

find / -size +50M -size -100M

To find all the files which are greater than 50MB and less than 100MB.

find / -size +100M -exec rm -rf {} \;

To find all 100MB files and delete them using one single command.

find / -type f -name *.mp3 -size +10M -exec rm {} \;

Find all .mp3 files with more than 10MB and delete them using one single command.

find . -type f \( -name "*.sh" -o -name "*.txt" \)

To find all files in the current directory with .sh and .txt file extensions, you can do this by running the command below:  here in command -0 means or

find . -type f \( -name "*.sh" -o -name "*.txt" -o -name "*.c" \)

To find three filenames with .sh, .txt and .c extensions, issues the command below

find /home/agoutam/Documents/ -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.deb" -o -name ".pdf" \)

To find files with .png, .jpg, .deb and .pdf extensions

!find

 How to run the last executed find command?

find -maxdepth 3 -name "sum.java"

How to print the files in the current directory and two levels down to the current directory?

find -mindepth 2 -maxdepth 5 -name "sum.java"

How to print the files in the subdirectories between level 1 and 4

\( -mtime +7 -atime +30 \)

 To match all files modified more than 7 days ago and accessed more than 30 days ago

\( -mtime +7 -o -atime +30 \)

To match all files modified more than 7 days ago or accessed more than 30 days ago

\! -name notme.txt -name \*.txt

you may specify "not" with an exclamation point. To match all files ending in .txt except the file notme.txt, use

 

 find /mydir1 /mydir2 -size +2000 -atime +30 -print

To report all files starting in the directories /mydir1 and /mydir2 larger than 2,000 blocks (about 1,000KB) and that have not been accessed in over 30 days, enter:

find /mydir -atime +100 -ok rm {} \;

To remove (with prompting) all files starting in the /mydir directory that have not been accessed in over 100 days, enter:

find /mydir \(-mtime +20 -o -atime +40\) -exec ls -l {} \;

To show a long listing starting in /mydir of files not modified in over 20 days or not accessed in over 40 days, enter:

find /prog -type f -size +1000 -print -name core -exec rm {} \;

To list and remove all regular files named core starting in the directory /prog that are larger than 500KB, enter:

 

 

No comments:

What is iSCSI and How Does it Work?