All notes


Basic examples


# Find in all CC files the dcm string.
find . -name "*.cc" -exec grep -Hnri dcmLongSCUStorageSOPClassUIDs {} \;

find . -print0 -name "*.cc" | xargs -0 grep -in "patch_add"

# Find all directories in current directory. -maxdepth should precede -type.
find . -maxdepth 1 -type d

# grep -H will also print the matched file name.
# grep -n prints the line number.
# Not Good one.
# Directly using grep can process only files in current directory.
# If the current directory has no CC, error is thrown.
grep -Hnri dcmLongSCUStorageSOPClassUIDs *.cc
# Use this instead.
# Search recursively in current directory, for only CC files.
# Reference.
grep -Hnri --include='*.cc' dcmLongSCUStorageSOPClassUIDs .

-exec command ;

Execute command;
True if 0 status is returned.
All following arguments to find are taken to be arguments to the command until an argument consisting of ';' or '+' is encountered. The two chars should be escaped.
The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a `\') or quoted to protect them from expansion by the shell.

find . -name "*" -exec grep -ri dimonooutputpixeltemplate {} +; | tee > res.txt
find . -type d -print # Find all directories.
find ~/ -daystart -type f -mtime 1 # list the regular files in your home directory that were modified yesterday (0, 24h].
ls -l | grep -vi "aug 18" # Find all files/dirs not with mtime on Aug 18th.
stat -c '%x %z' filename.txt # Show status of atime and ctime of filename.txt.
stat -c '%x, %y, %z, %n' * # %x:atime, %y:mtime, %z:ctime, %n:filename.
ls -l | wc -l # Count line number.

Advanced examples find command.

#---------- Time

# -cmin n: File's status was last changed n minutes ago.

#----- mtime
# -mtime n: File's data was last modified n*24 hours ago.

# Search for files modified in the last twenty-four hours.
find $HOME -mtime 0

# Find all clam logs modified more than 280 days ago. Suppose it's "20171011".
find /var/log/clamav/ -type f -mtime +280
# /var/log/clamav/freshclam.log-20161226.xz
# /var/log/clamav/freshclam.log-20170101.xz
# /var/log/clamav/clamav.log-20161226.xz
# /var/log/clamav/clamav.log-20170101.xz

# Find all clam logs modified less than 2 days ago:
find /var/log/clamav/ -type f -mtime -2
# /var/log/clamav/clamav.log
# /var/log/clamav/freshclam.log

# Find all files that were modified between 50 to 100 days ago.
find / -mtime +50 –mtime -100
# +n for greater than n. Therefore -ctime +1 means the file status must have changed at least 48 hours ago.

# Find files modified within the last 1 hour.
find /home/bob -cmin -60

#---------- Size

# find all the files which are greater than 50MB and less than 100MB.
find / -size +50M -size -100M

# Find largest files
find . -type f -exec ls -s {} \; | sort -n -r | head -5
# Similary when sorted in ascending order, it would show the smallest files first
find . -type f -exec ls -s {} \; | sort -n | head -5

#---------- Empty

find /tmp -type f -empty
# To file all empty directories
find ~/ -type d -empty

#---------- Permission

find . -type f -perm 0664
# Inversion can also be applied to permission checking.
find . -type f ! -perm 0777

# sgid bit set.
find / -perm 2644
# Similarly use 1664 for sticky bit.
# Or use alternative syntax instead of octal numbers:
find / -maxdepth 2 -perm /u=s

# Find all Read Only files
find /etc -maxdepth 1 -perm /u=r

# Find executable files
find /bin -maxdepth 2 -perm /a=x

#---------- User / group

find . -user bob
find /var/www -group developer

ctime, mtime, atime, cmin

access time – atime. Read by one of the Unix processes.
change time – ctime. Changes when you change file's ownership or access permissions, or the file had its contents updated.
modify time – mtime. Used for tracking the actual changes to data of the file itself. It does not change with owner or permission changes.
Most of the times ctime and mtime will be the same, unless only the file attributes are updated. In that case only the ctime gets updated.

# Shows mtime by default.
ls -l

# Shows atime:
ls -lu

# Shows ctime:
ls -lc

# Show all the three times:
stat /tmp/file1


Suppress "Permission denied"


## redirect error spam to /dev/null ##
find . -iname "data*.txt" -print 2>/dev/null