Page 1 of 1

Search with ls, like MS-DOS dir /s ...?

Posted: Thu Mar 15, 2012 9:13 am
by Bit Mad
Really basic question... I'm not too embarrased to ask :oops:

I gave up on Windows search years ago (kept missing out files I knew were there, GUI changes from one Windows version to the next), so I open a command window, cd\ if necessary, and
dir whatever.* /s
This scans the whole disk ( /s being to specify subdirectories) and finds what I'm looking for.

I tried to use ls in Linux, but although ls -R does list the whole disk, ls -R *.mp3 doesn't do what I expect - because if there's no file matching the search criteria in the initial folder, it errors (No such file or directory) and doesn't try anywhere else.
Doesn't look like any other switches would help, either.

What's the secret to such a simple search? :mrgreen:

Re: Search with ls, like MS-DOS dir -s ...?

Posted: Thu Mar 15, 2012 9:20 am
by xenopeek
Instead of ls, use find. As in: find path-to-search what-to-search-for

To find all mp3 files in the current folder and all subfolders:

Code: Select all

find . -name \*.mp3
The \* makes the wildcard (*) not expanded to match files in the current folder (else you would only find those mp3 files :wink:). The \ is an escape character, ensuring the next character is kept as a literal and not expanded by the shell to do filename matching (aka globbing).

You can also search case-insensitive:

Code: Select all

find . -iname \*.mp3
Find has lots more nice features.

Re: Search with ls, like MS-DOS dir /s ...?

Posted: Thu Mar 15, 2012 11:00 am
by Bit Mad
Thanks, that did the trick... more or less
- the wanted stuff is buried in the midst of tons of other lines of Permission denied :lol:

I still prefer the dir /s I'm used to with DOS though... you get a summary per folder with count of files and total filesize, and each line shows date/time and size too.

Sounds like the basis for a script.... :mrgreen:

Re: Search with ls, like MS-DOS dir /s ...?

Posted: Thu Mar 15, 2012 11:52 am
by xenopeek
You can disregard any warnings, by either running it as root, as in:

Code: Select all

sudo find . -name \*.mp3
Or, preferably, just disregarding the warnings:

Code: Select all

find . -name \*.mp3 2>/dev/null
(This redirects file descriptor 2, known as stderr, or where the errors are written, to the bit bucket.)

If you want to scan the whole disk, just replace . as path with /, as in:

Code: Select all

find / -name \*.mp3 2>/dev/null
With the -printf "format" argument added you can also tell it how to output its results. See "man find" for details, you'll need that anyway to write a script :wink: If you know perl, you can automatically generate the perl code for any find command with find2perl. Try it:

Code: Select all

find2perl . -name \*.mp3
Just edit the created code to tune it to what you need :mrgreen:

Re: Search with ls, like MS-DOS dir /s ...?

Posted: Fri Mar 16, 2012 6:54 am
by Bit Mad
Many thanks for the help, all works well.. shame it's so much more verbose than dir /s :mrgreen:
- but if I'm going to use it more often then I'll get used to it or script something up!

Thanks!