Home » 2016 » December » 11 » Some great usage and example of find command in Linux

10:49 PM
Some great usage and example of find command in Linux

Hello Linux/Unix lovers,

I never knew that find command can be so useful. I have collated some cool find command example which can always help you in your work stations.

I am sure you all know the format of find command.

Format: 

find path criteria action


Example

find . -name "*.log" -exec ls -l {} \;

 

1. Find all the log file inside a folder/subfolder and delete or archive them.

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

find . -name "*.log" -type f -exec gzip {} \;

 

2. Let's say you want to delete all txt files which are older than 60 days from /temp folder.

find /tmp -name "*.text" -type f -mtime +60 -exec rm -f {} \;

 

3. Now we want to delete/archive some set of files which are 3 months old and whose size is bigger than 1 mb.

find /path -name "*.log" -type f -mtime +90 -size +1024k -exec rm -f {} \;

 

4. Find out the total size of all log files in a folder which are 30 days older.

find /logs -name '*.log' -type f -mtime +30 -exec du -am {} \;

The above command will list files with their size in mb.

Now you can apply logic to add the first column values to get total size using awk.

find /logs -name '*.log' -type f -mtime +30 -exec du -am {} \; | awk '{ total += $1 }; END { print total }'

Using awk we added all the values in first column and print the total. 


Note:-

du -ak afile shows file size in KB

du -am afile shows file size in MB.

likewise du -ag afile shows file size in GB.

5. List the files' count in a directory which are any files but not log files(*.log).

find /path ! -name "*.log" -type f -exec ls -l {} \; | wc -l

 

6. List out all the directories in a folder recursively whose name is "archive"

find . -name "archive" -type d 

"." points to current directory.

7. Archive any files which are older than 2 days and but exclude the files which are already zipped, from /tmp folder.

find /tmp/ ! -name "*.zip" -type f -mtime +2 -exec gzip -vf {} \;

Here, gzip has below options:

-v -> archive in verbose mode.

-f -> if there is already a file with the name filename.gz, that will be replaced. 

 

8. Search for a particular pattern in all ARD file inside a folder which are older than 90 and get their occurrence count. Don't look into *.zip.gz files

find /path -name *.ARD" -type f ! -name "*.zip.gz" -mtime +90 -exec grep "pattern" {} \; | wc -l

 

I will keep this article updated with new stuff. If you like it share it!!

 
 

Category: Open System-Linux | Views: 1555 | Added by: shanky | Tags: housekeeping, find command, CleanUp, find | Rating: 5.0/1

Related blogs


You may also like to see:


[2014-10-17][Open System-Linux]
SETFACL command in Linux to set file access control list
[2016-05-24][Open System-Linux]
FACTER command in Linux : showing system facts
[2016-10-12][Open System-Linux]
Remove Control M characters from a script or any file in Linux/AIX
[2014-05-22][Open System-Linux]
Archiving and compression of files in Linux
[2014-09-20][Open System-Linux]
Export command in Linux

Total comments: 0
avatar