One-liner find commands

Some one-liners to find files and/or folders, mostly using Unix “find” command.

The target directory here for finding is /opt , yet it can be applied for any existing folder , from “/” to “/proc/some/path” …

 

* Find all files and folders which contain “log” in filename:

$ find /opt -name "*log*"

 

* Find all directories which contain “log” in filename:

$ find /opt -name "*log*" -type d

 

* Find all files which contain “log” in filename and older than 3 days:

$ find /opt -name "*log*" -type f -mtime +3

 

* Find all files which contain “log” in filename and older than 90 minutes then remove them:

$ find /opt -name "*log*" -type f -mmin +90 -delete
$ find /opt -name "*log*" -type f -mmin +90 -exec rm {} \;
$ find /opt -name "*log*" -type f -mmin +90 | xargs rm

(Note:

1/ some old “find” will not have -mmin or -delete options

2/ -exec is better than xargs when doing multi commands: -exec cmd1 \; exec cmd2 \;

3/ xargs is better than -exec when avoiding potential issues like buffer-overflowing or typo (quotes, semi-colon)

)

 

* Find all classes in the some java JAR files and writing output to a file:

$ find /opt -name "*.jar" -exec unzip -l {} \; > all_classes.txt

 

* Find all files which has file size bigger than 10000 bytes:

$ find /opt -type f -size +10000c

 

* File all files which has the text “log” in its content:

$ find /opt -type f | xargs grep "log" -n

 

* File all emty folders then remove them:

$ find /opt -type d | xargs rmdir 2> /dev/null

 

* File all duplicate files (based on MD5 checksum) – usually they are in .svn folders :

$ find /opt -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate

 

 

Have fun :) ,

 

./.

 

About DucQuoc.wordpress.com

A coder, brother, and player at times...
This entry was posted in Linux. Bookmark the permalink.

One Response to One-liner find commands

  1. Pingback: Logging best practices | DucQuoc's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s