I just want to get the files from the current dir and only output .mp4 .mp3 .exe files nothing else. So I thought I could just do this:
ls | grep \.mp4$ | grep \.mp3$ | grep \.exe$
But no, as the first grep will output just mp4's therefor the other 2 grep's won't be used.
Any ideas? PS, Running this script on Slow Leopard.
shopt -s nullglob
and then just refer to *.exe *.mp3 *.mp4
. See mywiki.wooledge.org/ParsingLs
Why not:
ls *.{mp3,exe,mp4}
I'm not sure where I learned it - but I've been using this.
egrep
-- extended grep -- will help here
ls | egrep '\.mp4$|\.mp3$|\.exe$'
should do the job.
ls
to a terminal (or with -C
option) produces multi-column output. ls
to a pipe (or with -1
) has single column output. (Compare output of ls
with ls | cat
).
Use regular expressions with find
:
find . -iregex '.*\.\(mp3\|mp4\|exe\)' -printf '%f\n'
If you're piping the filenames:
find . -iregex '.*\.\(mp3\|mp4\|exe\)' -printf '%f\0' | xargs -0 dosomething
This protects filenames that contain spaces or newlines.
OS X find
only supports alternation when the -E
(enhanced) option is used.
find -E . -regex '.*\.(mp3|mp4|exe)'
find . -iregex '.*\(mp3\|mp4\|exe\)'
mysupermp3.jpg
consider adding the $
at the end of the regex and the \\.
before the extensions
the easiest way is to just use ls
ls *.mp4 *.mp3 *.exe
ls
doesn't have some sort of silent option.
No need for grep. Shell wildcards will do the trick.
ls *.mp4 *.mp3 *.exe
If you have run
shopt -s nullglob
then unmatched globs will be removed altogether and not be left on the command line unexpanded.
If you want case-insensitive globbing (so *.mp3 will match foo.MP3):
shopt -s nocaseglob
-R
option to list matching files in subdirectories but the option doesn't seem to have any effect and I still only get the current directory's results.
Just in case: why don't you use find
?
find -iname '*.mp3' -o -iname '*.exe' -o -iname '*.mp4'
find . -name '*.mkv' -o -name '*.flv'
(adding as many -o clauses as required). I needed the .
to indicate the directory and the flag -name
not -iname
- I'm on macOS 10.13.
In case you are still looking for an alternate solution:
ls | grep -i -e '\\.tcl$' -e '\\.exe$' -e '\\.mp4$'
Feel free to add more -e flags if needed.
For OSX users:
If you use ls *.{mp3,exe,mp4}
, it will throw an error if one of those extensions has no results.
Using ls *.(mp3|exe|mp4)
will return all files matching those extensions, even if one of the extensions had 0 results.
ls *.@(mp3|exe|mp4)
. You need shopt -s extglob
for this to work. By the way, ls
is useless too, you could just do printf '%s\n' *.@(mp3|exe|mp4)
.
ls | grep "\.mp4$
\.mp3$
\.exe$"
mdfind
yields the best / fastest searches EVER! mdfind -name querystring | grep "\.h$"
finds all headers with quesrystring in the file title. pronto.
ls -R | findstr ".mp3"
ls -R
=> lists subdirectories recursively
it is easy try to use this command :
ls | grep \.txt$ && ls | grep \.exe
Here is one example that worked for me.
find <mainfolder path> -name '*myfiles.java' | xargs -n 1 basename
Success story sharing
ls: *.exe: No such file or directory
eg:ls *.{zip,tar.gz,tar} 2>/dev/null