Basic folder operation commands in CentOS

Source: Internet
Author: User
Tags disk usage

File (folder)View class commands

Ls -- display the content in the specified directory

Note: The ls display results are distinguished by different colors. Blue indicates the directory, gray indicates normal files, Green indicates executable files, red indicates compressed files, and light blue indicates linked files.

-A --- show all content, including hidden files

Note: in Linux, "." is used to hide files or directories.

-L --- displays detailed information about a file or directory in a long format (more detailed.

Note: The ls-l command can be abbreviated to ll,

The output information is divided into seven groups:
File category and File Permission, number of links or subdirectories, file owner, file group, file size (in bytes B), file creation or modification time, and file name.

File category: the first part of the first group represents the file category, "-" represents a common file, "d" represents a directory, "l" represents a symbolic link, and "c" represents a character device, "B" indicates Block devices.
File Permission: the last nine digits of the first group indicate the File Permission. The first three digits are user, the middle three are group, and the last three digits are other.

-D --- display the attributes of the directory rather than the contents in the directory.

12345[root@localhost ~]# ls -ld /homeDrwxr-xr-x. 4 root 4096 September 22 10:41/home[root@localhost ~]# ls -d /home/home[root@localhost ~]#

-H --- display the file size in units such as K, M, and G (the default value is byte)

1234567[root@localhost ~]# ls -h /homejustin lost+found[root@localhost ~]# ls -lh /homeTotal 20 KDrwx ------. 27 justin 4.0 K September 22 13:19 justinDrwx ------. 2 root 16 K September 18 15:30 lost + found[root@localhost ~]#

-R --- if the directory contains files, the files are listed in sequence.

12345678910111213141516171819202122232425262728293031323334[root@localhost ~]# ls -lR /home/home:Total usage 20Drwx ------. 27 justin 4096 September 22 13:19 justinDrwx ------. 2 root 16384 September 18 15:30 lost + found/home/justin:Total usage 32Drwxr-xr-x. 2 justin 4096 10:49 publicDrwxr-xr-x. 2 justin 4096 September 22 10:49 TemplateDrwxr-xr-x. 2 justin 4096 10:49 videoDrwxr-xr-x. 2 justin 4096 10:49 ImagesDrwxr-xr-x. 2 justin 4096 10:49 documentDrwxr-xr-x. 2 justin 4096 10:49 downloadDrwxr-xr-x. 2 justin 4096 September 22 10:49 musicDrwxr-xr-x. 2 justin 4096 September 22 10:49 Desktop/home/justin/Public:Total usage 0/home/justin/Template:Total usage 0/home/justin/Video:Total usage 0/home/justin/Image:Total usage 0/home/justin/Documentation:Total usage 0/home/justin/Download:Total usage 0/home/justin/Music:Total usage 0/home/justin/Desktop:Total usage 0/home/lost+found:Total usage 0[root@localhost ~]#

-T --- list archives in the order of creation time

1234567891011[root@localhost ~]# ls -l /homeTotal usage 20Drwx ------. 27 justin 4096 September 22 13:19 justinDrwx ------. 2 root 16384 September 18 15:30 lost + found-Rw-r --. 1 root 0 September 22 15:21 t[root@localhost ~]# ls -lt /homeTotal usage 20-Rw-r --. 1 root 0 September 22 15:21 tDrwx ------. 27 justin 4096 September 22 13:19 justinDrwx ------. 2 root 16384 September 18 15:30 lost + found[root@localhost ~]#

Note: The ls command can also be used with the wildcard "?" Or "*", question mark "?" It can match any character in the file name, while "*" can match any multiple characters in the file name. These two wildcards also apply to most other commands in the Shell environment.

123456789101112gssapi_mech.conf popt.d xmlgtk-2.0 portreserve yp.confhal postfix yumhost.conf ppp yum.confhosts prelink.cache yum.repos.dhosts.allow prelink.confhosts.deny prelink.conf.d[root@localhost etc]# ll -d /etc/po*.dDrwxr-xr-x. 2 root 4096 2010/etc/popt.d[root@localhost etc]# ll -d /etc/po?.dls: Inaccessible/etc/po?. D: The file or directory does not exist.[root@localhost etc]#


Du --- display file or directory size

-H or -- human-readable --- in the unit of K, M, G to improve information readability

12345[root@localhost src]# du -h nagios-3.5.0.tar.gz1.8M nagios-3.5.0.tar.gz[root@localhost src]# du nagios-3.5.0.tar.gz1748 nagios-3.5.0.tar.gz[root@localhost src]#

-A --- display the disk space occupied by all directories and each file in the second directory

-B or-bytes --- displays the directory or file size in bytes.

123[root@localhost local]# du -b src/nagios-3.5.0.tar.gz1789376 src/nagios-3.5.0.tar.gz[root@localhost local]#

-C or -- total --- displays the size of each directory or file, and the total of all directories or files.


-M or -- megabytes --- in 1 MB
-S --- only display the total size of each file

123[root@localhost local]# du -sh src/41M src/[root@localhost local]#

-X --- only calculate the files of the same file system

-L --- calculate the size of all files


Df --- displays the status of the file system. It is mainly used to understand the disk usage of each mounted file system in the system.

-H: display more readable capacity units
-T displays the type of the file system

12345678[root@localhost ~]# df -ThFile System Type capacity available in use % mount point/dev/sda2ext49.9G 2.6G 6.9G 28% /tmpfs tmpfs 504M 112K 504M 1% /dev/shm/dev/sda1ext4194M 27M 158M 15% /boot/dev/sda5ext47.7G 147M 7.2G 2% /home/dev/sr0iso96602.9G 2.9G 0 100% /media/RHEL_6.3 i386 Disc 1[root@localhost ~]#


File --- View file Types

123456[root@localhost home]# file justin/justin/: directory[root@localhost home]# file justin1justin1: empty[root@localhost ~]# file install.loginstall.log: UTF-8 Unicode text

Note: The file command is used to view the file type. It can be determined based on the internal storage structure of the file, rather than the file extension. In Linux, the file extension has no absolute relationship with the file type.


View File Content

Cat --- display file content

1[root@localhost log]# cat /var/log/messages

Note: cat does not pause when displaying the content of a text file. For files with long content, only the content of the last page is displayed on the screen after the quick scrolling display, therefore, cat is not suitable for viewing long files.

More --- display file content by PAGE

-Num --- number of rows displayed at a time
-S --- when there is a blank row with more than two consecutive rows, it is replaced by a blank row
+ Num --- display from row num

1234567[root@localhost log]# more -5s /var/log/messagesSep 22 10:36:06 localhost kernel: imklog 5.8.10, log source= /proc/kmsgstarted.Sep 22 10:36:06 localhost rsyslogd: [origin software="rsyslogd"swVersion="5.8.10"x-pid="1323"x-info="http://www.rsyslog.com"] startSep 22 10:36:06 localhost kernel: Initializing cgroup subsys cpusetSep 22 10:36:06 localhost kernel: Initializing cgroup subsys cpu--More--(0%)

Note: similar to cat, but it will be displayed on one page for users to read page by page. space: displays the next page, B: displays the previous page, and q: exits.

Less ---- display file content by PAGE

Note: The usage of the less command is similar to that of the more command. The difference between them is that when the file content is displayed at the end of the file, the more command automatically exits from the reading environment, while the less command does not exit automatically, you can still use the up/down key to scroll up a file, which is more conducive to reading the file content repeatedly. When you want to end browsing, press the Q key after the ":" prompt in the less command to exit.

Head --- View content at the beginning of the file

-N indicates the specific number of lines displayed.

1234567[root@localhost ~]# head -5 /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@localhost ~]#

Note: by default, the first 10 rows of content are displayed in the head.

Tail --- view the content at the end of the file

1234567[root@localhost ~]# tail -5 /etc/passwdnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologintcpdump:x:72:72::/:/sbin/nologinjustin:x:500:500:justin_peng:/home/justin:/bin/bash[root@localhost ~]#Note: The tail command is similar to the head command.


Create File (folder) command

Touch --- change the time record of the file. If the file does not exist, create an empty file.

1234[root@localhost home]# touch file1 file2[root@localhost home]# lsfile1 file2 justin lost+found t[root@localhost home]#

-A: changes the file read time record.
-M: Change the modification time record of the file.
-C if the target file does not exist, no new file will be created.

Note: in actual use, it is often used to create new test files. You can use the file name as a parameter to create multiple files at the same time. When the target file already exists, the time mark of the file will be updated. Otherwise, an empty file with the specified name will be created.

Mkdir --- Make Directory --- create a new Directory

-P: Make sure that the directory name exists. If the directory name does not exist, create one.

1234567[root@localhost home]# lsjustin lost+found t[root@localhost home]# mkdir dir1[root@localhost home]# mkdir dir2/dirmkdir: Unable to create directory"dir2/dir": The file or directory does not exist.[root@localhost home]# mkdir -p dir2/dir[root@localhost home]#

Create multi-level directories at the same time

12345678[root@localhost home]# lsjustin lost+found t[root@localhost home]# mkdir -p {dir1,dir2/{dir3,dir4}}[root@localhost home]# lsdir1 dir2 justin lost+found t[root@localhost home]# ls dir2dir3 dir4[root@localhost home]#


Delete file (folder) command

RmdIr --- delete an empty directory (if the directory is not empty, an error is returned)

-P: When the subdirectory is deleted and becomes an empty directory, delete it together.

12345678910[root@localhost home]# mkdir -p {dir1,dir2/dir3}[root@localhost home]# lsdir1 dir2 justin lost+found t[root@localhost home]# rmdir dir1[root@localhost home]# rmdir dir2rmdir: Delete"dir2"Failed: the directory is not empty.[root@localhost home]# rmdir -p dir2/dir3/[root@localhost home]# lsjustin lost+found t[root@localhost home]#


Rm --- delete an archive or directory

-I ask for confirmation one by one before deletion.
-F the original file is deleted directly even if its attribute is set to "read-only". You do not need to confirm the attribute one by one.
-R also deletes directories and the following files one by one.

123456[root@localhost home]# mkdir -p dir1/dir2[root@localhost home]# rm -r dir1/rm: Whether to enter the Directory"dir1"? yrm: Whether to delete the Directory"dir1/dir2"?yrm: Whether to delete the Directory"dir1"?y[root@localhost home]#



This article is from the "process is to achieve results" blog, please be sure to keep this source http://justinpeng.blog.51cto.com/7662323/1300532


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.