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

learn more… | top users | synonyms

8
votes
2answers
2k 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 getting an archive of files from a windows user. This archive ...
8
votes
3answers
5k views

find -exec in bash script with variable expansion

I'm trying to run a command similar to the one below in a bash script. It should search through all subfolders of $sourcedir and copy all files of a certain type to the root level of $targetdir. ...
7
votes
2answers
576 views

Why “ls” doesnt show the file that “find” discovered ?

(New to unix) In ubuntu When I am up in "/" folder i type sudo find . -name .erlang.cookie and the result is ./var/lib/rabbitmq/.erlang.cookie then when I am on the folder - /var/lib/rabbitmq ...
7
votes
4answers
542 views

How can I find a file whose name includes a given string, such as “abcde”?

Within a set of directories, how do I find a file whose name includes a given string, such as "abcde"?
7
votes
4answers
8k views

How to find a file in the filesystem from the command line?

I'm fairly new to Linux, and I've now found myself in a situation where I'd like to find where a file (with a partially-known filename) is in the file system. I'd like to know how to do this from the ...
7
votes
4answers
20k 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?
7
votes
7answers
1k 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. ...
7
votes
2answers
8k 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 ...
7
votes
3answers
1k views

Make find show slash after directories?

How can I make the find command show a slash after directories? For example, I want dir to show up as dir/ instead of dir. I'm using find . -print
7
votes
2answers
1k views

Find files that a user can not read?

I want to find files that a particular user will not be able to read. Assume the username is "user123" and they are in a group called "user123". I want to find files that, if they are owned by ...
7
votes
2answers
3k views

How can you move (or copy) all files to a directory with the same filename prefix?

Using Bash So let's say I have a bunch of files randomly placed in a parent directory ~/src, I want to grab all the files matching a certain suffix and move (or copy) them to a ~/dist directory. ...
7
votes
2answers
925 views

sync files recursively between two folders where files are less than 24 hours old

I want to find all the files under a directory which are within 24 hours then rsync those files with another server. There are some old files that I do not want to transfer, however if those old ...
7
votes
1answer
2k views

List of modified files of the past 24 hours

Is there a reliable way to have a system that mails me the list of files that were modified in the past 24 hours inside a set of directories? (like /home/*/public_html/* and /home/*/*domains/*)
7
votes
1answer
6k views

Solaris equivalent for depth/prune?

I am trying to find some files inside a directory. By default, the find command is searching the directory which I have specified and it's sub directories. I tried to use depth/maxdepth and prune, ...
7
votes
2answers
2k views

How to detect whether “find” found any matches?

Is there an idiomatic means to detect whether "find" found any matches? I'm currently using COUNT=`find ... | wc -l` if [ "$COUNT" -gt 0 ]; then but this seems a little indirect to me. Also, I'd ...
7
votes
1answer
360 views

Why do some regex commands have opposite intepretations of '\' with various characters?

Take, for example, this command: find . -regex ".*\.\(cpp\|h\)" This will find all the .h and .cpp files in your directory. The period character '.' in regular expressions usually means "any ...
7
votes
1answer
997 views

Got less output with print0 option on find?

I have read the man page and other references, but I am still confused by the behavior of find with -print0 option. -print0 This primary always evaluates to true. It prints the pathname of ...
7
votes
4answers
2k views

Find files with same name but different content?

I want to generate a list of files that have: Same name Different content in a directory (including all children directories and content). How to do? Bash, perl, anything is fine. So, two ...
7
votes
2answers
5k views

How do I stop a find from descending into found directories?

I'm wanting to find all directories with a specific string so I can do another find on the files contained within. So I don't want to waste time on ./my-search-term/dir/my-search-term etc. How can I ...
7
votes
5answers
15k views

How copy and rename files found in “find” function Linux?

I have a folder named /home/user/temps which has 487 folders. In each folder I have a file called thumb.png. I want to copy all files named thumb.png to a separate folder and rename them based on ...
7
votes
3answers
3k views

How to open multiple files from `find` output?

I know I can open multiple files found in a dir like so: find -name *.foo -xargs <command> {} \; This works, but when trying to open a bunch of textfiles in gedit at the same time, it opens ...
7
votes
2answers
261 views

Search MP3/Ogg files by tags/parameters from the command line

Is there any utility to search MP3/Ogg files by tags (or other characteristics) from the command line? (e.g. finding all audio files longer than 10 minutes with genre set to 'Rock'). I found this, ...
7
votes
2answers
67 views

break out of find if an -exec fails

Is there a way to write an find so that it breaks if one of the -exec operations fails on a file? E.g. (javac is conveniently used as a program that can return an exit code of 1 on some files and for ...
7
votes
3answers
1k views

Is it possible to nest a 'find -exec' within another 'find -exec'?

Something like the following is what I what I'm after, but my code doesn't work, no matter how I escape {} and + ; find ./ -maxdepth 1 -type d -name '.*' -exec \ find {} -maxdepth 1 -type f ...
6
votes
4answers
307 views

How to only zip the files of a specific user?

I like zip, it's simple, but recently I came with the need of only zipping the file owned by one user in a directory. I read the manual and I can't find any option to for example do things like: ...
6
votes
6answers
293 views

Searching in many files

Is there some sophisticated technique to search strings in many files? I tried this basic technique for i in `find .`; do grep searched_string "$i"; done; It doesn't look much sophisticated and ...
6
votes
7answers
15k views

How do I find text within a file and have it search multiple subfolders?

I'm looking for a function name and the folder structure is deep and there are a lot of files to look though. Usually I go with something like find * | grep functionname but is that the best way?
6
votes
5answers
1k views

Script to remove spaces and lowercase in file names

I am trying to write a script that will replace spaces with "-" and make all letters lower case for all files in the current directory. for x in 'ls' do if [ ! -f $x ]; then ...
6
votes
3answers
12k 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 ...
6
votes
5answers
4k views

How to copy all html files from a directory tree to a single directory?

I want to copy all the .html files in myDir and its subdirectories to ~/otherDir. Here's what I tried (it doesn't work): $ find myDir -name *.html -print | xargs -0 cp ~/otherDir usage: cp [-R [-H | ...
6
votes
5answers
1k views

How to clean up file extensions?

I have a directories with .MP3 files which I'd like to change the extensions to .mp3. What's the easiest way to do this? I'm think something along the lines of: find /RootPath -type f -iname "*.mp3" ...
6
votes
4answers
383 views

How to use find to remove *.py~ and *.pyc files at once?

I want to cleanup a directory (and subdirectories) from each occurrance of the .py and .pyc files. I tried the following find . -name '*pyc' -o -name '*py~' -exec rm -f {} \; which left out all pyc ...
6
votes
4answers
4k 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. I am ...
6
votes
6answers
11k views

How to search for a word in entire content of a directory in linux

need to search for something in entire content I am trying: find . | xargs grep word I get error: xargs: unterminated quote How to achieve this?
6
votes
2answers
4k 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 ...
6
votes
3answers
113 views

Excluding . and .. from results in find and ls

Is there any way to prevent find and ls -l from listing . and .. in their results? I never care about seeing this result in the output, and it prevents me from effectively piping their output to wc -l ...
6
votes
3answers
8k views

Pipe find into grep -v

I'm trying to find all files that are of a certain type and do not contain a certain string. I am trying to go about it by piping find to grep -v example: find -type f -name '*.java' | xargs grep -v ...
6
votes
3answers
7k 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 {} \; ...
6
votes
4answers
852 views

How to find a file in Linux with its path?

I am trying to find settings.xml file in my Ubuntu machine. I have no clue where it is, and which directory it is in. I tried using this - ls -R | grep settings.xml But it doesn't show me the ...
6
votes
2answers
210 views

Delete files specified by the output of find: rm thinks argument list is too long.

When I run the following command, it gives me an error: $ rm -f `find /home/domain/imap/domain.com/*/Maildir -mtime +190 | grep -E '/cur/|/new/'` -bash: /bin/rm: Argument list too long How can I ...
6
votes
5answers
1k views

Find images by size: find / file / awk

I've been trying to find png image files a certain height (over 500px). I know that file will return image dimensions. Example: $ file TestImg1a.png TestImg1a.png: PNG image data, 764 x 200, ...
6
votes
4answers
5k views

Delete files of certain size range

I want to delete files which size is between certain values. For example I have the following list of files: -rw-r--r-- 1 smsc sys 558 Apr 30 13:07 stats.sfe.1.20120430130513.xml.Z -rw-r--r-- 1 smsc ...
6
votes
4answers
96 views

Find newest file. Multiple Filetype restrictions

I want to write a command that gives me the newest file in a directory recursively. But that's not my only limitation. The files has to be an mp3 or a jpg file. (case insensitive prefered) I only need ...
6
votes
1answer
123 views

How does the order of arguments influence the speed of find

How does the order of arguments in a find call affect the speed of the results? Compare for example (A) find -name dir -type d and (B) find -type d -name dir Or any other of combinations of ...
6
votes
1answer
2k views

list files by symlink target

How do I get list of files (and then do something with that list) by filtering through the symbolic link target name, and not the symlink name? For example from the following list I'm only interested ...
6
votes
2answers
390 views

Remove all Vim undo files in all but one directory

I just realized that I have tons of Vim undo (.un~) files sprinkled around my file system. I'd like to delete all of these files except in one directory—~/.tmp. My first problem is that I can't seem ...
6
votes
3answers
12k views

Getting all files that have been modified on a specific date

Is it possible to find all php files within a certain directory that have been modified on a certain date I'm using find /var/www/html/dir/ -mtime -28 | grep '\.php' to get files modified within ...
6
votes
3answers
2k views

Is there a way to find a file in an inverse recursive search?

So say I'm in a directory that is deep within a file structure, such as /home/cory/projects/foo/bar/bash/baz I know that in either the current folder baz or one of the folders above me (such as ...
6
votes
1answer
161 views

find is missing a result — how is that possible?

What conditions explain this output: root@ip:/# find / -name "server.xml" -print /etc/tomcat7/server.xml root@ip:/# ls /var/lib/tomcat7/conf/server.xml /var/lib/tomcat7/conf/server.xml I am ...
6
votes
3answers
751 views

Deleting lots of files

I accidentally created 8 million files and every time I'm trying to delete them the server almost dies because of the rm process eating all disk IO (the server is remote without console). Should ...