shell学习之查找当前目录下文件以及文件夹大小-'du'命令和'df'命令

经过前几天的磁盘写满的事件之后,发现自己对du这个命令完全不知道,所以下决心要学习一番。

我就当一盘搬运工吧,外网找到一个教程

‘du‘就是查找文件夹的大小

du

Typing the above at the prompt gives you a list of directories that exist in the current directory along with their sizes. The last line of the output gives you the total size of the current directory including its subdirectories. The size given includes the sizes of the files and the directories that exist in the current directory as well as all of its subdirectories. Note that by default the sizes given are in kilobytes.

译:就是说吧当前的文件夹下的目录的size打印出来,在最后一行打印的是整个文件夹包括其子文件夹的总size,size包含目当前录下的所有文件、子目录。需要注意的是,默认情况下单位是kb

du /home/sunyuw

The above command would give you the directory size of the directory /home/sunyuw

这个就不翻了,部分输出如下:

528    ./.mozilla/firefox/reo52zta.default/storage/persistent
532    ./.mozilla/firefox/reo52zta.default/storage
13220    ./.mozilla/firefox/reo52zta.default/adblockplus
38788    ./.mozilla/firefox/reo52zta.default
38812    ./.mozilla/firefox
4    ./.mozilla/extensions
38820    ./.mozilla
2080040    .

du -h

This command gives you a better output than the default one. The option ‘-h‘ stands for human readable format. So the sizes of the files / directories are this time suffixed with a ‘k‘ if its kilobytes and ‘M‘ if its Megabytes and ‘G‘ if its Gigabytes.

译:这个命令输出的要比默认的好的多(更加贴近人类的阅读方式).选项‘-h‘代表‘人类可读的格式’.所以这个时候文件和文件夹的size被以合适的单位作为后缀,比如K、M、G分别对应kb、mb、gb

部分输出是这样的:

13M    ./.mozilla/firefox/reo52zta.default/adblockplus
38M    ./.mozilla/firefox/reo52zta.default
38M    ./.mozilla/firefox
4.0K    ./.mozilla/extensions
38M    ./.mozilla
2.0G    .

du -ah

This command would display in its output, not only the directories but also all the files that are present in the current directory. Note that ‘du‘ always counts all files and directories while giving the final size in the last line. But the ‘-a‘ displays the filenames along with the directory names in the output. ‘-h‘ is once again human readable format.

译:(楼主亲测)这个命令会将整个当前文件夹下的文件全部都遍历输出出来.需要注意的是du总会将所有文件总和size输出在最后一行,但因为-a参数的原因,文件名将会单独于文件输出,但是-h仍然会输出人类可读的格式,部分输出如下:

32K    ./self-learning/intro-linux/sect_03_01.html
8.0K    ./self-learning/intro-linux/sect_07_08.html
12K    ./self-learning/intro-linux/sect_06_01.html
12K    ./self-learning/intro-linux/sect_05_01.html
12K    ./self-learning/intro-linux/sect_01_05.html
800K    ./self-learning/intro-linux/intro-linux.html
8.0K    ./self-learning/intro-linux/sect_05_05.html
16K    ./self-learning/intro-linux/sect_07_01.html
2.7M    ./self-learning/intro-linux
9.4M    ./self-learning
309M    .

du -c

This gives you a grand total as the last line of the output. So if your directory occupies 30MB the last 2 lines of the output would be

译:这个将会再最后添加一个total这么一行,所以如果文件夹占据了30mb空间那么在最后两行都会显示

输出如下:

305328    ./jumei
800    ./bigchance
284    ./self-learning/Bash-Beginners-Guide/images
1692    ./self-learning/Bash-Beginners-Guide
2920    ./self-learning/advanced bash-guide
752    ./self-learning/intro-linux/images
2748    ./self-learning/intro-linux
9620    ./self-learning
315752    .
315752    total

du -ch | grep total

这个,呵呵不翻译了,不解释了,看不懂面壁去


du -s

This displays a summary of the directory size. It is the simplest way to know the total size of the current directory.

译:这个会输出一个概要,是获取当前文件夹size最简单的方法

输出:

315752    .

du -S

This would display the size of the current directory excluding the size of the subdirectories that exist within that directory. So it basically shows you the total size of all the files that exist in the current directory.

译:(楼主亲测)这个会每个罗列文件以及文件夹的大小,这个是查询本文件夹下直接的files的总size的方法

输出如下:

305328    ./jumei
800    ./bigchance
284    ./self-learning/Bash-Beginners-Guide/images
1408    ./self-learning/Bash-Beginners-Guide
2920    ./self-learning/advanced bash-guide
752    ./self-learning/intro-linux/images
1996    ./self-learning/intro-linux
2260    ./self-learning
4    .

注意下,这里的当前文件夹已经没有被计算sub file或者sub dir的size了。


du --exculde=mp3

The above command would display the size of the current directory along with all its subdirectories, but it would exclude all the files having the given pattern present in their filenames. Thus in the above case if there happens to be any mp3 files within the current directory or any of its subdirectories, their size would not be included while calculating the total directory size.

译:这条命令将会统计当前目录下以及子目录的size,但是将会忽略能够匹配表达式的文件。(后面不翻译了,就是说mp3在这个例子里面不成)

楼主没有mp3放在linux里面的,所以输出就部署出了吧,格式参照默认的du命令



接下来是df命令了,查看磁盘使用率

df

Typing the above, outputs a table consisting of 6 columns. All the columns are very easy to understand. Remember that the ‘Size‘, ‘Used‘ and ‘Avail‘ columns use kilobytes as the unit. The ‘Use%‘ column shows the usage as a percentage which is also very useful.

译:输出的表格具有6个元素,每个元素都很容易理解,请记住‘Size‘,‘Used‘,‘Avail‘元素以kb为单位,‘Use%‘元素以百分比的形式显示了使用率(这个是有用的哟).

不太全,但是先凑合着发布吧,哈哈

shell学习之查找当前目录下文件以及文件夹大小-'du'命令和'df'命令

时间: 2024-08-23 23:33:31

shell学习之查找当前目录下文件以及文件夹大小-'du'命令和'df'命令的相关文章

php简单的查找当前目录下的非法文件

<?php /** *只遍历当前目录下的文档 *@return bool */ $new = microtime(); function read_all_file($path) { if(empty($path)) { return false; } $path = rtrim($path,'/'); static $file_arr = array(); if(! is_readable($path)) { echo $path.'无权限读取文件或文件夹不存在'; return false;

ubuntu 用shell脚本实现将当前目录下所有目录中的某一类文件拷贝到同一目录下

当前目录下有一些文件和目录,其中每个目录里都有若干.txt文件, 现在要求在当前目录创建一个新目录all,且将那些目录所有.txt文件 都拷贝到目录all.在ubuntu12.04的shell脚本实现如下: #!/bin/sh # 提示信息 echo "start:" # 定义变量 dst=all pst=.txt # 复制文件到目标文件夹 if [ -d ${dst} ] then echo "${dst} existed" else echo "mkd

查找目录下的所有文件中是否含有某个字符串 linux

查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xargs grep -ri "IBM" -l 1.正则表达式    (1)正则表达式一般用来描述文本模式的特殊用法,由普通字符(例如字符a-z)以及特殊字符(称为元字符,如/.*.?等)组成.   (2)基本元字符集及其含义       ^ :只匹配行首.   如^a 匹配以a开头的行abc,

linux查找目录下的所有文件中是否含有某个字符串

查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" find .|xargs grep -ri "IBM" -l \ :只用来屏蔽一个元字符的特殊含义. 如\*,\',\",\|,\+,\^,\. 等       .:(点)只匹配任意单字符.       pattern\{n\}:只用来匹配前面pattern出现的次数.n为次数.如a\{2\}匹配aa.       pattern\{n,\}:含义同上,但次数

linux查找目录下的所有文件中是否含有某个字符串 &lt;zhuan&gt;

查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xargs grep -ri "IBM" -l 1.正则表达式 (1)正则表达式一般用来描述文本模式的特殊用法,由普通字符(例如字符a-z)以及特殊字符(称为元字符,如/.*.?等)组成. (2)基本元字符集及其含义 ^ :只匹配行首. 如^a 匹配以a开头的行abc,a2e,a12,aaa,.

查找目录下的所有文件中是否含有某个字符串

find .|xargs grep -ri "IBM" find .|xargs grep -ri "IBM" -l \ :只用来屏蔽一个元字符的特殊含义. 如\*,\',\",\|,\+,\^,\. 等       .:(点)只匹配任意单字符.       pattern\{n\}:只用来匹配前面pattern出现的次数.n为次数.如a\{2\}匹配aa.       pattern\{n,\}:含义同上,但次数最少为n.如a\{2,\}匹配aa,aaa

递归打印当前目录下的所有文件的文件名和文件大小

递归打印当前目录下的所有文件的文件名和文件大小,在ubuntu14.04下编译通过: /************************************************************************* > File Name: dirwalk.c > Author: > Mail: > Created Time: Tue 31 Mar 2015 11:56:38 AM CST ************************************

Linux查看文件夹大小du

du命令参数详解见: http://baike.baidu.com/view/43913.htm 下面我们只对其做简单介绍: 查看linux文件目录的大小和文件夹包含的文件数   统计总数大小   du -sh filename(其实我们经常用du -sh *,显示当前目录下所有的文件及其大小,如果要排序再在后面加上 | sort -n)     du -sm * | sort -n //统计当前目录大小 并按大小 排序     du -sk * | sort -n     du -sk * |

mac 查找当前目录下所有同一类型文件,并执行命令行

以TexturePacker举例 MAC下用TexturePacker命令行打包当前目录下所有的 *.tps文件 1.配置好tps文件需要配置好路径.参数等.(也可不配置,用命令行实现.具体参考:https://www.codeandweb.com/texturepacker/documentation) 2.新建一个*.sh文件,内容填写 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && p