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

Forum rules
Before you post please read this

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

Postby Bit Mad on Thu Mar 15, 2012 9:13 am

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:
Last edited by Bit Mad on Thu Mar 15, 2012 11:01 am, edited 1 time in total.
User avatar
Bit Mad
Level 3
Level 3
 
Posts: 135
Joined: Mon Nov 16, 2009 9:00 am

Linux Mint is funded by ads and donations.
 

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

Postby xenopeek on Thu Mar 15, 2012 9:20 am

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.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14890
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

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

Postby Bit Mad on Thu Mar 15, 2012 11:00 am

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:
User avatar
Bit Mad
Level 3
Level 3
 
Posts: 135
Joined: Mon Nov 16, 2009 9:00 am

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

Postby xenopeek on Thu Mar 15, 2012 11:52 am

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:
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14890
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

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

Postby Bit Mad on Fri Mar 16, 2012 6:54 am

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!
User avatar
Bit Mad
Level 3
Level 3
 
Posts: 135
Joined: Mon Nov 16, 2009 9:00 am


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 3 guests