Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Human-readable sizes all over the place UNIX
I saw this older hint about getting the "human readable" version of information from the df and du commands. As of Panther, at least, the du, df and ls commands all take the -h option. Not only is it no longer necessary to tweak your du or df command to get the behavior, but I think this feature in ls is relatively recent, too.

As far as I can tell, Mac OS did not adopt the GNU fileutils, but they did adopt this nice feature. I find it handy when I'm using a Terminal to use ls -lh, so I don't have to count digits to figure out the sizes. Here's an example:

% ls -lh
total 5504344
-rw-rw-r--  1 paco  staff         42B  3 Aug 20:59 01.gif
-rw-rw-r--  1 paco  staff        872M  3 Aug 22:43 BTV Movie 001.mov
-rw-rw-r--  1 paco  staff          1G  3 Aug 22:47 BTV Movie 002.mov
-rw-rw-r--  1 paco  staff          2M 30 Jul 14:54 GameServices.pdf
-rw-rw-r--  1 paco  staff          1M  3 Aug 17:29 befw11s4_v4_ug.pdf
-rw-r--r--  1 paco  staff        460B  3 Aug 16:58 chiras1-nt
[robg adds: If you use the -h option a lot, you might want to create an alias for it -- I use lsh, which is an alias for ls -alh, the options I use most often with ls.]
    •    
  • Currently 1.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[20,764 views]  

Human-readable sizes all over the place | 10 comments | Create New Account
Click here to return to the 'Human-readable sizes all over the place' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Warning on alias
Authored by: Jordi Bunster on Aug 04, '04 11:11:54AM

It's because of their inclusion in FreeBSD 5 that Panther has them. They're not (as said) the GNU stuff, they just added -h.

I would NOT recommend aliasing the -h to the normal command, or aliasing any POSIX command to itself plus some switches, because scripts could easily rely on those acting according to POSIX. I'm not sure if the aliases also work for the scripts, but I imagine they would, unless one uses bash's features to differentiate interactive from non-interactive shell instances.

Of course, it depends on how much UNIX stuff you do, but it's always good to remember that.



[ Reply to This | # ]
Warning on alias
Authored by: geohar on Aug 05, '04 06:16:43AM

This is actually pretty common practice and won't cause recursion.

Shell scripts are interpreted by a separate shell invocation. For example, the sh-bang line will cause bash to be invoked in non-interactive sh compatibility mode (under os x earlier than 10.3 this was actually an invocation of zsh). At any rate, in this mode no initialization scripts are read - so no aliases occur.

As an example, create a simple file:

#!/bin/sh
ll

and chmod it to be executable. Normally ll is an alias to 'ls -l'. Type ll in the terminal to confirm this. Then execute the file. You should get 'command not found'.

To confirm -- script invocations run in a separate shell. whilst the environment bindings are inherited by that shell, aliases and the rest are not. Secondly, the bash invocation which runs the script will not read initialization files. This can often cause you to scratch your head if you include extra paths in the initialization file, run some commands that work interactively and then don't work in a script.

HTH



[ Reply to This | # ]
Human-readable sizes all over the place
Authored by: madrobby on Aug 04, '04 11:22:59AM

You could also create an alias to have your standard "ls" work this way. Just add a line like this to your .bashrc:

alias ls="ls -(options you want)"



[ Reply to This | # ]
Human-readable sizes all over the place
Authored by: frankxiv on Aug 04, '04 04:11:28PM

This is nice if all you want to know is the size of the individual files. Does anyone know of a unix command that will give you the total size of a directory (and all of its contents)?

Frank



[ Reply to This | # ]
Human-readable sizes all over the place
Authored by: dr_bob_cms on Aug 04, '04 04:45:58PM

du does exactly that. man du



[ Reply to This | # ]
Human-readable sizes all over the place
Authored by: frankxiv on Aug 06, '04 09:12:59AM

Thanks!



[ Reply to This | # ]
Human-readable sizes all over the place
Authored by: cubal on Aug 04, '04 04:49:53PM
du -sh "dirname" (s for summary, h for human-readable) will give you what you're after.

[ Reply to This | # ]
Human-readable sizes all over the place
Authored by: frankxiv on Aug 06, '04 09:19:13AM

Thanks! du -sh "directory name" did indeed give me the total size of a directory.

Also, for those interested du -ah gives you the size of every file inside of the directory (and in the directories inside of that directory). In other words, it will recursively list all files inside of a directory (including other directories) along with the size of each. That is exactly the command I was looking for.

Thanks everyone!

Frank



[ Reply to This | # ]
Human-readable sizes all over the place
Authored by: xor on Aug 10, '04 07:24:05PM
 du -h some_dir_name | tail -1


[ Reply to This | # ]
Human-readable sizes all over the place
Authored by: ogs on Aug 11, '04 12:09:03PM

One I use quite frequently is

du -d1 -h

where -d[num] is the depth that you want to show

du -d1 | sort -n

is handy too



[ Reply to This | # ]