Visit Our UNIX and Linux User Community


Best way to list a directory's contents?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Best way to list a directory's contents?
# 1  
Old 02-05-2008
Java 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  
Old 02-05-2008
Quote:
Originally Posted by bbilheimer
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  
Old 02-05-2008
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  
Old 02-05-2008
Quote:
Originally Posted by bbilheimer
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  
Old 02-05-2008
Smilie 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  
Old 02-05-2008
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  
Old 02-05-2008
Quote:
Originally Posted by bbilheimer
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
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #512
Difficulty: Medium
PHP requires that all variables begin with a dollar sign, for example $flag.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Zipping contents without the actual directory

Hi , I want to zip files present in the directories listed under a parent directory without zipping the directory itself my parent directory path is /informatica/DGDMN/PowerCenter1011/server/infa_shared/SrcFiles/OTE/Final_Directory I have the below directories named as 1,2,3,4,5 listed... (9 Replies)
Discussion started by: paul1234
9 Replies

2. UNIX for Dummies Questions & Answers

What option will use for deleting directory with all its contents?

Hi How to completely delete directory with all it contents I try to use rmdir -r but it give error Thanks ---------- Post updated at 03:10 AM ---------- Previous update was at 02:52 AM ---------- Hi all I got the solution for my thread i use mkdir with the option -p Thanks (1 Reply)
Discussion started by: Tauatioti
1 Replies

3. Shell Programming and Scripting

Need Help with Bash - comparing directory contents with list of files

Hey guys, need help with a script I'm trying to write. Basically I need to compare the contents of a folder called "profiles" with a list of files called "template". when the file matches the contents of the folder it needs to set a variable called "checked" to "1" Cookies to anyone... (4 Replies)
Discussion started by: Scriporium
4 Replies

4. Shell Programming and Scripting

Create a list of directory contents

Well I did a search and didn't anything for my specific case. I got a directory with a bunch of text file. All of them have the following pattern on the filename "ABCD_<As of Date>.txt" Example: ABCD_20110301.txt ABCD_20110302.txt ABCD_20110303.txt All I want to accomplish is a Korn... (3 Replies)
Discussion started by: Shark Tek
3 Replies

5. Shell Programming and Scripting

Remove contents of directory, but not directory

What's the best way to delete everything in a directory, but not the directory itself, without using shell wildcards? (9 Replies)
Discussion started by: pdc
9 Replies

6. Shell Programming and Scripting

Regular Expression on Directory Contents

This should be an easy question for you gurus. :) How can I create a regular expression to match all files in the current directory that have only one period in their file name, and also not contain the string "abc" before the period? It would match: foo.txt foobar.log It would not... (4 Replies)
Discussion started by: blondie53403
4 Replies

7. Solaris

Directory should not be deleted, But the contents can be

Hi Guys, I have an user's home directory set to /home/A And A contains the following directories B & C Is there some way in solaris by which i can prevent the directories B and C from getting deleted by the user but the contents of the directories B & C can be deleted ? Also i have... (2 Replies)
Discussion started by: localhost
2 Replies

8. UNIX for Dummies Questions & Answers

list contents of directory

I want to list the contents of a directory, but I do not want to use the ls, is there another way?? (3 Replies)
Discussion started by: carl_vieyra
3 Replies

9. UNIX for Dummies Questions & Answers

Message trying to list contents of directory

I'm getting this return whenever I try to do anything on a directory root# ls -al /directory ls: .: Value too large to be stored in data type. total 0 I can change directory down two levels but can not list contents of the root of this directory. ANy one seen this? (1 Reply)
Discussion started by: sallender
1 Replies

10. UNIX for Dummies Questions & Answers

Unable to view contents of a directory

Hi, first post here be gentle. Very new to Unix. Using HP-UX 10.20 I CD into a remote directory on one machine $ cd /net/remote hostname yet when I do an ll in this directory none of the contents appear. It just is empty. when I do the same command from another machine, $ cd... (13 Replies)
Discussion started by: maddave
13 Replies

Featured Tech Videos