Skip to content

Instantly share code, notes, and snippets.

@johncf
Forked from gr1ev0us/find.sh
Last active April 20, 2024 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johncf/56502c5e49282cf8fbcee9e4f79a373f to your computer and use it in GitHub Desktop.
Save johncf/56502c5e49282cf8fbcee9e4f79a373f to your computer and use it in GitHub Desktop.
Cheatsheet for find linux
# Based on: https://alvinalexander.com/unix/edu/examples/find.shtml
# basic 'find file' commands
# --------------------------
find / -name foo.txt -type f -print # find regular files under / with name foo.txt
find / -name foo.txt -type f # -print is the default action, and may be omitted
find . -name foo.txt # search under the current dir, and all types
find . -name "foo*" -type d # wildcard name-matching, and only directories
find /opt /usr /var -name foo.scala -type f # search multiple dirs
find . -iname foo # case-insensitive: foo, Foo, FOo, FOO, etc.
# logical expressions
# -------------------
find . -type f \( -name "*.c" -o -name "*.sh" \) # *.c or (-o) *.sh files
find . -type f -not -name "*.html" # find all files not ending in ".html"
# find files by text in the file (find + grep)
# --------------------------------------------
find . -type f -name "*.java" -exec grep -l Buffer {} \; # find Buffer in all *.java files
find . -type f -name "*.java" -exec grep -l Buffer {} \+ # grep multiple files at once (much faster)
find . -type f -name "*.java" -exec grep -B5 -A10 Buffer {} \+ # print before- and after- context lines
# find files and act on them (find + exec)
# ----------------------------------------
find /usr/local -name "*.html" -type f -exec chmod 644 {} \; # change html files to mode 644
find htdocs cgi-bin -name "*.cgi" -type f -exec chmod 755 {} \; # change cgi files to mode 755
find . -name "*.pl" -exec ls -ld {} \; # run ls command on files found
find . -type f -name "*.mp3" -exec cp {} /tmp/MusicFiles \; # cp *.mp3 files to /tmp/MusicFiles
# find and delete
# ---------------
find . -type f -name "Foo*" -exec rm {} \; # remove all "Foo*" files under current dir
find . -type d -name CVS -exec rm -r {} \; # remove all subdirectories named "CVS" under current dir
# find files by modification time
# -------------------------------
find . -mtime 0 # within 24 hours
find . -mtime +30 # more than 30 days
find . -mtime -7 -type f # last 7 days, regular files
# find files by modification time relative to another file
# --------------------------------------------------------
touch -t 01281245 poop # 1) create a file with timestamp [YY]MMDDhhmm
find . -mnewer poop # 2) returns a list of newer files
# find with time: this works on mac os x
# --------------------------------------
find . -newermt "1 minute ago" # string must be parse-able by `date --date="$string"`
# find and tar
# ------------
find . -type f -name "*.java" | xargs tar cvf myfile.tar
find . -type f -name "*.java" | xargs tar rvf myfile.tar
# (see http://alvinalexander.com/blog/post/linux-unix/using-find-xargs-tar-create-huge-archive-cygwin-linux-unix for more information)
# find, tar, and xargs
# --------------------
find . -name -type f '*.mp3' -mtime -180 -print0 | xargs -0 tar rvf music.tar
# (`find -print0` and `xargs -0` helps handle spaces correctly in filenames)
# (see http://alvinalexander.com/mac-os-x/mac-backup-filename-directories-spaces-find-tar-xargs)
# exclude files from other devices
find -xdev . -type f -name "*.java"
# finding large files
find / -type f -size +100M -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment