Linux/find: Difference between revisions

From Omnia
Jump to navigation Jump to search
Line 44: Line 44:


ref: https://serverfault.com/questions/354403/remove-path-from-find-command-output
ref: https://serverfault.com/questions/354403/remove-path-from-find-command-output
== Indicate directories and files ==
Print the file type along with the name with -printf "%y %p\n": <ref>https://unix.stackexchange.com/questions/652076/how-to-mark-directories-in-the-output-of-the-find-command</ref>
<pre>
$ sudo find . -name 'example.com*' -printf "%y %p\n"
d ./archive/example.com
f ./renewal/example.com.conf
d ./live/example.com
</pre>


== find large files ==
== find large files ==

Revision as of 20:44, 9 May 2025

find

Find file syntax:

find [path] [expression]
# case sensitive
find [path] -name [pattern]
# case insensitive
find [path] -iname [pattern]

find . -iname "test"

man find:

      -mtime n
             File’s data was last modified n*24 hours ago.  See the comments for -atime to  understand  how
             rounding affects the interpretation of file modification times.

Time modifier:

+n     for greater than n,
-n     for less than n,
n      for exactly n.

Find all files modified within the last 'n' days:

find [path] -mtime [+//-][n]

# to find all files modified today:
find /etc -mtime -1

Find all files modified within the last 'n' minutes:

find [path] -mmin [+//-][n]

# to find all files modified within the last hour:
find /etc -mmin -60

Filter (exclude) certain folders:

find . -type d -not -path "./badpath/*" -not "./badpath2/*" -print

Find files between two dates: [1]

find Your_Mail_Dir/ -newermt "2011-01-01" ! -newermt "2011-12-31"

Remove path from find command output

Use %P with printf:

find . -type d -mtime 14 -printf "%P\n" > deploy.txt

ref: https://serverfault.com/questions/354403/remove-path-from-find-command-output

Indicate directories and files

Print the file type along with the name with -printf "%y %p\n": [1]

$ sudo find . -name 'example.com*' -printf "%y %p\n"
d ./archive/example.com
f ./renewal/example.com.conf
d ./live/example.com

find large files

find files larger than 100M:

# RHEL
find / -type f -size +100M -not -path "/proc/*" -not -path "/sys/*" -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
# Ubuntu
find / -type f -size +100M -not -path "/proc/*" -not -path "/sys/*" -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'

find small files

Find files smaller than 500 bytes:

find ~ -size -500b

Find Executable Files

Files and folders (natural):

find <path> -executable

Files only:

find <path> -executable -type f

Delete files older than n days

Delete files older than 30 days: [2]

find /path/to/files/ -type f -name '*.jpg' -mtime +30 -exec rm {} \;

Files Owned by User

Find by user:

find /var -user vivek

Find by group:

find /home -group ftpusers

keywords