Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

Questions tagged [find]

find is a command line utility to search for files in a directory hierarchy

415
votes
6answers
659k views

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?
251
votes
9answers
146k views

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. ...
236
votes
3answers
294k views

find - 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 execmethod is preferred....
163
votes
6answers
17k views

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 ...
159
votes
3answers
152k views

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. ...
141
votes
6answers
73k views

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 ...
131
votes
4answers
377k views

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?
131
votes
10answers
164k views

Looping through files with spaces in the names? [duplicate]

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 $...
127
votes
7answers
186k views

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 ...
123
votes
2answers
123k views

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

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 ...
122
votes
4answers
50k views

locate vs find: usage, pros and cons of each other

In Linux and Unix systems there are two common search commands: locate and find. What are the pros and cons of each? When one have benefits over the other?
116
votes
3answers
348k views

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%...
114
votes
4answers
65k views

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?
108
votes
5answers
119k views

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?
107
votes
7answers
436k views

Why does find -mtime +1 only return files older than 2 days?

I'm struggling to wrap my mind around why the find interprets file modification times the way it does. Specifically, I don't understand why the -mtime +1 doesn't show files less than 48 hours old. ...
95
votes
1answer
141k views

How to skip “permission denied” errors when running find in Linux? [duplicate]

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 ...
93
votes
2answers
92k views

How do I pass a list of files to grep

I am using find and getting a list of files I want to grep through. How do I pipe that list to grep?
83
votes
4answers
88k views

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 ...
70
votes
9answers
110k views

Sorting files according to size recursively

I need to find the largest files in a folder. How do I scan a folder recursively and sort the contents by size? I have tried using ls -R -S, but this lists the directories as well. I also tried ...
67
votes
2answers
105k views

Find files with certain extensions

How can I use find to find all files that have a .xls or .csv extension? I have seen a -regex option but I don't know how to use it.
67
votes
4answers
103k views

Sorting the output of “find”?

I need to be able to alphabetically sort the output of find before piping it to a command. Entering | sort | between didn't work, so what could I do? find folder1 folder2 -name "*.txt" -print0 | ...
66
votes
2answers
66k views

How can I use `find` and sort the results by mtime? [duplicate]

I want to use find but sort the results reverse chronologically as with ls -ltr. Is this possible through any combo of flags or pipelines?
66
votes
4answers
225k views

Recursive grep vs find / -type f -exec grep {} \; Which is more efficient/faster?

Which is more efficient for finding which files in an entire filesystem contain a string: recursive grep or find with grep in an exec statement? I assume find would be more efficient because you can ...
65
votes
2answers
59k views

`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 -...
59
votes
3answers
95k views

Why is my find not recursive?

I am running the following command, but it is not performed recursively: find . -name *.java I know there are java files further down in the current directory but it is performing the find on the ...
56
votes
4answers
180k views

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 ...
48
votes
6answers
113k views

How to integrate mv command after find command?

I am searching for files which name which contain AAA within their path using following command: find path_A -name "*AAA*" Given the output showed by the above command, I want to move those files ...
47
votes
5answers
54k views

How can I run a specific command for each find result?

How would one run a specific command for each file that was found by using the find command? For the purpose of the question lets say that I would simply like to delete each file found by find.
47
votes
1answer
5k views

Why is GNU find so fast in comparison with graphical file search utilities?

I'm trying to find a file that doesn't exist in my home directory and all subdirectories. find ~/ -name "bogus" gives me that information after few seconds, yet KDE's dolphin file manager needed ...
45
votes
2answers
89k views

Bash: How to read one line at a time from output of a command?

I am trying to read the output of a command in bash using a while loop. while read -r line do echo "$line" done <<< $(find . -type f) The output I got ranveer@ranveer:~/tmp$ bash test....
43
votes
5answers
43k views

Getting size with du of files only

How can I get the size of all files and all files in its subdirectories using the du command. I am trying the following command to get the size of all files (and files in subdirectories) find . -...
42
votes
6answers
141k views

How can I list subdirectories recursively?

The obvious ls -dR does not work. I am currently using find /path/ -type d -ls but the output is not what I need (plain listing of sub-folders) Is there a way out?
41
votes
8answers
17k views

find searching in parent directories instead of subdirectories

I'm nested deep in a file tree, and I'd like to find which parent directory contains a file. E.g. I'm in a set of nested Git repositories and want to find the .git directory controlling the files I'm ...
41
votes
7answers
66k views

Find files in multiple folder names

I am trying to list all the files from dir1, dir2, dir3 and dir4 which might be anywhere in as a sub directory of my cwd using the find command. I tried the following with no success: find . -type f -...
41
votes
3answers
68k views

How to find all binary executables recursively within a directory?

If I use find command like this: find /mydir/mysubdir -executable -type f all executable files are listed (excluding directories), and including executable script file (like script.sh, etc). What I ...
40
votes
3answers
58k views

How to return both file name and line number with find … -exec grep?

When using find, how do I return the file name and the line number when searching for a string? I manage to return the file name in one command and the line numbers with another one, but I can't seem ...
40
votes
1answer
34k views

find files without extension

Let say I have: foo.txt bar baz.ooo If I use ls -1 !(*.*) then I'll get only bar on the output. Great, now I wish to have same results with find - some find -regex that will do the job. NOTE: find ...
39
votes
1answer
18k views

Understanding the -exec option of `find`

I find myself constantly looking up the syntax of find . -name "FILENAME" -exec rm {} \; mainly because I don't see how exactly the -exec part works. What is the meaning of the braces, the ...
38
votes
9answers
30k views

Finding all “Non-Binary” files

Is it possible to use the find command to find all the "non-binary" files in a directory? Here's the problem I'm trying to solve. I've received an archive of files from a windows user. This archive ...
37
votes
6answers
47k views

Get list of subdirectories which contain a file whose name contains a string

How can I get a list of the subdirectories which contain a file whose name matches a particular pattern? More specifically, I am looking for directories which contain a file with the letter 'f' ...
37
votes
5answers
63k views

How can I find all files that do NOT contain a text string?

What concise command can I use to find all files that do NOT contain a text string? I tried this (using -v to invert grep's parameters) with no luck: find . -exec grep -v -l shared.php {} \; ...
37
votes
2answers
132k views

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 ...
33
votes
6answers
113k views

Move all files with a certain extension from multiple subdirectories into one directory

I have a bunch of .zip files in several directories: Fol1/Fol2 Fol3 Fol4/Fol5 How would I do move them all to a common base folder?
33
votes
3answers
3k views

GNU find and masking the {} for some shells - which?

The man page for GNU find states: -exec command ; [...] The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, ...
32
votes
3answers
27k views

How can I use two bash commands in -exec of find command?

Is it possible to use 2 commands in the -exec part of find command ? I've tried something like: find . -name "*" -exec chgrp -v new_group {} ; chmod -v 770 {} \; and I get: find: missing ...
31
votes
4answers
15k views

find -exec + vs find | xargs: which one to choose?

I understand that the -exec can take a + option to mimic the behaviour of xargs. Is there any situation where you'd prefer one form over the other? I personally tend to prefer the first form, if ...
31
votes
3answers
8k views

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:...
31
votes
3answers
49k views

Piping commands after a piped xargs

HP-UX ***** B.11.23 U ia64 **** unlimited-user license find . -type d -name *log* | xargs ls -la gives me the directory names (the ones which contain log in the directory name) followed by all files ...
31
votes
9answers
12k views

Open a file given by the result of a command in vim

I find myself doing the following almost every day Run a find (find -name somefile.txt) Open the result in vim The problem is I have to copy and paste the result of the find into the vim command. ...
31
votes
2answers
1k views

find(1): how is the star wildcard implemented for it to fail on some filenames?

In a file system where filenames are in UTF-8, I have a file with a faulty name; it is displayed as: D�sinstaller, actual name according to zsh: D$'\351'sinstaller, Latin1 for Désinstaller, itself a ...