ls question

hello,

I have been doing some testing with the "ls" command and basically I want to list not only the files/folders in a directory but also the files/folders contained in any directory found. I believe the command would be "ls -R" correct? the thing is I have noticed with this command it would also search hidden directories such as a directory within an app. is there another command I should be using that will only show what was found inside a folder for example and not inside something like an app or .pkg file?

thank you,

rick

macbook, Mac OS X (10.5.6)

Posted on Mar 3, 2009 6:09 AM

Reply
11 replies

Mar 4, 2009 5:35 AM in response to Cole Tierney

thank you both and i will definitely look more into this link. in a related question maybe too i can ask this...what would you recommend as a method for searching? for example i want to find certain files on my mac with my wife's name in the filename. the way i see it i have these options:

locate - maybe a problem if database is not updated?
find - finds everything but how to exclude some certain types?
ls | grep - another possibility?
spotlight - would have to use cocoa

of course i want to do this quickly so updating the database first (for example with locate) is not good. any thoughts? this probably explains a bit better what i'm trying to do...

thank you,

rick

Mar 4, 2009 6:09 AM in response to rick722

in a related question maybe too i can ask this...what would you recommend as a method for searching? for example i want to find certain files on my mac with my wife's name in the filename.

As with everything in life, *It Depends*

find / -x -name "wifename" | egrep -v "DoNotWantThis|NorThis|NotThis|etc..."

This would be complete, but it will take time. NOTE: The find command does have -regex options which if you want to exclude stuff you would code as

find / -x -name "wifename" ! -regex ".Library." ! -regex ".Pictures."

If your wife's name would only appear in typical user documents, then just use Spotlight as that is fast and generally always up to date.

The locate command can be useful if your wife's name is goingt to be clustered with other occurances. Once you find 1 you can then drill down using the ls command to find the related occurances.

And just like you, your wife's name is just an example.

Mar 4, 2009 6:14 AM in response to rick722

i want to find certain files on my mac with my wife's name in the filename.


You could start with something like:
<pre style="border: 1px solid #ddd; padding-left: .75ex; padding-top: .25em; padding-bottom: .25em; margin-top: .5em; margin-bottom: .5em; margin-left: 1ex; max-width: 30ex; overflow: auto; font-size: 10px; font-family: Monaco, 'Courier New', Courier, monospace; color: #444; background: #eee; line-height: normal">find . -iname ' MrsRick'</pre>
--
Cole

Mar 4, 2009 6:28 AM in response to BobHarris

thank you bob for the quick reply...

yes i know it all depends so of course i have my work cut out for me. 🙂 i would like as much as possible to do a full system search, but i might take a few shortcuts if the time is too long. however i don't want to limit my search locations too much or else i might miss out on too many files. i have been thinking about spotlight, but since my wife's name is an example and i need to look in other places than user documents i'm afraid spotlight might leave too many things out. and i definitely need to customize it a bit (like you inclusion of egrep) and i'm not sure too if i can do all that with spotlight. am i right in assuming i need to do it in cocoa if i want spotlight? or it there a way to do it with terminal?

as a note...i can't seem to run the -x only -xdev is that what you're referring to?

thank you very much,

rick

Mar 4, 2009 6:46 AM in response to rick722

...but i might take a few shortcuts if the time is too long. however i don't want to limit my search locations too much or else i might miss out on too many files.

One short cut is to only specify directories that are likely to contain the files you want.

find /Users ...

would eliminate most system directories. And the find command can accept multiple directories on the command line

find /Users /Library -name "wifename"

That can save time if you are sure the files you want are not in other directory trees.
i have been thinking about spotlight, but since my wife's name is an example and i need to look in other places than user documents i'm afraid spotlight might leave too many things out. and i definitely need to customize it a bit (like you inclusion of egrep) and i'm not sure too if i can do all that with spotlight. am i right in assuming i need to do it in cocoa if i want spotlight? or it there a way to do it with terminal?

There are the mdfind, mdls, mdutil, mdimport, etc... which can be used from the command line.
as a note...i can't seem to run the -x only -xdev is that what you're referring to?

Yea. I actually always remember -xdev, but when I looked at the find man page, it said -xdev was deprecated so I figured I should post what the man page said was the current option. No one ever said the man pages were 100% up-to-date 🙂

Mar 4, 2009 7:07 AM in response to BobHarris

thanks again bob!

i think i will have to spend some time doing my homework to see what works best. i'm already there with mdfind (good i can do all my experiments from terminal) and i'll just have to see if spotlight will work for me. (i know there are limitations when you use it normally such as leaving out certain locations, hidden files, etc. so i'll have to see if these can be overcome.)

and i agree with you about the man pages. it should be -x how it's written but oh well? 🙂

thanks again very much,

rick

Mar 5, 2009 12:23 AM in response to Jeffrey Jones2

hello again,

i hope it's ok i continue this thread. i wanted to share what i found so far. it seems using terminal mdfind is definitely the fastest and does a pretty good job. a couple questions would be what if there's a mac where there is no spotlight index? will mdfind still work only much slower? and how to enable finding "." hidden files? it seems to find invisible directories and i know if we use find in finder you can select to show hidden files but i couldn't figure it out using mdfind...

with locate also requiring a database build it would seem better to go with mdfind since most people use spotlight anyways. as for find | grep or ls | grep neither one can match the speed it seems...

one more question if it can be answered here. i have come across the carbon class FSCatalogSearch and the cocoa class NSDirectoryEnumerator and does anyone know if these are superior?

thank you again for all the help and input,

rick

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

ls question

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.