find Command Examples in Linux (Advanced)

I would like to share some useful commands for daily use. Here are some examples.

Find command AND Operator

By default find command will use AND option between two options. No need of mentioning any option. For example see below examples.

1. Find all the files which are more than 100MB and less than 1GB in size.

# find / -size +100M –size -1G
or
# find / -size +100M -a -size -1G

2. Find all the files which are with more than size 100MB and less than 1GB and the owner of the file is geek and the file name is address.txt in /add folder:

# find /add –size +100M –size -1G –user geek –iname address.txt

3. Find all the files with SGID for the group prod and with size exactly 100MB with file name as user.txt under /dept:

# find /dept –size 100M –group prod –perm g+s –name user.txt

Search for files and execute commands on Found files

1. Find a file with user.txt in /data folder and long list this file for checking file properties.

# find /data –iname user.txt –exec ls –l {} ;

Let me explain the above command. Up to find /data –iname user.txt, this command you are aware of. This command will search for user.txt file in /data folder and give the paths and file names where this file is located.

Here,

  • -exec - with this option we are saying to find command to execute a command(here its ls -l) followed by this option
  • {} - This is used as input to the command which we get as files/folders from find command output.
  • ; – This indicates that find command is completed.

Actually ; is a command chaining capability and it’s a special character. In order to negate this we are using a special character before ;.

2. Find all the files with name user.txt in /geek and change the ownership of the files from geek to user1:

# find /geek –user geek –name user.txt –exec chown user1: {} ;

3. Find all the files with name data.txt owned by geek in /movie and change the permissions to 555 to them:

# find /movie –user geek –name data.txt –exec chmod 555 {} ;

4. Find files which are more than 1GB and not accessed for the past 3 months and delete them.

# find / -size +1G -mtime +90 –exec rm –rf {} ;

Caution: Be careful when using -exec option. This is very dangerous option which can change/remove anything if don’t use wisely.

Find command with multiple -exec option

Find files with oracle.txt name in /data directory change the owner permissions from geek to oracle and change the permissions to 775:

# find /data –user geek –name oracle.txt –exec chown oracle: {} ; -exec chmod 775 {} ;

Find command OR –o operator

1. Find have OR operator to do multiple file searches at a time. I want to search for oracle.txt and learn.c files at a time. This can be achieved by using -o operator:

# find / -name oralce.txt -o -name learn.c

2. I want to find two directories say data and oracle:

# find / -type d ( -name data -o -name oracle )

find command ! Negation operator

Negation operator is useful for negating a search team. for example we want to find all the files with name user.txt which don’t have 555 permissions:

# find . -type f ! -perm 555 -name user.txt

In the above command -perm 555 is negated so that all the files with name user.txt is displayed expect file user.txt with permissions 555.

Search for files in multiple locations

1. Search multiple locations using single find command We can accomplish searching multiple folders with single command without any options. Find oracle.txt file in /opt and /var folder at a time:

# find /opt /var –name oracle.txt

2. Search in entire system expect /data folder:

#find / -path /data -prune -name oracle.txt

The -path variable to define the path of a location. And -prune combined with -path will say not to descend in to the mention path /data.

Search for multiple files

1. Find all the files which start with ora and ends with different extension in /opt folder:

# find /opt –name "ora.*"

2. Find files which always end with dump:

# find / -name "*dump"