Why does du give different results? - linux

I use du command very often. Recently, I used it to find out 5 heaviest files in root directory of my server, I used the following command:
sudo du -ah / | sort -nr | head -n 5
Result was:
1016K /var/cache/apt/archives/fonts-dejavu-core_2.35-1_all.deb
1016K /bin/bash
1008K /usr/src/linux-aws-headers-4.4.0-1052/fs
1008K /usr/src/linux-aws-headers-4.4.0-1049/fs
1004K /var/awslogs/lib/python2.7/site-packages/botocore/data/ec2/2016-09-15/
I then removed -h, and observed an entirely different result:
sudo du -a / | sort -nr | head -n 5
Result:
2551396 /
1189240 /usr
894000 /var
541836 /usr/lib
406276 /var/lib
From the man page of du,
-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)
According to my understanding, including or excluding of -h should not really cause change in the results, just the size format.
Could you help me understand why this would happen?

1016K is numerically greater than 2M. sort -n just extracts 1016 and 2.
Try sort -h if your sort supports it.

The sort command does not take K/M/G into account, and sorts all "K" before all "M".

Probably whith -h your directories' sizes are something like 2G or 4G, while files remain 1016K or 1008K, and 2 is smaller than 1016 hence directories get sorted out to the end of a list.
Without -h total sizes of directories (actually: directory trees) appear clearly greater than sizes of files inside, hence directories are sorted at the top of a list.

Related

Count directories and subdirectories

I want to combine directories and sub-directories and sum-up the first column as follows:
original output:
8 ./.g/apps/panel/mon/lt/prefs
12 ./.g/apps/panel/mon/lt
40 ./.g/apps/panel/mon
44 ./.g/apps/panel
88 ./.g/apps
112 ./.g
4 ./.g
4 ./.pof
20 ./.local/share/applications
4 ./.local/share/m/packages
8 ./.local/share/m
4 ./.local/share/Trash/info
4 ./.local/share/Trash/files
12 ./.local/share/Trash
44 ./.local/share
new output:
308 ./.g
4 ./.pof
96 ./.local/share
the original command: du -k, and I'm trying with awk and cut commands but fails.
edit:- I got up to here:
du -k | awk '{print $1}' | cut -d "/" -f 1
Now, I'm struggling to merge similar lines and sum-up the first column.
p.s this is just an output example*
thank you.
Use du -d 1 to list accumulative content of 1 directory bellow current.
du -h -d 1
Provide a human readable count.
You can try with command:
du -sh *
Try
du -sk .g .pof .local/share
The -s switch is summary, that is, du will search all the files, all the way down the folders inside, and report just the grand total. (The -k switch print the size in kilobytes; thanks Romeo Ninov).
You have to manually specify each folder you want to know the grand total of.
If you type, for example
du -sk .
it will output just a single number, accounting for the current folder (and below) file sizes.
If you type
du -sk *
the result will depend on what your shell expands * to (usually all the files and folders not starting with a dot (.) in the current folder).

how do I access or sort the files of a directory in another order that the alphabetical order?

I run the following commands in linux on a pdf file to convert its pages to image files. However, it runs twice over the pdf file
pdftoppm -H 700 -f 30 -l 40 -png rl.pdf top
pdftoppm -y 700 -f 30 -l 40 -png rl.pdf bottom
output would be (the list of output files):
bottom-001.png
bottom-002.png
top-001.png
top-002.png
However, I want to access and process them in the following order (for ffmpeg):
top-001.png
bottom-001.png
top-002.png
bottom-002.png
To reach this goal you may suggest another way for naming the output files or run another script on the output files to sort them out.
sort -n -t- -s -k2
Sort numerically using - as separator on the second field. Stable sort so that top is on top.
Alternatively sort the first field in reverse:
sort -t- -k2n -k1r
For example the following command:
echo 'bottom-001.png
bottom-002.png
top-001.png
top-002.png' | sort -t- -k2n -k1r
outputs:
top-001.png
bottom-001.png
top-002.png
bottom-002.png
Another solution in this case is adding a suffix (in alphabetical order) to the output files of each command and move them to a new directory:
pdftoppm -H 450 -f 30 -l 40 -png rl.pdf page
for file in *.png; do
mv "$file" "out/${file%.png}_a.png"
done
pdftoppm -y 700 -f 30 -l 40 -png rl.pdf page
for file in *.png; do
mv "$file" "out/${file%.png}_b.png"
done
Variants of sorting with ls command
$ ls --help
...
-r, --reverse reverse order while sorting
-S sort by file size, largest first
--sort=WORD sort by WORD instead of name: none (-U), size (-S),
time (-t), version (-v), extension (-X)
-t sort by modification time, newest first
-u with -lt: sort by, and show, access time;
with -l: show access time and sort by name;
otherwise: sort by access time, newest first
-U do not sort; list entries in directory order
-v natural sort of (version) numbers within text
-X sort alphabetically by entry extension

List all directories sorted by size in descending order

I have a requirement to sort all directories of current directory in descended order by size.
I tried following
du -sh * | sort -rg
It is listing all the folders by size but it's just listing by size of folder by values. However it's not sorting correcting. 100 MB Dir should be listed before 200KB.
Any help will be appreciable.
-g is for floats. For human-readable output use human-readable sort:
du -sh * | sort -rh
If you have numfmt utility from coreutils, you can use numeric sort with numfmt formatting:
du -s * | sort -rn | numfmt --to=iec -d$'\t' --field=1
I prefer to just go straight to comparing bytes.
du -b * | sort -nr
du -b reports bytes.
sort -n sorts numerically. Obviously, -r reverses.
My /tmp before I clean it -
104857600 wbxtra_RESIDENT_07202018_075931.wbt
815372 wbxtra_RESIDENT_07192018_075744.wbt
215310 Slack Crashes
148028 wbxtra_RESIDENT_07182018_162525.wbt
144496 wbxtra_RESIDENT_07182018_163507.wbt
141688 wbxtra_RESIDENT_07182018_161957.wbt
56617 Notification Cache
20480 ~DFFA6E4895E749B423.TMP
16384 ~DF543949D7B4DF074A.TMP
13254 AdobeARM.log
3614 PhishMeOutlookReporterLoader.log
3448 msohtmlclip1/01
3448 msohtmlclip1
512 ~DF92FFF2C02995D884.TMP
28 ExchangePerflog_8484fa311d504d0fdcd6c672.dat
0 WPDNSE
0 VPMECTMP
0 VBE
Don't ask the machine to process human data:
du -s * | sort -rg

Get size of image in bash

I want to get size of image. The image is in folder by name encodedImage.jpc
a="$(ls -s encodedImage.jpc | cut -d " " -f 1)"
temp="$(( $a*1024 * 8))"
echo "$a"
The output is not correct. How to get size? Thank You
Better than parsing ls output, the proper way is to use the command stat like this :
stat -c '%s' file
Check
man stat | less +/'total size, in bytes'
If by size you mean bytes or pretty bytes can you just use
ls -lh
-h When used with the -l option, use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the number of digits to three or less using base 2 for sizes.
I guess the more complete answer if you're just trying to tear off the file size alone (I added the file name as well you can remove ,$9 to drop it)
ls -lh | awk '{print $5,$9}'
U can use this command
du -sh your_file

Sorting folder size not working - RedHat Linux

I am using du -h --max-depth=2 to get list of folders by size upto 2 levels down. Problem is I am not able to sort them in max folder size.
Normally this can be achieved by using du -k | sort -nr * but I can't use it here since it conflicts (the -s argument) with the --max-depth=2 argument.
Can someone please suggest if there is any other way to achieve this. I need to keep the list in human readable format though.
478M var
500K usr/sdfijf/best
568K user/dbuser
56M etc/basic/linux
68K var/redhat
9.0K user/aunix
I have following two columns and was expecting to sort them by the human readable folder size with biggest folders being on top (so descending and reverse order basically)
Thanks,
Morgan
Use the -h flag to sort:
-h, --human-numeric-sort
compare human readable numbers (e.g., 2K 1G)
So du -h --max-depth=2 | sort -hr.
One of the ways could be du -k --max-depth=2 | sort -nr |awk '{ print $2}'|xargs du -sh but not sure if it is efficient

Resources