linux中g s在文件中的使用方法,linux系统下以存储从大到小并以K,M,G为单位的方式查看当前目录下的文件信息...

前言

最近几乎大部分工做都在linux上进行的,发如今工做过程当中常常要查看当前目录下的文件大小,可是每次都是用ls命令每次都是以字节形式显示的,看起来特别不爽。因而开始了再linux下如何看起来很爽文件显示之旅。python

ls命令

这个命令多是咱们在linux环境下最经常使用的命令了,闲来没事, ls一下。用ls命令查看文件夹下的文件信息通常是这样的linux

sty@dl-server01:~/styfiles$ ls

demo.sh fastAI pythonFile styNet teamviewer_amd64.deb

ls -a

咱们只能看见文件夹下的显示当前目录下非隐藏的文件与目录信息

咱们用ls -a显示当前目录下包括隐藏文件在内的全部文件列表git

sty@dl-server01:~/styfiles$ ls -a

. .. demo.sh fastAI .ipynb_checkpoints pythonFile styNet teamviewer_amd64.deb

ls -l

咱们用ls -l 输出长格式列表,信息包含了文件的大小和类型github

sty@dl-server01:~/styfiles$ ls -l

total 10148

-rw-rw-r-- 1 sty sty 394 4月 5 20:42 demo.sh

drwxrwxr-x 5 sty sty 4096 1月 14 18:20 fastAI

drwxrwxr-x 12 sty sty 4096 4月 5 21:48 pythonFile

drwxrwxr-x 3 sty sty 4096 4月 5 20:26 styNet

-rw-rw-r-- 1 sty sty 10374958 12月 19 15:25 teamviewer_amd64.deb

ll

我平时用的更多的多是ll,这个在ls -l的基础上还能够显示隐藏文件shell

sty@dl-server01:~/styfiles$ ll

total 10160

drwxrwxr-x 6 sty sty 4096 4月 5 22:01 ./

drwxr-xr-x 6 sty sty 4096 4月 5 21:57 ../

-rw-rw-r-- 1 sty sty 394 4月 5 20:42 demo.sh

drwxrwxr-x 5 sty sty 4096 1月 14 18:20 fastAI/

drwxr-xr-x 2 sty sty 4096 1月 14 16:48 .ipynb_checkpoints/

drwxrwxr-x 12 sty sty 4096 4月 5 21:48 pythonFile/

drwxrwxr-x 3 sty sty 4096 4月 5 20:26 styNet/

-rw-rw-r-- 1 sty sty 10374958 12月 19 15:25 teamviewer_amd64.deb

du命令

这个命令主要是显示目录或者文件所占空间vim

[root@localhost test]# du

608 ./test6

308 ./test4

4 ./scf/lib

4 ./scf/service/deploy/product

4 ./scf/service/deploy/info

12 ./scf/service/deploy

16 ./scf/service

4 ./scf/doc

4 ./scf/bin

32 ./scf

8 ./test3

1288 .

du -s

使用du -s只显示总和的大小bash

sty@dl-server01:~/styfiles$ du -s

2747588 .

du -s *

使用du -s *将显示全部文件的大小markdown

sty@dl-server01:~/styfiles$ du -s *

4 demo.sh

2722128 fastAI

15292 pythonFile

24 styNet

10132 teamviewer_amd64.deb

du -s * | sort -nr

使用du -s * | sort -nr咱们能够将文件从大到小显示出来spa

sty@dl-server01:~/styfiles$ du -s * | sort -nr

2722128 fastAI

15292 pythonFile

10132 teamviewer_amd64.deb

24 styNet

4 demo.sh

可是这是以kb形式显示文件大小的,好比我想知道fastAI这个文件夹多大,我确定但愿以MB或者GB为单位。使用du -sh *是能够这么人性化的显示的

sty@dl-server01:~/styfiles$ du -sh *

4.0K demo.sh

2.6G fastAI

15M pythonFile

24K styNet

9.9M teamviewer_amd64.deb

可是没有排序,咱们再用组合命令du -sh * | sort -nr进行排序以后发现排序是以数字大小排序的,咱们fastAI文件夹都2.6G大小了却排到了最后面,这显示不是咱们想要的

sty@dl-server01:~/styfiles$ du -sh * | sort -nr

24K styNet

15M pythonFile

9.9M teamviewer_amd64.deb

4.0K demo.sh

2.6G fastAI

个人DIY_Bash

可是我想要的结果是这样的:

sty@dl-server01:~/styfiles$ sort_file.sh

2.60G fastAI

14.93M pythonFile

9.89M teamviewer_amd64.deb

24k styNet

4k demo.sh

其实这个咱们能够经过du 和awk命令组合完成

#!/bin/sh

# description: Sort the files in your directory by size, and Display their storage size directly

# user: sty

# blog: https://blog.csdn.net/sty945

# github: https://github.com/sty945

du -s * | sort -nr | awk -F'\t' '{if(1024 * 1024 * 1024 * 1024 > $1 && $1 >= 1024 * 1024 * 1024) {printf "%.2fT\t\t %-2s\n", $1/(1024 * 1024 * 1024), $2} else if(1024 * 1024 * 1024 > $1 && $1 >= 1024 * 1024) {printf "%.2fG\t\t %-2s\n", $1/(1024 * 1024), $2} else if (1024 * 1024 > $1 && $1 >= 1024) {printf "%.2fM\t\t %-2s\n", $1/1024, $2} else {printf "%sk\t\t %-2s\n", $1, $2}}'

我已经将该脚本写入shell脚本,并上传到Github之上地址以下,而且里面也将详细写出了如何在linux任何地方均可以随时使用这个脚本,就像你使用linux命令同样方便:

DIY_Bash

欢迎你们fork这个文件夹,让里面的内容丰富起来,若是以为不错,请点个star,鼓励我一下

在linux任意位置均可以使用这个目录下的脚本

咱们从Github中下载文件夹到本身的电脑上,解压后,进入文件夹,而后敲命令pwd显示当前文件的路径是多少,好比个人电脑的路径是:

sty@dl-server01:~/DIY_Bash$ pwd

/media/home/sty/DIY_Bash

而后咱们打开~/.bash_profile

sty@dl-server01:~$ vim ~/.bash_profile

在~/.bash_profile中按照下面的样式加入刚才显示的目录

export PATH=/media/home/sty/DIY_Bash:$PATH

而后执行下面的命令

source ~/.bash_profile

从新执行刚修改的初始化文件,使之当即生效,而没必要注销并从新登陆

而后咱们就能够在任意的文件夹下使用咱们的脚本了

好比咱们只须要在输入sorf_file.sh就能够以从存储大到小的顺序并且人性化的显示当前文件夹的非隐藏目录的大小了。

注意

若是你出现出现相似下面的错误:Permission denied

sty@dl-server01:~$ sort_file.sh

-bash: /media/home/sty/DIY_Bash/sort_file.sh: Permission denied

这是因为这个脚本没有权限的缘由,你只须要进入DIY_Bash文件夹执行下面的命令便可,

这将为DIY_Bash下的脚本都赋予执行权限:

chmod +x *

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值