Centos檔案操作常用命令整理

來源:互聯網
上載者:User

Centos檔案操作常用命令整理

收集、整理日常系統管理或維護當中的,常用到的一些關於檔案操作的命令或需求,後續會慢慢補充、完善!

查看、產生指定目錄的分類樹結構?


[root@DB-Server ~]#tree #目前的目錄

[root@DB-Server ~]#tree /home/oracle/kerry

查看目前的目錄或指定目錄的大小?

[root@DB-Server ~]#du -sh /u01
[root@DB-Server ~]#du -sh 

查看各個檔案或子檔案夾大小

[root@DB-Server ~]#du -sh *

尋找相關檔案並列印輸出?

[root@DB-Server ~] find /home/oracle -name "awr*" -print

計算目前的目錄下的檔案和檔案夾數?

[root@DB-Server ~]# ls -lrt | grep -v 'total' | wc -l

計算目前的目錄下的檔案數(不包含子目錄)?

[root@DB-Server ~]#ls -l | grep "^-" | wc -l

計算目前的目錄下的檔案數(包含子目錄)?

[root@DB-Server ~]#ls -l * | grep "^-" | wc –l

只查看或列出當前檔案夾下的子目錄?

[root@DB-Server ~]# ls -l | grep "^d" | wc -l

只列出目錄下面的子目錄?

[root@DB-Server ~]# ls -F | grep /$ #注意:此命令不能統計子目錄的子目錄。

查看檔案的某一列內容

[root@DB-Server ~]# cat /etc/passwd | awk -F ":"'{print $1}'

查看檔案的前N行內容

[root@DB-Server ~]# head -5 /etc/passwd

查看檔案的後N行內容

[root@DB-Server ~]# tail -5 /etc/passwd

查看檔案中的某幾行內容

[root@DB-Server ~]# sed -n '5,10p' /etc/passwd

查看檔案的建立時間、修改時間、訪問時間 建立時間、訪問時間、修改時間和改變時間

[root@DB-Server ~]# touch test
[root@DB-Server ~]# stat test
 File: `test'
 Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 4196044 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-07-25 15:53:07.000000000 +0800
Modify: 2016-07-25 15:53:07.000000000 +0800
Change: 2016-07-25 15:53:07.000000000 +0800
[root@DB-Server ~]# 

Access 是訪問時間

Modify 是修改時間

Change 是改變時間

檔案建立時間其實是不存在的,若檔案從建立後不曾修改過則可認為建立時間=修改時間,若檔案建立後狀態也不曾改變過則可認為建立時間=改變時間,若檔案建立後不曾被讀取過則可認為建立時間=訪問時間。但是很少有檔案自建立後,不從不被訪問、修改。

[root@DB-Server ~]# cat test
You have new mail in /var/spool/mail/root
[root@DB-Server ~]# stat test
 File: `test'
 Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 4196044 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-07-25 16:20:20.000000000 +0800
Modify: 2016-07-25 15:53:07.000000000 +0800
Change: 2016-07-25 15:53:07.000000000 +0800
[root@DB-Server ~]# 

[root@DB-Server ~]# vi test
121232131
[root@DB-Server ~]# stat test
 File: `test'
 Size: 10 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 4196050 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-07-25 16:21:31.000000000 +0800
Modify: 2016-07-25 16:21:31.000000000 +0800
Change: 2016-07-25 16:21:31.000000000 +0800
[root@DB-Server ~]# 

分別刪除 建立5天前、修改5天前、訪問5天前的檔案。前2個用的較多。

find . -type f -ctime +5 -delete

find . -type f -mtime +5 -delete

find . -type f -atime +5 -delete

上面命令,只能刪除檔案,而不能刪除檔案夾或子檔案夾,如果要刪除對於的檔案夾,可以使用下面命令

find /backup/mysql -mtime +5 -name "*.*" -exec rm -rf {} \;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.