Common commands for Centos File Operations
Collect and sort out commonly used commands or requirements for file operations during routine system management or maintenance, which will be supplemented and improved in the future!
View and generate the directory tree structure of the specified directory?
[Root @ DB-Server ~] # Tree # Current Directory
[root@DB-Server ~]#tree /home/oracle/kerry
View the size of the current directory or specified directory?
[root@DB-Server ~]#du -sh /u01
[root@DB-Server ~]#du -sh
View the size of each file or subfolders
[root@DB-Server ~]#du -sh *
Search for related files and print the output?
[root@DB-Server ~] find /home/oracle -name "awr*" -print
Calculate the number of files and folders in the current directory?
[root@DB-Server ~]# ls -lrt | grep -v 'total' | wc -l
Calculate the number of files in the current directory (excluding subdirectories )?
[root@DB-Server ~]#ls -l | grep "^-" | wc -l
Calculate the number of files in the current directory (including subdirectories )?
[root@DB-Server ~]#ls -l * | grep "^-" | wc –l
Only view or list subdirectories in the current folder?
[root@DB-Server ~]# ls -l | grep "^d" | wc -l
Only list subdirectories under the directory?
[Root @ DB-Server ~] # Ls-F | grep/$ # Note: This command cannot count subdirectories of subdirectories.
View a column of the file
[root@DB-Server ~]# cat /etc/passwd | awk -F ":"'{print $1}'
View the first N lines of the file
[root@DB-Server ~]# head -5 /etc/passwd
View the last N lines of the file
[root@DB-Server ~]# tail -5 /etc/passwd
View certain lines of content in the file
[root@DB-Server ~]# sed -n '5,10p' /etc/passwd
View the file creation time, modification time, access time creation time, access time, modification time, and change time
[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 is the Access time.
Modify is the modification time.
Change is the Change time.
The file creation time does not actually exist. If the file has not been modified since it was created, the creation time = the modification time, if the file status has not changed after it is created, the creation time is considered as the change time. If the file has not been read after it is created, the creation time is considered as the access time. However, few files are never accessed or modified after they are created.
[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 ~]#
Delete the files created 5 days ago, modified 5 days ago, and accessed 5 days ago. The first two are mostly used.
find . -type f -ctime +5 -delete
find . -type f -mtime +5 -delete
find . -type f -atime +5 -delete
The preceding command can only delete files, but cannot delete folders or subfolders. to delete a folder, run the following command:
find /backup/mysql -mtime +5 -name "*.*" -exec rm -rf {} \;