my cheat sheet on find

Home

1 find syntax

find [starting dir] [matching criteria] [actions]

2 criteria

(grouping with escaped parenthesis allowed see examples)

  • -atime [last accessed] ……. exactly <n> days ago
  • -mtime [last modified] ……. less than <-n> days ago
  • ……. more than <+n> days ago
  • -amin same for minutes ago
  • -mmin same for minutes ago
  • -cnewer /etc/installdate File's status was last changed more recently than file /etc/install was (can use any file you want)
  • -size in 512 byte blocks
  • -type [f=file d=dir l=link]
  • -name [\* for wildcard]
  • -usr uid
  • -group gid
  • -perm permissions
  • -o "or" two criteria

3 actions

  • -print
  • -exec cmd
  • -exec cmd -ok user must confirm action
  • -exec cmd {} \; for placeholder of filename
  • -mount(sysV) restrict to fs of starting directory
  • -xdev(bsd) restrict to fs of starting directory
  • -prune (bsd) do NOT descend into subdirectories

——————8<-——————8<-—————

4 examples (assuming sudo is used on all)

  • find /var -size +1000 -mtime +30 -exec ls -lg {} \; to list all unchanged (at least 30 days) files greater than 1/2 meg
  • find / -type f -printf “%s\t%p\n” | sort -n | tail -10 find largest files while skipping the directories
  • find . -name \*.c -print to list all .c files under current directory
  • find / \(-mtime +7 -o -atime +30 \) -print to list both; unmodified files or even longer unaccessed files
  • find / \(-name a.out -o -name core -o -name '.CKP.*' -o -name '.BAK.*' \ -o -name '#*#' \) -type f -atime +14 -exec rm -f {} \; -o fstype nfs -prune

    to remove all files that I probably should remove, excluding any nfs mounted files for obvious reasons.

  • find / -mmin -900 -print print all files modified within the last 15 hours (15*60=900)
  • find / -cnewer /etc/installdate finds all files whose status has changed since the date stamp on the file /etc/installdate
  • find . -xdev -type f -size +100M find files greater than 100 MB in the current directory

    I like this workflow:

  • find . -xdev -type f -size +100M
  • find . -xdev -type f -size +100M -exec ls -la {} \;
  • find . -xdev -type f -size +100M -exec file {} \;
  • du -a /var | sort -n -r | head -n 10

Of course if you are looking for large files you could use du directly.

  • cd into target directory first, then:
  • du -hsx * | sort -rh | head -n 10
  • du -hsx -- * | sort -rh | head -n 10
  • -h human readable, -s summary (total) -x skip directories on different

*filesystems, -r reverse sort order, -h compare human readable numbers

To see what you are deleting while you do so try this: find . -name ,\?\* -exec ls {} \; -exec rm {} \;

================ find . -exec grep -i ripley {} \; -exec ls -l {} \; there are easier ways to recursively search for a string in a directory tree but this is one way to at least list the files. See emacs' recursive grep M-x rgrep in a dired window in some directory.

4.0.1 -delete vs -exec rm {} \;

It seems that -delete is easier to type, but otherwise the same thing.

  • find * -type f -name "*.png" -size -1k -delete # deletes small png files

Don't blindly delete large log files without at least doing a "tail": the contents may alert you to a ongoing system problem.

You might want to look for core files too:

find / -type f -name core -exec ls -l {} \;

or

find / -type f -name core -exec rm {} \;

If you are running a mail server, check its spool directory: undeliverable messages can be backing up.

Although unusual, it is also possible that your filesystem is confused: running fsck (single user mode_ or unmounted of course) can fix that. Because fsck varies greatly between OSes, check your man page and look for options - for example SCO has "-ofull" and "-s" (which reconstructs the freelist even if nothing seems to be wrong). For Mac, you can use fsck or boot from the install CD and use Disk Utility from there.

Remember that if a process has a file open, the space you remove will not appear in the free list until the process closes the file, either of its own choice or through being killed. Use "lsof" to show what process(es) are using a file.

By the way, in the examples below I've tried to use generic examples that should work on any Unix/Linux. However, syntax can be slightly different (for an example see find large files). If you are not familiar with find, sort etc., experiment and read the man pages before doing anything drastic.

Although time consuming, the following procedure can be used to track down where your space has been used.

cd /

du -s *

(Some folks like to use "du -cks *", which is easy to remember as "ducks". For "human readable", add an "h" to the end of that.)

This command will print the number of blocks used by each directory and file in the root. Most likely, the largest number printed is where you should be looking. You can then cd to that directory, and examine it. If it has sub-directories, you might use:

find . -type d -exec du -s {} \;

You can search for "large" files by cd'ing to a suspect directory ( or even starting at /, if you must), and typing:`

find . -size +5000 -print

which will print the names of all files over 5,000 blocks (2,560,000) bytes. This may find many, many files, so you might want to refine it with larger numbers. You might also want to sort it:

find / -size +2000 -exec ls -s {} \; | sort -nr | more

To limit find to the filesystem you are on, add "-mount":

find . -mount -size +5000 -print

If you are using Mac OS X:

mdfind 'kMDItemFSSize > 20000000'

will find files over 20,000,000 bytes.

You may not be looking for a large file per se. A directory that contains a very large number of file can be just as bad. Find those with:

find / -type d -size +5 -print

Again, you'll want to use "lsof" to see what (if anything) might be using that directory.

============================ Use the following syntax to find files owned by users(s) in Linux/Unix: find directory-location -group {group-name} -name {file-name}

Where,

  • directory-location : Locate the file in this directory path.
  • group {group-name} : Find the file belongs to group-name.
  • name {file-name} : The file name or a search pattern

In this example, locate or find all files belongs to a group called “ftpusers” in the /home directory:

To find all *.c file belongs to a group called “ftpusers” in /data/project directory, run:

  • find /data/project -group ftpusers -name "*.c"

OR do case insensitive search:

  • find /data/project -group ftpusers -iname "*.c"

============================

The easy way to copy all files in a directory (/media) that has a name made up of non-viewable (i.e. invisible) characters is to just copy all your files without actaully moving into the folder, which you can't do because you don't see what the name of the folder is: (could be a blank, a back-space, a LF etc..)

find /media -mindepth 2 -not -path '/media/System*' -exec cp -rt /new/dir {} + Let's break that down:

find /media : searc in the directory /media

  • -mindepth 2 : only look for files or directories that are in a subdirectory of /media. This will ensure you skip things in /media itself.
  • -not -path '/media/System*' : exclude any files or directories whose path matches /media/System*. This will make find ignore anything in the /media/System Volume Information directory.
  • -exec cp -rt /new/dir {} : copy anything found to /new/dir.

That's it, your files have been copied and you don't need to cd into the weird name.

*

4.1 Home