How to Find File and Folder Size Under Linux?

Source: Internet
Author: User
Keywords linux file size linux folder size how to check the folder size in linux
1. View file and folder size under Linux

2. Delete system logs, etc.

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.

Scenario: When performing automatic deployment in sts, maven prompts No space left on device error, and later it is found that the disk space is full. The following method is used to analyze and find that the logs directory under tomcat takes up a lot of space and delete extra logs problem solved!


Back to the top
1 View file and folder size under Linux

When the disk size exceeds the standard, there will be an alarm prompt, then if you master the df and du commands is a very wise choice.

 df can check the size of the first-level folder, the use ratio, the file system and its mount point, but it can't do anything with the file.

 du can view the size of files and folders.

  The combination of the two is very effective. For example, use df to check which level one directory is too large, and then use df to check the size of the folder or file, so you can quickly determine the crux.

  Brief introduction below

1.1 df command
//Can be used to view the file system of the partition
df -T
 The df command can display the current free space and usage of all file systems. Please see the following example:
df -h
 The parameter -h means to use "Human-readable" output, that is, to use GB, MB and other easy-to-read formats in the file system size.

    The first field (Filesystem) and the last field (Mounted on) of the above command output are the file system and its mount point, respectively. We can see that the partition /dev/sda1 is hung in the root directory.

    The next four fields Size, Used, Avail, and Use% are the capacity of the partition, the used size, the remaining size, and the percentage used. Under FreeBSD, when the hard disk capacity is full, you may see that the used percentage exceeds 100%, because FreeBSD will leave some space for root, so that when the file system is full, root can still write to the file system. For management.

1.2 du command
ps: actual combat experience, use the two together
//Check the usage of files in the system
df -h
//Check the space occupied by each file and directory under the current directory
du -sh *
//Method 1: Switch to the directory to be deleted and delete all files in the directory
rm -f *
//Method 2: Delete all files in the logs folder without deleting the folder itself
rm -rf log/*
The following deletion methods have not yet been tried.
ls *.log | xargs rm -f
 du: Query the disk usage of files or folders

    If there are many files and folders in the current directory, use the command without the parameter du to cycle through the space used by all files and folders. This is unfavorable for checking whether the place is too large, so you have to specify the number of layers in the deep directory, the parameter: --max-depth=, this is an extremely useful parameter!

As follows, pay attention to the use of "*" to get the size of the used space of the file.

    Reminder: The FreeBSD command has always been more complex than linux, but its du command specifies the number of depths of the directory is simplified than linux, which is -d.
du -h --max-depth=1 /home

What are the similarities and differences between the following command and the above command? ?

Answer: du -h --max-depth=1 /home only lists the file sizes of all the first-level directories under the home directory;

du -h --max-depth=1 /home/* List the size of the first-level directory files of all first-level directories under home.
du -h --max-depth=1 /home/*
Received an alarm, Linux space utilization rate>95%
[root@www.linuxidc.com]# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 10080520 9112968 455484 96% /
none 1977584 0 1977584 0% /dev/shm
/dev/cciss/c0d0p5 28842748 6706924 20670700 25% /u01
/dev/cciss/c0d0p6 26850172 683148 24803108 3% /u02
But using du -kxs check, the root directory usage rate is 80%

du Statistics file size add up

df statistics data block usage

If a process is opening a large file and the large file is directly dropped by rm or mv, du will update the statistical value, df will not update the statistical value, and it is still considered that the space has not been released. Until the process of opening large files is Killed.

The problem we encountered is that we regularly delete the files under /var/spool/clientmqueue, but the process is not killed, so the space has not been released.

After killing the process using the following command, the system resumes.
fuser -u /var/spool/clientmqueue
1.4 Check the size of the Linux file directory and the number of files in the folder
    Statistics total size

    du -sh xmldb/

    du -sm * | sort -n //Statistics of the current directory size and install size sort

    du -sk * | sort -n

    du -sk * | grep guojf //Look at the size of a person

    du -m | cut -d "/" -f 2 //Look at the text before the second/ character

    See how many files are in this folder /*/*/* how many files are there

    du xmldb/

    du xmldb/*/*/* |wc -l

    40752

    Explanation:

    wc [-lmw]

    Parameter Description:

    -l: how many lines

    -m: how many characters

    -w: how many words

1.5 Linux: ls view the file size in K, M, G units
ps: Pay attention to the use of man command, press "q" key to exit man query.
#man ls
...
-h, --human-readable
                print sizes in human readable format (e.g., 1K 234M 2G)
...
# ls
cuss.war nohup.out
# ls -l
total 30372
-rw-r--r-- 1 root root 31051909 May 24 10:07 cuss.war
-rw------- 1 root root 0 Mar 20 13:52 nohup.out
# ls -lh
total 30M
-rw-r--r-- 1 root root 30M May 24 10:07 cuss.war
-rw------- 1 root root 0 Mar 20 13:52 nohup.out
# ll -h
total 30M
-rw-r--r-- 1 root root 30M May 24 10:07 cuss.war
-rw------- 1 root root 0 Mar 20 13:52 nohup.out
back to the top

2 Delete system logs, etc.

Delete generated core, mbox and other files

#find / -name core|xargs rm –rf

Delete log
#rm -rf /var/log/*
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.