How to only find files in a given directory, and ignore subdirectories using bash
I looked at other similar questions, but didn’t find one that would enable me to grasp the concept and make it applicable to my situation based on my limited time. I’m simply running the find command to find certain files, but some files in sub-directories have the same name which I want to ignore. Thanks for any help. Below is the command that I’m using:
The files/pattern I’m interested in: /dev/abc-scanner, /dev/abc-cash .
What’s being returned:
I want to ignore the latter files: /dev/.udev/.
4 Answers 4
If you just want to limit the find to the first level you can do:
. or if you particularly want to exclude the .udev directory, you can do:
Is there any particular reason that you need to use find ? You can just use ls to find files that match a pattern in a directory.
If you do need to use find , you can use the -maxdepth 1 switch to only apply to the specified directory.
This may do what you want:
Does not work for me. It return nothing. If I just do ‘.’ it gives me all the files in directory below the one I’m working in on.
Return nothing with ‘.’ instead I get list of all ‘big’ files in my directory as well as the rootfiles/ directory where I store old ones.
Continuing. This works.
Apparently /dev means directory of interest. But ./ is needed, not just . . The need for the / was not obvious even after I figured out what /dev meant more or less.
I couldn’t respond as a comment because I have no ‘reputation’.
How to only get file name with Linux ‘find’?
I’m using find to all files in directory, so I get a list of paths. However, I need only file names. i.e. I get ./dir1/dir2/file.txt and I want to get file.txt
10 Answers 10
In GNU find you can use -printf parameter for that, e.g.:
If your find doesn’t have a -printf option you can also use basename:
Use -execdir which automatically holds the current file in <> , for example:
You can also use $PWD instead of . (on some systems it won’t produce an extra dot in the front).
If you still got an extra dot, alternatively you can run:
The -execdir primary is identical to the -exec primary with the exception that utility will be executed from the directory that holds the current file.
When used + instead of ; , then <> is replaced with as many pathnames as possible for each invocation of utility. In other words, it’ll print all filenames in one line.
If you are using GNU find
Or you can use a programming language such as Ruby(1.9+)
If you fancy a bash (at least 4) solution
If you want to run some action against the filename only, using basename can be tough.
will just echo basename /my/found/path . Not what we want if we want to execute on the filename.
But you can then xargs the output. for example to kill the files in a dir based on names in another dir:
/clang+llvm-3.3/bin/ -type f -exec basename <> \; – commonpike Apr 25 ’18 at 11:09
On mac (BSD find ) use:
-exec and -execdir are slow, xargs is king.
xargs ‘s parallelism also helps.
Funnily enough i cannot explain the last case of xargs without -n1 . It gives the correct result and it’s the fastest ¯\_(ツ)_/¯
( basename takes only 1 path argument but xargs will send them all (actually 5000) without -n1 . does not work on linux and openbsd, only macOS. )
Some bigger numbers from a linux system to see how -execdir helps, but still much slower than a parallel xargs :
Honestly basename and dirname solutions are easier, but you can also check this out :
As others have pointed out, you can combine find and basename , but by default the basename program will only operate on one path at a time, so the executable will have to be launched once for each path (using either find . -exec or find . | xargs -n 1 ), which may potentially be slow.
If you use the -a option on basename , then it can accept multiple filenames in a single invocation, which means that you can then use xargs without the -n 1 , to group the paths together into a far smaller number of invocations of basename , which should be more efficient.
Here I’ve included the -print0 and -0 (which should be used together), in order to cope with any whitespace inside the names of files and directories.
Here is a timing comparison, between the xargs basename -a and xargs -n1 basename versions. (For sake of a like-with-like comparison, the timings reported here are after an initial dummy run, so that they are both done after the file metadata has already been copied to I/O cache.) I have piped the output to cksum in both cases, just to demonstrate that the output is independent of the method used.
As you can see, it really is substantially faster to avoid launching basename every time.
Linux command: How to ‘find’ only text files?
After a few searches from Google, what I come up with is:
which is very unhandy and outputs unneeded texts such as mime type information. Any better solutions? I have lots of images and other binary files in the same folder with a lot of text files that I need to search through.
16 Answers 16
I know this is an old thread, but I stumbled across it and thought I’d share my method which I have found to be a very fast way to use find to find only non-binary files:
The -I option to grep tells it to immediately ignore binary files and the . option along with the -q will make it immediately match text files so it goes very fast. You can change the -print to a -print0 for piping into an xargs -0 or something if you are concerned about spaces (thanks for the tip, @lucas.werkmeister!)
Also the first dot is only necessary for certain BSD versions of find such as on OS X, but it doesn’t hurt anything just having it there all the time if you want to put this in an alias or something.
EDIT: As @ruslan correctly pointed out, the -and can be omitted since it is implied.
grep -rIl «needle text» my_folder
Why is it unhandy? If you need to use it often, and don’t want to type it every time just define a bash function for it:
put it in your .bashrc and then just run:
EDIT to reflect OP’s edit:
if you want to cut out mime informations you could just add a further stage to the pipeline that filters out mime informations. This should do the trick, by taking only what comes before : : cut -d’:’ -f1 :
This is unfortunately not space save. Putting this into bash script makes it a bit easier.
This is space safe:
Another way of doing this:
If you want empty files too:
If you want the filenames without the file types, just add a final sed filter.
You can filter-out unneeded file types by adding more -e ‘type’ options to the last grep command.
If your xargs version supports the -d option, the commands above become simpler:
Here’s how I’ve done it .
1 . make a small script to test if a file is plain text istext:
2 . use find as before
«text» ]]` instead. – user unknown Mar 17 ’12 at 16:08
I have two issues with histumness’ answer:
It only list text files. It does not actually search them as requested. To actually search, use
It spawns a grep process for every file, which is very slow. A better solution is then
This only takes 0.2s compared to 4s for solution above (2.5GB data / 7700 files), i.e. 20x faster.
Also, nobody cited ag, the Silver Searcher or ack-grep¸as alternatives. If one of these are available, they are much better alternatives:
As a last note, beware of false positives (binary files taken as text files). I already had false positive using either grep/ag/ack, so better list the matched files first before editing the files.
Although it is an old question, I think this info bellow will add to the quality of the answers here.
When ignoring files with the executable bit set, I just use this command:
To keep it from recursively enter into other directories:
No need for pipes to mix lots of commands, just the powerful plain find command.
- Disclaimer: it is not exactly what OP asked, because it doesn’t check if the file is binary or not. It will, for example, filter out bash script files, that are text themselves but have the executable bit set.
That said, I hope this is useful to anyone.
I do it this way: 1) since there’re too many files (
30k) to search thru, I generate the text file list daily for use via crontab using below command:
2) create a function in .bashrc:
Then I can use below command to do the search:
if your filenames are weird look up using the -0 options:
grep eth0 $(find /etc/ -type f -exec file <> \; | egrep -i «text|ascii» | cut -d ‘:’ -f1)
Here’s a simplified version with extended explanation for beginners like me who are trying to learn how to put more than one command in one line.
If you were to write out the problem in steps, it would look like this:
To achieve this, we can use three UNIX commands: find , file , and grep .
find will check every file in the directory.
file will give us the filetype. In our case, we’re looking for a return of ‘ASCII text’
grep will look for the keyword ‘ASCII’ in the output from file
So how can we string these together in a single line? There are multiple ways to do it, but I find that doing it in order of our pseudo-code makes the most sense (especially to a beginner like me).
find ./ -exec file <> «;» | grep ‘ASCII’
Looks complicated, but not bad when we break it down:
find ./ = look through every file in this directory. The find command prints out the filename of any file that matches the ‘expression’, or whatever comes after the path, which in our case is the current directory or ./
The most important thing to understand is that everything after that first bit is going to be evaluated as either True or False. If True, the file name will get printed out. If not, then the command moves on.
-exec = this flag is an option within the find command that allows us to use the result of some other command as the search expression. It’s like calling a function within a function.
file <> = the command being called inside of find . The file command returns a string that tells you the filetype of a file. Regularly, it would look like this: file mytextfile.txt . In our case, we want it to use whatever file is being looked at by the find command, so we put in the curly braces <> to act as an empty variable, or parameter. In other words, we’re just asking for the system to output a string for every file in the directory.
«;» = this is required by find and is the punctuation mark at the end of our -exec command. See the manual for ‘find’ for more explanation if you need it by running man find .
| grep ‘ASCII’ = | is a pipe. Pipe take the output of whatever is on the left and uses it as input to whatever is on the right. It takes the output of the find command (a string that is the filetype of a single file) and tests it to see if it contains the string ‘ASCII’ . If it does, it returns true.
NOW, the expression to the right of find ./ will return true when the grep command returns true. Voila.