Tuesday, 3 February 2015

Advance find commands examples in Linux

I would like to share some useful commands for daily use. Here is 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.
  • 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

  • Find all the files which are with more than size 100MB and less than 1GB and the owner of the file is vinil and the file name is address.txt in /add folder
#find /add –size +100M –size -1G –user vinil –iname address.txt

  • 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

Caution: Be careful when using -exec option . This is very dangerous option which can change/remove anything if don’t use wisely.
  • 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 above command. Up to find /data –iname user.txt, this command you are aware. This command will search for user.txt file in /data folder and give the paths and file names where this file is located.

-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 special character we are using before;.

  • Find all the files with name user.txt in /vinil and change the ownership of the files from vinil to user1
#find /vinil –user vinil –name user.txt –exec chown user1: {} ;

  • Find all the files with name data.txt owned by vinil in /movie and change the permissions to 555 to them.
#find /movie –user vinil –name data.txt –exec chmod 555 {} ;
  • 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 {} ;
There are many command that you could try with find command. Do a try  !

Find command with multiple -exec option
  • Find files with oracle.txt name in /data directory change the owner permissions from vinil to oracle and change the permissions to 775

#find /data –user vinil –name oracle.txt –exec chown oracle: {} ; -exec chmod 775 {} ;
Note: We can use this multiple –exec option more than two times.

Find command OR –o operator
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
  • 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 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
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
  • 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
  • Find  all the files which start with ora and ends with different extension in /opt folder
#find /opt –name "ora.*"
  • Find files which always end with dump.
#find / -name "*dump"
That’s all! I will be coming up with few more interesting articles on Linux, till then stay tuned to learn Linux and don’t forget to add your valuable comments

1 comment: