Questions tagged [find]

For questions pertaining to find, a command-line utility to search for files in a directory hierarchy. Use this tag for questions about find itself or questions about issues arising from using the find command-line utility.

find is a utility to search a directory tree for objects (files and/or directories) matching certain criteria (name, type, date, …) and print their names or perform actions on them.

The find command is invoked like

find [options] [starting-point...] [expression]

starting-point is one or more paths (absolute or relative directories). These are the places where find will start to look for files. The default starting point is the current directory .

expression is zero or more of: tests and actions (and some other types).

  • Tests are used to filter the set of files you want to act upon. You can filter on types of files, filename patterns, file attributes, and so on.
  • Actions are things you want to do with the files. The default action is to print the path to the file. You can specify multiple actions. Some actions are:
    • -exec -- send the filenames as arguments to some program,
    • -ls -- list them like ls -l
    • -printf -- print out your choice of attributes about the files
    • -delete -- make sure before you delete your files that you know which files will be found! -print them first.

Common idioms

List all the regular files in the current directory and its subdirectories:

find . -type f

Same, but do not recurse into directories called .svn:

find . -type d -name .svn -prune -o -type f -print

Recode all *.txt files in the current directory and its subdirectories from latin1 to utf8 (two equivalent commands):

find . -type f -name '*.txt' -exec recode latin1..utf8 {} +
find . -type f -name '*.txt' -print0 | xargs -0 recode latin1..utf8

Further reading

External references

3330 questions
6 answers

How to run find -exec?

I'd like to find the files in the current directory that contain the text "chrome". $ find . -exec grep chrome find: missing argument to `-exec' What am I doing wrong?
  • 29,573
  • 43
  • 82
  • 90
4 answers

find's "-exec rm {} \;" vs "-delete"

I'm trying to understand the difference between these two commands: sudo find / -name .DS_Store -delete and sudo find / -name ".DS_Store" -exec rm {} \; I noticed that the -exec ... {} method is preferred. Why? Which one is safer/faster/better?…
  • 4,141
  • 2
  • 13
  • 6
13 answers

How can I find broken symlinks

Is there a way to find all symbolic links that don't point anywere? find ./ -type l will give me all symbolic links, but makes no distinction between links that go somewhere and links that don't. I'm currently doing: find ./ -type l -exec file…
  • 11,192
  • 10
  • 43
  • 57
2 answers

how can I recursively delete empty directories in my home directory?

Possible Duplicate: How to remove all empty directories in a subtree? I create directories very often, scattered over my home directory, and I find it very hard to locate and delete them. I want any alias/function/script to find/locate and delete…
Santosh Kumar
  • 3,453
  • 6
  • 26
  • 35
5 answers

How to use find command to search for multiple extensions

I can get all jpg images by using: find . -name "*.jpg" But how can I add png files to the results as well?
  • 3,303
  • 3
  • 16
  • 8
3 answers

Find command: how to ignore case?

I am looking for file "WSFY321.c" in a huge directory hierarchy. Usually I would use GNU find: find . -name "WSFY321.c" But I do not know the case, it could be uppercase, lowercase, or a mix of both. What is the easiest way to find this file? Is…
Nicolas Raoul
  • 7,555
  • 13
  • 41
  • 54
9 answers

How to delete directories based on `find` output?

I issue the following command to find the .svn directories: find . -name ".svn" That gives me the following results: ./toto/.svn ./toto/titi/.svn ./toto/tata/.svn How could I process all these lines with rm -fr in order to delete the directories…
  • 2,351
  • 2
  • 12
  • 8
1 answer

How to skip "permission denied" errors when running find in Linux?

Possible Duplicate: How do I remove “permission denied” printout statements from the find program? When I run this command in Linux (SuSE): find / -name ant I get many error messages of the form: find: `/etc/cups/ssl': Permission denied Does…
  • 2,399
  • 3
  • 16
  • 12
9 answers

Looping through files with spaces in the names?

I wrote the following script to diff the outputs of two directores with all the same files in them as such: #!/bin/bash for file in `find . -name "*.csv"` do echo "file = $file"; diff $file /some/other/path/$file; read…
Amir Afghani
  • 6,773
  • 11
  • 25
  • 23
8 answers

Why is looping over find's output bad practice?

This question is inspired by Why is using a shell loop to process text considered bad practice ? I see these constructs for file in `find . -type f -name ...`; do smth with ${file}; done and for dir in $(find . -type d -name ...); do smth with…
  • 74,370
  • 29
  • 205
  • 234
8 answers

How to remove all empty directories in a subtree?

How can I remove all empty directories in a subtree? I used something like find . -type d -exec rmdir {} 2>/dev/null \; but I needs to be run multiple times in order to remove directories containing empty directories only. Moreover, it's quite…
  • 4,821
  • 7
  • 29
  • 28
4 answers

Delete files older than X days +

I have found the command to delete files older than 5 days in a folder find /path/to/files* -mtime +5 -exec rm {} \; But how do I also do this for subdirectories in that folder?
  • 2,699
  • 5
  • 22
  • 30
7 answers

How to stop the find command after first match?

Is there a way to force the find command to stop right after finding the first match?
  • 12,094
  • 12
  • 42
  • 56
3 answers

Finding all large files in the root filesystem

I have a linux server, which currently has below space usage: /dev/sda3 20G 15G 4.2G 78% / /dev/sda6 68G 42G 23G 65% /u01 /dev/sda2 30G 7.4G 21G 27% /opt /dev/sda1 99M 19M 76M 20%…
Abhishek dot py
  • 2,273
  • 4
  • 15
  • 13
4 answers

How to combine 2 -name conditions in find?

I would like to search for files that would not match 2 -name conditions. I can do it like so : find /media/d/ -type f -size +50M ! -name "*deb" ! -name "*vmdk" and this will yield proper result but can I join these 2 condition with OR somehow ?
  • 12,536
  • 20
  • 50
  • 58
2 3
99 100