[ Prev ] [ Table of Contents ] [ Front Page ] [ FAQ ] [ Next ]

(?) The Answer Guy (!)

By James T. Dennis, tag@lists.linuxgazette.net
LinuxCare, http://www.linuxcare.com/

(?) DIR /S

From Romulus Gintautas on Sun, 14 May 2000

First off, thank you for your time.

I did a man on ls but did not find what I was looking for. I'm looking for a linux equivalent of dir /s (DOS). Basically, I am looking for a way to find how much data is stored in any specific dir in linux (red hat 6.0). As you know, in dos, all you do is enter the dir in question and just do dir /s.

(!) Under UNIX we use a separate command for that.
You want the 'du' (disk usage) command. So a command like:
du -sck foo bar
... will give you summaries of the disk usage of all the files listed under the foo and bar directories. It will also give a total, and the numbers will be in kilobytes. Actually "foo" and "bar" don't have to be directory names; you can list files and directories --- basically as many as you like. Of course you can mix and match these command line switches (-s -c -k, and many others).
To work with your free disk space you can use the 'df' (disk free) command. It also has lots of options. Just the command 'df' by itself will list the free disk space on all of your currently mounted regular filesystems. (There are about a half dozen psuedo-filesystems, like /proc, devpts, the new devfs and shmfs and some others that are no listed by 'df' --- because the notion of "free space" doesn't apply to them).
Anyway, read the man pages for both of these utilities to understand them better. Read the 'info' pages to learn even more.
Incidentally --- if you want to get more detailed information about a list of files than 'ls' can provide, or you need the meta information in custom format then you usually want to use the UNIX/Linux 'find' command. This is basically a small programming language for "finding" a set of files that match a set of criteria and printing specific type of information about those files, or executing commands on each of them.
In other words 'find' is one of the most powerful tools on a UNIX system. As a simple example, if I want to find the average file sizes of all of the "regular" files under a pair of directories I can use a command like:
      find foo bar -type f -printf "%s\n" | awk '{ c++; t+= $1 }; END { print "Average: ", t/c }'
The 'find' command looks at the files/directories named "foo" and "bar" finds all of them that are of type "f" (regular files) and prints their sizes. It doesn't print ANYTHING else in this case, just one size in bytes, per line. The 'awk' command computes the average (awk is a little programming language, simpler than PERL).
To find all of the files older than one week in the current directory you can use a command like:
find . -ctime +7
... for those that are newer than a week:
find . -ctime -7
... (BTW: UNIX keeps three timestamps on its files,
ctime is the timestamp on the "inode" --- when the file's meta-data was modified, the mtime is the timestamp for the file's data when the data blocks OR meta-data were touched and atime is the last "access" (read) time).
I think the current version of GNU 'find' has about 60 options and switches (including support for -and, -or, and -not for combining complex expressions) and the -printf and -fprintf directives support about 25 different "replaceable parameters" and a variety of formatting options within some of those.
About the only bit of 'stat' information I can't get write from 'find' is the "device number" on which a file resides. (Under UNIX every file can be uniquely identified by the combination of device number and inode. inodes are unique within any given device). 'find' also doesn't (yet) give me the ability to print or test some special "flags" (BSD UFS) or "attributes" (Linux ext2).
I've been meaning to write a custom patch to add those features.

(?) I apologize if this is a simple question. I am just starting in Linux and hope to learn a lot more.


(!) That's O.K. I'm too tired to do hard questions at the moment.

Copyright © 2000, James T. Dennis
Published in The Linux Gazette Issue 54 June 2000
HTML transformation by Heather Stern of Tuxtops, Inc., http://www.tuxtops.com/

[ Answer Guy Current Index ] greetings   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18 [ Index of Past Answers ]

[ Prev ] [ Table of Contents ] [ Front Page ] [ FAQ ] [ Next ]