|
|
Tree Command?
|
|
|
|
Forum Regular
Join Date: Feb 2007
Status:
Offline
|
|
Hello, this is my first (of a potential many) post to this forum, as I am a recent convert (I bought my MacBook two weeks ago). Now, I have been using Windows since '96ish, and about 4 years ago, starting using Linux off and on. I was wondering... is there a Mac terminal command equivalent to 'tree' on Linux? I used to use this command to count how many files are in a massive directory structure. I fired up my Mac terminal tonight, and saw that 'tree' was not a valid command. So, I used the finder, selected all of the directories, chose 'Get info', only to be told how many directories I had selected (not how many files were in said directories).
Any assistance would be great, as it will take me a bit to get spun up... Thanks!
|
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Feb 2007
Status:
Offline
|
|
Nevermind... ls -R looks like it will do for now. I must have not entered in friendly search terms before, because I never saw it until after I posted this. Thanks anyway. I'll be back...
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Feb 2005
Status:
Offline
|
|
Since under the hood it's UNIX, and you're already in terminal.app, a find should do what you need:
find . -type d -print
In whatever directory you want to find all the directories. The output might be a bit 'busy', but it'll get you the folders under that folder (directory.)
|
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Feb 2007
Status:
Offline
|
|
Thanks for that! I actually like the output of yours better. The 'ls -R' is very busy, much more so than I need. I was going to ask if I could assume that I could also use '-type f' to see the actual files within those directories, or if that show the '.' and '..' in each directory as well, but decided that instead of wasting time with it, I would just try it, and it works as expected (just in case anyone else newer than I was curious). So, in the end I am using 'find [directory-to-start-in] -type f | wc -l' and it seems to be okay for my needs now... Thanks again!
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Mar 2004
Status:
Offline
|
|
Originally Posted by vwgtiturbo
So, in the end I am using
find [directory-to-start-in] -type f | wc -l
and it seems to be okay for my needs now.
Note that will delve inside any "packages" along the way (apps, bundles, prefPanes, etc.)
which can be prevented by adding -not -path '*Contents/*' to the find parameters,
(unless you want to count such contents that is).
Also counted that way will be DS_Stores (and other similarly hidden items), which can be
excluded via -not -name '.DS_Store' (or -not -name '.*').
--
I'm curious to learn the practical usage of such numbers. How does one use that knowledge?
For example...
[codex]
$ find ~/Library -not -path '*Contents/*' -not -name '.DS_Store' -type f | wc -l
7118
[/codex]
Okay... my ~/Library folder has "7,118 files."
Now what?
|
-HI-
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Mar 2001
Location: yes
Status:
Offline
|
|
How does one use the knowledge? It depends on the objective behind seeking this information in the first place - what were you hoping to accomplish?
|
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Feb 2007
Status:
Offline
|
|
Hmm... I hadn't thought of following the packages and such. I guess that is just the 'nix CLI newb in me...
Initially, my intent was to find how many MP3s were in my Music directory (which in hindsight would have been better served by 'find /Music -name *.mp3') because for some strange reason, the count on my backup server didn't match what was on my Mac, so I was a little worried that my backup strategy failed and that I would be reripping 600 CDs.
In any case, I am not sure why this escaped me... I was making it so much more complicated than it needed to be. My brain just isn't in it right now...
|
Black 13" Widescreen MacBook
2.0Ghz C2D, 2GB RAM, 320GB HDD
Mac OS X v10.6 Snow Leopard
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Sep 2005
Location: Los Angeles, California
Status:
Offline
|
|
Welcome to the forums!!
Also, your sig is too large. Keep it to 4 lines or less please!
|
Linkinus is king.
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Nov 2003
Location: The back of the room
Status:
Offline
|
|
For such a simple search Command f in the Finder will do you wonders.
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Mar 2004
Status:
Offline
|
|
Originally Posted by vwgtiturbo
Initially, my intent was to find how many MP3s were in my Music directory (which in hindsight would have been better served by 'find /Music -name *.mp3') because for some strange reason, the count on my backup server didn't match what was on my Mac
That's why I asked. Once we know we're looking to count a "type" of file,
then that's a good job for a Spotlight search -- saved as a Smart Folder.
[kind : music]
Try it... you'll like it.
|
-HI-
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Nov 2005
Status:
Offline
|
|
I do have a "tree" command on my G4 Powerbook, but I forget where I downloaded it. I'm not sure if I have tree on my newer Mac.
Here is a computer geek way to run a command that might be useful. First open the Terminal program from Applications/Utilities and run this command to see the output:
find ./ -type d|sed -e "s/ /\\\ /g" -e "s/'/\\\'/g" -e 's/"/\\\"/g'|xargs ls -ldT
This uses the find command, piped into the sed command to deal with embedded spaces, and then uses xargs to take that output and send it to the ls command to show the size, ownership, and other information of each directory. There is a chance that some folders with wacky characters will cause a problem, but you could add those characters to the sed command to fix it.
You can copy that line into a text file and call the file tree.sh and put it in the /usr/bin folder. Then run the command:
sudo chmod ugo=rx tree.sh
from the /usr/bin folder to make that script executable and read-only. Then run the command tree.sh to create the output relative to the current directory.
If you wanted the output sent to a file, you could type:
tree.sh > ~/mytree.txt
which sends the output to a file called mytree.txt in your home directory.
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Mar 2004
Status:
Offline
|
|
Originally Posted by rehoot
find ./ -type d|sed -e "s/ /\\\ /g" -e "s/'/\\\'/g" -e 's/"/\\\"/g'|xargs ls -ldT
This uses the find command, piped into the sed command to deal with embedded spaces, and then uses xargs to take that output and send it to the ls command to show the size, ownership, and other information of each directory. There is a chance that some folders with wacky characters will cause a problem, but you could add those characters to the sed command to fix it.
Actually, there's a much easier way to deal with odd chars (even newlines!).
Take out all the seds and put in find's print0 and add the 0 option to xargs:
find ./ -type d -print0 | xargs -0 ls -ldT
The '0' (those are zeros, btw) appends ascii \000 (the "null" char) to the end of
each filepath, and then xargs takes it off as it reads each name. (also see grep -Z).
" -print0|xargs -0" is the way to go, if you want to go that way.
|
-HI-
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Feb 2007
Status:
Offline
|
|
Wow, that is nuts... I wish I were a command line guru... I find that I don't have the memory to remember all of the ins and outs of it anymore (and I am only 30... I can't wait to see how bad it is when I hit 50+).
I will have to try some of this later, when things have calmed down enough to afford some spare time... Thanks for the tips!
|
Black 13" Widescreen MacBook
2.0Ghz C2D, 2GB RAM, 320GB HDD
Mac OS X v10.6 Snow Leopard
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|