歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> Centos文件操作常用命令整理

Centos文件操作常用命令整理

日期:2017/3/6 9:15:13   编辑:學習Linux

Centos文件操作常用命令整理


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 ~]# 

clip_image001

[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 {} \;

http://xxxxxx/Linuxjc/1155986.html TechArticle

Copyright © Linux教程網 All Rights Reserved