ls -l --block-size=M will give you a long format listing (needed to actually see the file size) and round file sizes up to the nearest MiB.
If you want MB (10^6 bytes) rather than MiB (2^20 bytes) units, use --block-size=MB instead.
If you don't want the M suffix attached to the file size, you can use something like --block-size=1M. Thanks Stéphane Chazelas for suggesting this.
This is described in the man page for ls; man ls and search for SIZE. It allows for units other than MB/MiB as well, and from the looks of it (I didn't try that) arbitrary block sizes as well (so you could see the file size as number of 412-byte blocks, if you want to).
Note that the --block-size parameter is a GNU extension on top of the Open Group's ls, so this may not work if you don't have a GNU userland (which most Linux installations do). The ls from GNU coreutils 8.5 does support --block-size as described above.
I prefer this command ll -sha
.
There is also a great ncdu
utility - it can show directory size with detailed info about subfolders and files.
Ubuntu:
$ sudo apt-get install ncdu
Just type ncdu [path]
in the command line. After a few seconds for analyzing the path, you will see something like this:
$ ncdu 1.11 ~ Use the arrow keys to navigate, press ? for help
--- / ---------------------------------------------------------
. 96,1 GiB [##########] /home
. 17,7 GiB [# ] /usr
. 4,5 GiB [ ] /var
1,1 GiB [ ] /lib
732,1 MiB [ ] /opt
. 275,6 MiB [ ] /boot
198,0 MiB [ ] /storage
. 153,5 MiB [ ] /run
. 16,6 MiB [ ] /etc
13,5 MiB [ ] /bin
11,3 MiB [ ] /sbin
. 8,8 MiB [ ] /tmp
. 2,2 MiB [ ] /dev
! 16,0 KiB [ ] /lost+found
8,0 KiB [ ] /media
8,0 KiB [ ] /snap
4,0 KiB [ ] /lib64
e 4,0 KiB [ ] /srv
! 4,0 KiB [ ] /root
e 4,0 KiB [ ] /mnt
e 4,0 KiB [ ] /cdrom
. 0,0 B [ ] /proc
. 0,0 B [ ] /sys
@ 0,0 B [ ] initrd.img.old
@ 0,0 B [ ] initrd.img
@ 0,0 B [ ] vmlinuz.old
@ 0,0 B [ ] vmlinuz
Delete the currently highlighted element with d, exit with CTRL + c
I do the following all the time:
$ du -sh backup-lr-May-02-2017-1493723588.tar.gz
NB:
-s, --summarize
display only a total for each argument
-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)
You have to differenciate between file size and disk usage. The main difference between the two comes from the fact that files are "cut into pieces" and stored in blocks.
Modern block size is 4KiB, so files will use disk space multiple of 4KiB, regardless of how small they are.
If you use the command stat
you can see both figures side by side.
stat file.c
If you want a more compact view for a directory, you can use ls -ls
, which will give you usage in 1KiB units.
ls -ls dir
Also du
will give you real disk usage, in 1KiB units, or dutree with the -u
flag.
Example: usage of a 1 byte file
$ echo "" > file.c
$ ls -l file.c
-rw-r--r-- 1 nacho nacho 1 Apr 30 20:42 file.c
$ ls -ls file.c
4 -rw-r--r-- 1 nacho nacho 1 Apr 30 20:42 file.c
$ du file.c
4 file.c
$ dutree file.c
[ file.c 1 B ]
$ dutree -u file.c
[ file.c 4.00 KiB ]
$ stat file.c
File: file.c
Size: 1 Blocks: 8 IO Block: 4096 regular file
Device: 2fh/47d Inode: 2185244 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ nacho) Gid: ( 1000/ nacho)
Access: 2018-04-30 20:41:58.002124411 +0200
Modify: 2018-04-30 20:42:24.835458383 +0200
Change: 2018-04-30 20:42:24.835458383 +0200
Birth: -
In addition, in modern filesystems we can have snapshots, sparse files (files with holes in them) that further complicate the situation.
You can see more details in this article: understanding file size in Linux
Go to the chosen directory and execute:
$ du -d 1 -h
where:
-d 1 is the depth of the directories
-h is the human-readable option
You'll see like that:
0 ./proc
8.5M ./run
0 ./sys
56M ./etc
12G ./root
33G ./var
23M ./tmp
3.2G ./usr
154M ./boot
26G ./home
0 ./media
0 ./mnt
421M ./opt
0 ./srv
2.6G ./backups
80G .
ls -sh video.mp4 | sed s/video.mp4//g
output, 5.6M
If you are using it in a script, use stat
.
$ date | tee /tmp/foo
Wed Mar 13 05:36:31 UTC 2019
$ stat -c %s /tmp/foo
29
$ ls -l /tmp/foo
-rw-r--r-- 1 bruno wheel 29 Mar 13 05:36 /tmp/foo
That will give you size in bytes. See man stat
for more output format options.
The OSX/BSD equivalent is:
$ date | tee /tmp/foo
Wed Mar 13 00:54:16 EDT 2019
$ stat -f %z /tmp/foo
29
$ ls -l /tmp/foo
-rw-r--r-- 1 bruno wheel 29 Mar 13 00:54 /tmp/foo
To get the total size of directory or the total size of file use,
du -csh <directory or filename*> | grep total
File Size in MB
ls -l --b=M filename | cut -d " " -f5
File Size in GB
ls -l --b=G filename | cut -d " " -f5
You can use:
ls -lh
Using this command you'll see the apparent space of the directory and true space of the files and in details the names of the files displayed, besides the size and creation date of each.
Use ls command with -h argument: [root@hots19 etc]# ls -lh
h : for human readable.
Exemple:
[root@CIEYY1Z3 etc]# ls -lh
total 1.4M
-rw-r--r--. 1 root root 44M Sep 15 2015 adjtime
-rw-r--r--. 1 root root 1.5K Jun 7 2013 aliases
-rw-r--r-- 1 root root 12K Nov 25 2015 aliases.db
drwxr-xr-x. 2 root root 4.0K Jan 11 2018 alternatives
-rw-------. 1 root root 541 Jul 8 2014 anacrontab
-rw-r--r--. 1 root root 55M Sep 16 2014 asound.conf
-rw-r--r--. 1 root root 1G Oct 6 2014 at.deny
I'm a Ubuntu 16.04 user myself and I find that the ll
command is by far the easiest way to see a directory's contents. I've noticed that not all Linux distributions support this command, but there's probably a workaround/install for each distro out there.
Example:
user@user-XPS-15-9560:/$ ll
total 188
drwxr-xr-x 27 root root 4096 Jan 26 09:13 ./
drwxr-xr-x 27 root root 4096 Jan 26 09:13 ../
drwxr-xr-x 2 root root 4096 Jan 22 15:13 bin/
drwxr-xr-x 4 root root 12288 Jan 29 11:35 boot/
drwxr-xr-x 2 root root 4096 Sep 3 18:14 cdrom/
drwxr-xr-x 20 root root 4440 Feb 5 08:43 dev/
drwxr-xr-x 153 root root 12288 Feb 2 15:17 etc/
drwxr-xr-x 4 root root 4096 Sep 3 18:15 home/
...
The biggest advantage for me is that it's quick and really intuitive to use.
UPDATE: what I didn't know was that on Ubuntu it's a pre-configured alias. You can easily set it yourself by executing alias ll="ls -la"
on the command line, or by adding this entry in your .bashrc config file:
sudo nano ~/.bashrc
...add line described above and save file by pressing Ctrl+X and Y...
source ~/.bashrc
you can use ls -sh in linux you can do sort also you need to go to dir where you want to check the size of files
All you need is -l
and --block-size
flags
Size of all files and directories under working directory (in MBs)
ls -l --block-size=M
Size of all files and directories under working directory (in GBs)
ls -l --block-size=G
Size of a specific file or directory
ls -l --block-size=M my_file.txt
ls -l --block-size=M my_dir/
ls --help
-l
use a long listing format
--block-size=SIZE
: scale sizes bySIZE
before printing them; e.g.,'--block-size=M'
prints sizes in units of 1,048,576 bytes; seeSIZE
format below
SIZE
is an integer and optional unit (example: 10M is 10*1024*1024). Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).
go to specific directory then run below command
# du -sh *
4.0K 1
4.0K anadb.sh --> Shell file
4.0K db.sh/ --> shell file
24K backup4/ --> Directory
8.0K backup6/ --> Directory
1.9G backup.sql.gz --> sql file
Use ls -s
to list file size, or if you prefer ls -sh
for human readable sizes.
For directories use du
, and again, du -h
for human readable sizes.
You can use below command to get list of files in easily human readable format.
ls -lrtsh
du -sh [file_name]
works perfectly to get size of a particular file.
There is du
command.
Size of a directory and/or file, in a human-friendly way:
$ du -sh .bashrc /tmp
I memorised it as a non-existent English word dush.
--apparent-size
command line switch makes it measure apparent sizes (what ls
shows) rather than actual disk usage.
Source: Stackoverflow.com