How to Find the File Size in Unix?

Source: Internet
Author: User
Keywords unix file size how to find the file size in unix how to sort files by size in unix

When running cases with irunner today, there are too many cases and unix home quotas are exceeded, and errors will be reported when running cases.

Use ll | grep Sep to view all files in the current directory, sep means month

Use du -sh * to view the current file size

df -k View home usage

irunner will generate simfab, .wm and .lsbatch files under my home, these files will continue to grow and add up a lot

du -h

du -h --max-depth=1 View a layer of commands

Alibaba Cloud Simple Application Server:  Anti COVID-19 SME Enablement Program
$300 coupon package for all new SMEs and a $500 coupon for paying customers.

Common commands

du -h –max-depth=1 |grep [TG] |sort #Find the directories of G and T and sort them
du -sh # count the size of the current directory and display it in an intuitive way

du -h –max-depth=1 |grep ‘G’ |sort #View the G directory and sort
du -sh --max-depth=1 #View the size of all first-level sub-directory folders under the current directory
du -h --max-depth=1 |sort #View the size of all first-level sub-directory folders under the current directory and sort

du -h –max-depth=1 |grep [TG] |sort -nr #reverse order

This is the case under FreeBSD
du -hd 1 | sort
du -hd 1 |grep [GT] | sort

Du command function description: count the size of the disk space occupied by the directory (or file).
Syntax: du [-abcDhHklmsSx] [-L <symbolic link>][-X <file>][–block-size][–exclude=<directory or file>] [–max-depth=<number of directory levels>] [–Help][–version][directory or file]

Common parameters:

-a or -all displays the disk usage for each specified file, or displays the respective disk usage for each file in the directory.
-b or -bytes When displaying the directory or file size, use bytes as the unit.
-c or –total In addition to displaying the size of a directory or file, it also displays the total of all directories or files.
-D or -dereference-args displays the size of the source file of the specified symbolic link.
-h or -human-readable Take K, M, G as the unit to improve the readability of information.
-H or -si is the same as the -h parameter, but K, M, and G are converted in units of 1000 instead of 1024.
-k or -kilobytes is in units of 1024 bytes.
-l or -count-links repeatedly count the files connected by the hardware.
-L<symbolic link> or --dereference<symbolic link> displays the source file size of the symbolic link specified in the option.
-m or -megabytes in units of 1MB.
-s or –summarize displays only the total, which is the size of the current directory.
-S or –separate-dirs When displaying the size of each directory, it does not include the size of its subdirectories.
-x or -one-file-xystem is based on the file system at the beginning of processing, and skips if it encounters a different file system directory.
-X<file> or –exclude-from=<file> Specify a directory or file in <file>.
–Exclude=<directory or file> Skip the specified directory or file.
–Max-depth=<number of directory levels> If the directory exceeds the specified number of levels, it will be ignored.
–Help Display help.
–Version Display version information.

Example of du command usage in linux:

1> To display the disk usage of a directory tree and each subtree
du /home/linux
This shows the number of disk blocks in the /home/linux directory and each of its subdirectories.
2> To display the disk usage of a directory tree and each subtree in units of 1024 bytes
du -k /home/linux
This shows the number of 1024-byte disk blocks in the /home/linux directory and each of its subdirectories.
3> Display the disk usage of a directory tree and each subtree in MB
du -m /home/linux
This shows the number of MB disk blocks in the /home/linux directory and each of its subdirectories.
4> Display the disk usage of a directory tree and each subtree in GB
du -g /home/linux
This shows the number of GB disk blocks in the /home/linux directory and each of its subdirectories.
5>View the size of all directories and subdirectories in the current directory:
du -h.
"." represents the current directory. It can also be replaced with a clear path
-h means to display in the humanized form of K, M, G
6>Check the size of the user directory in the current directory, and don’t want to look at other directories and their subdirectories:
du -sh user
-s means summary, that is, only one summary value is listed
du -h --max-depth=0 user
–Max-depth=n means only go deep into the nth level directory, here is set to 0, which means not go deep into the subdirectory.
7> List the size of all directories and files under the user directory and its subdirectories:
du -ah user
-a means including directories and files
8>List the size of the directory whose name does not include the xyz string in the current directory:
du -h –exclude=’xyz’
9>I want to list more information about the size of the user directory and subdirectories on one screen:
du -0h user
-0 (bar zero) means that every time the information of a directory is listed, the information of the next directory is directly output instead of a new line.
10>Only display all disk usage of a directory tree

[maple@linux ~] du
8 ./test/links
8 ./test/dir/subdir1
8 ./test/dir/subdir2
20 ./test/dir
160 ./test
108 ./test2
1492.
[maple@linux ~]$
[maple@linux ~] du -0
8 ./test/links8 ./test/dir/subdir18 ./test/dir/subdir220 ./test/dir160 ./test108 ./test21492

[maple@linux ~]$ du -c

8 ./test/links
8 ./test/dir/subdir1
8 ./test/dir/subdir2
20 ./test/dir
160 ./test
108 ./test2
1492.
1492 total
[maple@linux ~]$
[maple@linux ~] du -h

8.0K ./test/links
8.0K ./test/dir/subdir1
8.0K ./test/dir/subdir2
20K ./test/dir
160K ./test
108K ./test2
1.5M.
[maple@linux ~] du -k
8 ./test/links
8 ./test/dir/subdir1
8 ./test/dir/subdir2
20 ./test/dir
160 ./test
108 ./test2
1492.
[maple@linux ~] du -sh
1.5M.
[maple@linux ~] du -S
8 ./test/links
8 ./test/dir/subdir1
8 ./test/dir/subdir2
4 ./test/dir
132 ./test
108 ./test2
1224.
[maple@linux ~] du -Sh –exclude=”sub*”
8.0K ./test/links
4.0K ./test/dir
132K ./test
108K ./test2
1.2M.
[maple@linux ~] du -h
8.0K ./test/links
8.0K ./test/dir/subdir1
8.0K ./test/dir/subdir2
20K ./test/dir
160K ./test
108K ./test2
1.5M.
[maple@linux ~]$

Command purpose

The du(disk usage) command can calculate the disk space occupied by a file or directory. When no option is specified, it will measure the current working directory and all its subdirectories, and display the fast numbers occupied by each directory respectively, and finally display the total fast numbers occupied by the working directory.
Command format
du [OPTION]... [FILE]...
-a, –all
Include all files, not just directories
–Apparent-size
print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due
to holes in (’sparse’) files, internal fragmentation, indirect blocks, and the like
-B, --block-size=SIZE use SIZE-byte blocks
-b, --bytes take bytes as the unit of calculation
-k is calculated in kilobytes (KB)
-m is calculated in megabytes (M)
-c, --total add a total at the end (system default)
-D, --dereference-args
dereference FILEs that are symbolic links
-H has the same effect as -si.
-h, --human-readable Output file size information in a comparative reading mode (for example, 1K 234M 2G). Note: This option is also valid in many other commands (df, ls).
-Si has the same effect as -h, but uses 1000 as the conversion unit
-l, --count-links count all file sizes, and count multiple times for hard link files.
-L, –dereference display the size of the source file of the symbolic link specified in the option.
-P, --no-dereference do not follow any symbolic connection (default)
-S, -separate-dirs does not include the size of subdirectories when calculating the space occupied by directories.
-s, --summarize only show the total space occupied by the working directory
-x, –one-file-system is based on the file system at the beginning of processing, and skipped if it encounters a different file system directory.
-X FILE, --exclude-from=FILE exclude the specified FILE
–Exclude=PATTERN exclude files that match the style, Pattern is the ordinary Shell style,? Represents any one character, * represents any number of characters.
--Max-depth=N
Only list the information of directories and files whose depth is less than max-depth --max-depth=0, the effect is the same as --s
Use Cases
root@ubuntu:/# cd /home/web/
root@ubuntu:/home/web# du -s
793832 .– Calculate the amount of space occupied by the current directory without specifying the FILE name.
root@ubuntu:/# du –max-depth=1 -h
-Output the space used by each subdirectory in the current directory
83M ./java
87M ./build
197M ./jboss
128M ./lib
1.1M ./bin
52K ./synclogs
4.8M ./sql
920K ./conf
52K ./logs
20K ./mail_group
56K ./.svn
144M ./htdocs
56K ./jboss-conf
2.7M ./auto-conf
8.0K ./.mule
23M ./classes
43M ./templates
144K ./project
776M.
Find large files
When the disk space is exhausted, it is inevitable to clean up. For example, if the /home directory is too large, you can use the following command to see who it is:
du -s /home/* | sort -nr

[root@SVN svnhome]# du -h –max-depth=1 |grep [TG] |sort -nr
518G ./sichuandian
474G ./zgx
162G ./20kc
98G ./bgong
80G ./s
73G ./yn
44G ./siua12
35G ./cdtielug13
31G ./kifa
22G ./zhhu
22G ./shae1
18G ./eCaker
17G ./1-youswu
16G ./20kc
14G ./sicngda
14G ./b
e11
6.5G ./service
5.4G ./YNmeeting
5.2G ./YNkangjia
5.0G ./1-1hongqinshan
4.6G ./doucaiku
4.6G ./bunopertion
3.7G ./naax11
2.6G ./yneries
2.6G ./teamals
2.4G ./sichxue
2.4G ./1cm
1.8G ./Yb
1.7T.
1.7G ./ynedate
1.3G ./cddshe
1.2G ./YmD
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.