The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
list contents of directory carl_vieyra UNIX for Dummies Questions & Answers 3 01-30-2007 03:24 PM
how to generate a random list from a given list mskcc Shell Programming and Scripting 3 05-30-2006 03:30 AM
Creating file contents using contents of another file ReV Shell Programming and Scripting 21 02-24-2006 10:25 AM
Message trying to list contents of directory sallender UNIX for Dummies Questions & Answers 1 10-19-2005 08:22 AM
Comparing a distinct value in 1 list with another list manualvin Shell Programming and Scripting 6 06-22-2004 06:42 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-05-2008
bbilheimer bbilheimer is offline
Registered User
  
 

Join Date: Feb 2008
Location: Pasadena, CA
Posts: 3
Post Best way to list a directory's contents?

Hey guys!

I'm so glad I found this site, I've had so many questions and have been left alone for roughly a year scanning man pages but It's just not quite cutting it for some things.

So, I often like to list directories when browsing around my local machine, a friend's machine, or my web server. Especially the latter where size, permissions, and extensions really matter. The problem is most of the commands I use just don't quite show all the information I need!

If I use
Code:
ls -lsh
I get what I want, human readable file sizes, and I can sort the files in any number of ways, but it's messy and too much information I never use. With lots of files it can be huge and hard to read.

If I use
Code:
du -sh *
I get a nice crisp clear output with file sizes but this isn't supported on my web server while in FTP mode in my shell.

Both of these have the same problem: after reading the man pages up and down I can't figure out how to simply list the total number of files in a given directory!

I hope these questions aren't ridiculous, I'd just like to see if there is a better way of doing this that other people know, I've held of on asking anyone until now to avoid all of the "RTFM" responses I'd get in IRC Channels.

Thanks in advance!
  #2 (permalink)  
Old 02-05-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
Quote:
Originally Posted by bbilheimer View Post
I can't figure out how to simply list the total number of files in a given directory!
total number of files, including directories
Code:
# ls -1 |wc -l
number of files, recursive
Code:
# find /path -type f  | wc -l
  #3 (permalink)  
Old 02-05-2008
bbilheimer bbilheimer is offline
Registered User
  
 

Join Date: Feb 2008
Location: Pasadena, CA
Posts: 3
Lightbulb

Cool, thanks! So you use ls to list it in separate it lines then pipe it to wc to count those lines and print the total number.

That seems like such a hassle, though, there is no flag or something like that so that du or ls can simply show the total number of files in its output?
  #4 (permalink)  
Old 02-05-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
Quote:
Originally Posted by bbilheimer View Post
Cool, thanks! So you use ls to list it in separate it lines then pipe it to wc to count those lines and print the total number.

That seems like such a hassle, though, there is no flag or something like that so that du or ls can simply show the total number of files in its output?
why do you think its a hassle? FWIW, you can just do it one time. save it as a shell library or function or shell script and you can reuse it next time. Its just a one time effort.
  #5 (permalink)  
Old 02-05-2008
bbilheimer bbilheimer is offline
Registered User
  
 

Join Date: Feb 2008
Location: Pasadena, CA
Posts: 3
It's not that tough, I've just always loved using the shell because you can do so much with so little text, I try to compact as much as I can in one line. This works well, and is just what I wanted, thanks so much!

What about the differences between du and ls? Do most people use the latter even though it's not a very simple output? Are there better ways of doing it? I ask because I'm always surprised by little neat tricks and tips people have here and there.
  #6 (permalink)  
Old 02-05-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
Quote:
Do most people use the latter even though it's not a very simple output? Are there better ways of doing it?
they serve different purposes. pls read their man pages to see what they can do.
  #7 (permalink)  
Old 02-05-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
Quote:
Originally Posted by bbilheimer View Post
What about the differences between du and ls?
man du, man ls

The former tells you about "disk usage", that is: how much space is used by a file or a part of a directory tree. The following method will tell you how much space is used by which part of the file hierarchy:

cd to a directory
issue "du -ks * | sort -rn"

You will get a list of files/directories with their respective space used sorted reversely by size. If you find a directory in this list with an abnormal size, then change into it and repeat the command there to see which files/directory is responsible for that, and so forth.

The latter command (ls) is to list directory information: my system (AIX) lacks the -h option you seem to find so useful, but it has ~20 other options to tailor the output to exactly your needs. Most of the times i use "ls -l" or "ls -lai", "ls -lrt" to get the files sorted by time and for everything else i look into the manpage for reference. Nobody knows all the options of "ls" by heart, it is for most of us a mixture of having a general grasp of what to use and a knowledge of where to find additional information when it is needed.

Quote:
Do most people use the latter even though it's not a very simple output?
Most common is "ls -l", but only because there is all the information you "normally" need. Use whatever is suited to your needs, there is no "One True Way" as with churches: UNIX is an ecumenical system, so to say. ;-))

Quote:
Are there better ways of doing it? I ask because I'm always surprised by little neat tricks and tips people have here and there.
What is "better" is defined by what you want to accomplish: there is no "better" way to get the information "ls -l" provides than issuing "ls -l", etc. The "neat little tricks" are just to strip some abundance of information down to the very minimum you need for a specific task.

If you think you need that set of info on a regular basis use "alias" and "function" to make it easier accessible, as ghostdog74 has already told you.

bakunin
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:27 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0