One-liner find commands

Some one-liners to find files and/or folders, mostly using Unixfind” 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 files which has extension “.java” and the text “Locale.ITALY” in its content:

$ find . -type f -name "*.java" | xargs grep "Locale.ITAL" -n
 

* Find all emty folders then remove them:

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

* Find 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

* Remove all “.svn” folders in current folder ( “.” ) , including sub folders (recursive):

find . -iname ".svn" -print0 | xargs -0 rm -r

 

Have fun :) ,

.

./.

About DucQuoc.wordpress.com

A brother, husband and father...
This entry was posted in Linux. Bookmark the permalink.

3 Responses to One-liner find commands

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

  2. Pingback: Git pull all | DucQuoc's Blog

  3. Pingback: Grep Sed Awk | 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 )

Facebook photo

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

Connecting to %s