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

3524 questions
785
votes
6 answers

Problem running find: missing argument to `-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?
ripper234
  • 31,763
453
votes
4 answers

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

I'm trying to understand the difference between these two commands: find / -name .DS_Store -delete and find / -name ".DS_Store" -exec rm {} \; I noticed that the -exec ... {} method is preferred. Why? Which one is safer/faster/better? I've used…
Onion
  • 4,641
281
votes
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…
213
votes
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?
Vombat
  • 12,884
130
votes
2 answers

`find` with multiple `-name` and `-exec` executes only the last matches of `-name`

When I'm using find . -type f -name "*.htm*" -o -name "*.js*" -o -name "*.txt" it finds all the types of file. But when I add -exec at the end: find . -type f -name "*.htm*" -o -name "*.js*" -o -name "*.txt" -exec sh -c 'echo "$0"' {} \; it seems…
jakub.g
  • 3,263
  • 5
  • 21
  • 18
118
votes
7 answers

How can I grep the results of FIND using -EXEC and still output to a file?

Better to explain on examples. I can: find . -name "*.py" -type f > output.txt But how can I store the output to the same file for: find . -name "*.py" -type f -exec grep "something" {} \ I can't just do find . -name "*.py" -type f -exec grep…
bakytn
  • 2,951
78
votes
5 answers

How to find and delete files older than specific days in unix?

I have got one folder for log with 7 folders in it. Those seven folders too have subfolders in them and those subfolders have subfolders too. I want to delete all the files older than 15 days in all folders including subfolders without touching…
gtaware
  • 891
64
votes
3 answers

How do I remove "permission denied" printout statements from the find program?

Code find / -name netcdf Output find: `/root/.dbus': Permission denied find: `/root/.gconf': Permission denied find: `/root/.gconfd': Permission denied find: `/root/.gnome': Permission denied find: `/root/.gnome2': Permission denied find:…
InquilineKea
  • 6,262
59
votes
1 answer

Finding files by their owner and file permissions

I'm trying to find files which are owned and have the primary group of root. Is there a parameter available to search for files like this? It's critical that all files in a certain directory not be owned by root, so I'd like to check periodically to…
Naftuli Kay
  • 39,676
56
votes
4 answers

"find" output relative to directory

I'd like to use find to list all files and directories recursively in a given root for a cpio operation. However, I don't want the root directory itself to appear in the paths. For example, I currently get: $ find…
nneonneo
  • 1,088
52
votes
1 answer

Explanation of % directives in find -printf

find /tmp -printf '%s %p\n' |sort -n -r | head This command is working fine but what are the %s %p options used here? Are there any other options that can be used?
50
votes
6 answers

Removing leading dots from find command output when used with -exec echo {} option

find . -type f -exec echo {} \; Using the above command, I want to get file names without leading "./" characters. So basically, I want to get: filename Instead of: ./filename Any way to do this?
Diplomat
  • 503
48
votes
11 answers

How do I use find when the filename contains spaces?

I want to pipe file names to other programs, but they all choke when the names contain spaces. Let's say I have a file called. foo bar How can I get find to return the correct name? Obviously I want: foo\ bar or: "foo bar" EDIT: I don't want to…
bug
  • 2,518
48
votes
1 answer

What is the difference when using FIND between -name and -iname

Well the question says it all, I have seen both find -name and find -iname used all over the place without a discernible pattern. Could somebody explain the differences, perhaps with an example to clarify?
47
votes
4 answers

Is it possible to exclude a directory from the find command?

I am using find -type f command to recursively find all files from a certain starting directory. However, I would like to have some directories prevented from entering and extracting names of files inside. So basically I am looking for something…
user219860
  • 471
  • 1
  • 4
  • 3
1
2 3
24 25