前言
最近幾乎大部分工作都在linux上進行的,發現在工作過程中經常要查看當前目錄下的文件大小,但是每次都是用ls
命令每次都是以字節形式顯示的,看起來特別不爽。於是開始了再linux下如何看起來很爽文件顯示之旅。
ls命令
這個命令可能是我們在linux環境下最常用的命令了,閑來沒事, ls
一下。用ls
命令查看文件夾下的文件信息一般是這樣的
-
sty@dl-server01:~/styfiles$ ls demo.sh fastAI pythonFile styNet teamviewer_amd64.deb
ls -a
我們只能看見文件夾下的顯示當前目錄下非隱藏的文件與目錄信息
我們用ls -a
顯示當前目錄下包括隱藏文件在內的所有文件列表
-
sty@dl-server01:~/styfiles$ ls -a . .. demo.sh fastAI .ipynb_checkpoints pythonFile styNet teamviewer_amd64.deb
ls -l
我們用ls -l
輸出長格式列表,信息包含了文件的大小和類型
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
的基礎上還可以顯示隱藏文件
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命令
這個命令主要是顯示目錄或者文件所占空間
[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
只顯示總和的大小
sty@dl-server01:~/styfiles$ du -s 2747588 .
du -s *
使用du -s *
將顯示所有文件的大小
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
我們可以將文件從大到小顯示出來
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 *