Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 23, 2016

Using ls to list directories and their total sizes

Using ls to list directories and their total sizes


Is it possible to use ls in Unix to list the total size of a sub-directory and all its contents as opposed to the usual 4K that (I assume) is just the directory file itself? I.E.

total 12K  drwxrwxr-x  6 *** *** 4.0K 2009-06-19 10:10 branches  drwxrwxr-x 13 *** *** 4.0K 2009-06-19 10:52 tags  drwxrwxr-x 16 *** *** 4.0K 2009-06-19 10:02 trunk  

After scouring the man pages I'm coming up empty.

Answer by zappan for Using ls to list directories and their total sizes


look at du command for that

Answer by molf for Using ls to list directories and their total sizes


Try something like:

du -sh *  

Explanation:

du: Disk Usage

-s: Display an entry for each specified file. (Equivalent to -d 0)

-h: "Human-readable" output. Use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte.

Answer by Crenshaw for Using ls to list directories and their total sizes


I always use du -sk (-k flag showing file size in kilobytes) instead.

Answer by Keith Smith for Using ls to list directories and their total sizes


The command you want is 'du -sk' du = "disk usage"

The -k flag gives you output in kilobytes, rather than the du default of disk sectors (512-byte blocks).

The -s flag will only list things in the top level directory (i.e., the current directory, by default, or the directory specified on the command line). It's odd that du has the opposite behavior of ls in this regard. By default du will recursively give you the disk usage of each sub-directory. In contrast, ls will only give list files in the specified directory. (ls -R gives you recursive behavior.)

Answer by GraveDigger for Using ls to list directories and their total sizes


du -sk * | sort -n will sort the folders by size. Helpful when looking to clear space..

Answer by user2969885 for Using ls to list directories and their total sizes


du -sh * | sort -h

This will be display in human readable format

Answer by Pascal for Using ls to list directories and their total sizes


To display current directory's files and subdirectories sizes recursively:

du -h .  

To display the same size information but without printing their sub directories recursively (which can be a huge list), just use the --max-depth option:

du -h --max-depth=1 .  

Answer by kakubei for Using ls to list directories and their total sizes


These are all great suggestions, but the one I use is:

du -ksh * | sort -n -r  

-ksh makes sure the files and folders are listed in a human-readable format and in megabytes, kilobytes, etc. Then you sort them numerically and reverse the sort so it puts the bigger ones first.

The only downside to this command is that the computer does not know that Gigabyte is bigger than Megabyte so it will only sort by numbers and you will often find listings like this:

120K  12M  4G  

Just be careful to look at the unit.

This command also works on the Mac (whereas sort -h does not for example).

Answer by Riasat for Using ls to list directories and their total sizes


du -h --max-depth=1 . | sort -n -r  

Answer by John for Using ls to list directories and their total sizes


du -sch * in the same directory.

Answer by Sebi for Using ls to list directories and their total sizes


To display it in ls -lh format, use:

(du -sh *; ls -lh --color=no) | awk '{ if($1 == "total") {X = 1} else if (!X) {SIZES[$2] = $1} else { sub($5 "[ ]*", sprintf("%-7s ", SIZES[$9]), $0); print $0} }'  

Awk code explained:

if($1 == "total") { // Set X when start of ls is detected    X = 1   } else if (!X) { // Until X is set, collect the sizes from `du`    SIZES[$2] = $1  } else {    // Replace the size on current current line (with alignment)    sub($5 "[ ]*", sprintf("%-7s ", SIZES[$9]), $0);     print $0  }  

Sample output:

drwxr-xr-x 2 root     root 4.0K    Feb 12 16:43 cgi-bin  drwxrws--- 6 root     www  20M     Feb 18 11:07 document_root  drwxr-xr-x 3 root     root 1.3M    Feb 18 00:18 icons  drwxrwsr-x 2 localusr www  8.0K    Dec 27 01:23 passwd  

Answer by Jay Chakra for Using ls to list directories and their total sizes


To list the largest directories from the current directory in human readable format:

du -sh * | sort -hr

Sample:

[~]$ du -sh * | sort -hr  48M app  11M lib  6.7M    Vendor  1.1M    composer.phar  488K    phpcs.phar  488K    phpcbf.phar  72K doc  16K nbproject  8.0K    composer.lock  4.0K    README.md  

It makes it more convenient to read :)

Answer by CrossEntropy for Using ls to list directories and their total sizes


For a while, I used Nautilus (on Gnome desktop on RHEL 6.0) to delete files on my home folder instead of using the rm command in bash. As a result, the total size shown by

du -sh  

did not match the sum of disk usage of each sub-directory, when I used

du -sh *  

It took me a while to realise Nautilus sends the deleted files to its Trash folder, and that folder is not listed in du -sh * command. So, just wanted to share this, in case somebody faced the same problem.

Answer by sopel for Using ls to list directories and their total sizes


du -S  

du have another useful option: -S, --separate-dirs telling du not include size of subdirectories - handy on some occasions.

Example 1 - shows only the file sizes in a directory:

du -Sh  *   3,1G    10/CR2  280M    10  

Example 2 - shows the file sizes and subdirectories in directory:

du -h  *   3,1G    10/CR2   3,4G    10  

Answer by Martin Wilde for Using ls to list directories and their total sizes


just a warning, if you want to compare sizes of files. du produces different results depending on file system, block size, ... .

It may happen that the size of the files is different, e.g. comparing the same directory on your local hard-disk and a USB mass storage device. I use the following script, including ls to sum up the directory size. The result in in bytes taking all sub directories into account.

echo "[GetFileSize.sh] target directory: \"$1\""    iRetValue=0    uiLength=$(expr length "$1")  if [ $uiLength -lt 2 ]; then    echo "[GetFileSize.sh] invalid target directory: \"$1\" - exiting!"    iRetValue=-1  else    echo "[GetFileSize.sh] computing size of files..."      # use ls to compute total size of all files - skip directories as they may    # show different sizes, depending on block size of target disk / file system    uiTotalSize=$(ls -l -R $1 | grep -v ^d | awk '{total+=$5;} END {print total;}')    uiLength=$(expr length "$uiTotalSize")    if [ $uiLength -lt 1 ]; then      uiTotalSize=0    fi    echo -e "[GetFileSize.sh] total target file size: \"$uiTotalSize\""    fi    exit "$iRetValue"  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.