Monday, 5 August 2019

Useful find command in Unix/Linux with example



Find command in Unix is a extremely useful command. you can search for any file anywhere using this command provided that file and directory you are searching has read write attributes set to you ,your, group or all. Find descends directory tree beginning at each path name and finds the files that meet the specified conditions.


conditions of find
The basic syntax of the find command is:
find path options
where path is the path to search
and options are the options given to find command.
Options:
-atime +n |-n| n will find files that were last accessed more than n or less than -n days or n days.
-ctime +n or -n will find that were changed +n -n or n days ago.
-depth descend the directory structure, working on actual files first and then directories.
-exec command {} \; run the Unix command on each file matched by find. Very useful condition.
-print print or list to standard output (screen).
-name pattern find the pattern.
-perm nnn find files whole permission flags match octal number nnn.
-type l/d/f find links,directory and files
-size <size> Find files greater than given size

find command in Unix with example

In all our below Find command in Unix/Linux  with examples, the path is our current directory and hence we use .(dot).
To find file with name like “test*”
find . -name "test*" -print
find file command in unix
To find files modified in the last 2 days:
find . -mtime -2
To find files modified before 1 days:
find . -mtime +1
find mtime command example unix
To find files modified in the last 10 mins:
find . -mmin -10
find mmin command example in Unix
To find files modified before 10 mins:
find . -mmin +10
The above commands will find both files and directories modifying the criteria. If you want to find only files, use the -type option.
find . -type f -mmin -40
This will find only the files modified in the last 40 mins, not directories.
Show all links
find . -type l -exec ls -ld {} \;
show all directories
find . -type d -exec ls -ld {} \;
Unix find directory command
More Linux find command examples
Print will lists all files in your home directory
find $HOME
Print will list all files named x1 in /work directory.
find /work -name x1
Print will list all man page directories.
find / -type d -name 'man*' -
Command to remove all empty files on system.
find / -size 0 -exec rm {} \;
List all the files having more than 100000 bytes
find . -type f -size +100000 -exec ls -al {} \;
move (mv) files older then 1 day to dir TMP
find . -atime +1 -type f -exec mv {} TMP \;
Remove the error-file
find . -name "error-file" -exec rm {} \;
Compress all the file which are not compressed
find . \! -name "*.Z" -exec compress -f {} \;
find with exec command line option example
Search all the files like *.txt
find . -name "*.txt"
Change the permission(chmod) of all the files in the directory to 755
find . -exec chmod 755 {} \;
Count files  which are accessed after 30 days
find . -atime +30 -exec ls –l {} \; | wc -l
Find command to prevent printing error message like permission denied while using find command
find . -name xeroxrelease* -print 2>/dev/null
Remove tmp* files in the current directory and one level down directory
find . -name 'tmp*' -maxdepth 2 -exec rm {} \;
Find files which has write permission to group
find . -perm -g=w -type f -exec ls -l {} \;
Find and print files in the current directory and two level down directory and then copy that list /tmp/tmpfiles file
find . -name 'tmp[0-9][0-9]*' -maxdepth 2 -print | tee -a /tmp/tmpfiles
find and replace in a range of files
find ./ -type f -exec sed -i 's/tom/dick/g' {} \;
Some more information about find command in Unix with example
-mmin option of find is not available in all Unix flavors . If you dont have the mmin option, use the following:
Step 1:First create a dummy file whose timestamp is the time you are looking for:
touch -d "10 mins ago" temp
The above touch command will create a temp file whose timestamp is 10 mins before. For example, if the time now is 8 hours 50 mins, the temp file timestamp will be 8 hours 40 mins.
If your Unix flavor does not have the “-d” option in the touch command, you can use the following method to set the timestamp:
touch -t 1006021020 temp
This creates a temp file whose time stamp is 2010,June 2, 10 hours 20 mins. [YYMMDDHHMM]
Step 2: search files which are modified after this file temp has been modified. The below command will display all the files modified after the temp has been modified OR in other words find files which are newer than temp file:
find . -newer temp
Similarly, to find files which are modified before 10 mins. In other words to negate the above search, use the exclamation:
find . ! -newer temp
In the same way, we can find files modified from any time we need.
Links of other resources on Find

0 comments:

Post a Comment