CentOS Learning Notes-Basic commands-File and directory management

Source: Internet
Author: User
Tags syslog syslog example

Linux basic commands--File and directory management

Ben has been selected from the bird Brother's Linux private dishes-Basic Study directory seventh chapter, Linux file and directory management

LS (view of files and directories)

The LS command is the abbreviation of list, LS can be said to be the most common command, because we always need to know the file or directory related properties.

[[Email protected] ~]#ls [-aadffhilnrrst] Directory name[[Email protected] ~]#ls [--color={never,auto,always}] Directory name[[Email protected] ~]#ls [--full-time] Directory nameoptions and Parameters:-A: All files are listed together with the hidden file (files that begin with.) (common)-A: All documents, together with hidden files, but not included. The two directories-D: List only the directory itself, not the file data in the directory (common)- F: List the results directly without sorting (LS is sorted by default!) )- F: Gives additional data structures based on information such as files, directories, etc., such as:*: representative of the executable file;/: representative Directory; =: represents socket file; |: Represents FIFO file;-h: List the file capacity in a human-readable manner (e.g. GB, KB, etc.);-I: List the inode number, the meaning of the Inode the next chapter will be introduced;-L: Long data string list, including the file attributes and permissions and so on data; (common)-N: List uid with GID instead of user and group name (UID and GID will be mentioned in account management!) )-r: Reverse the output of the sorting results, for example: the original file name from small to large, the reverse is from large to small;-R: Together with the subdirectory contents, all the files in the directory will be displayed;-S: Sort by file size, not by filename;-T: Sort by time, not by file name. --color=never: Do not give color display according to the file characteristics;--color=Always : Display color--color=Auto: Let the system decide whether to give the color according to the configuration--full-time: Output in full-temporal mode (including year, month, day, hour, minute)--time={atime,ctime}: Output access time or Change permission attribute time (CTime) instead of content change time (modification times)

Most commonly used is ls–l, many Linux versions in the default case, will (L's lowercase) configured to become ls-l meaning! In fact, that function is the alias function of Bash Shell ~ that is, we directly input ll will be equal to the input ls-l is the same

Example one:-Al shows all the files, hidden also shows [root@localhost ~]#Ls-alTotal 52Dr-xr-x---. 2 root root 4096 Nov 3 20:06. Dr-xr-xr-x. Root root 4096 Nov 14 16:08 ..-RW-------. 1 root root 1098 Nov 3 19:57 anaconda-ks.cfg-RW-------. 1 root root 1483 Dec 11 12:16. Bash_history-rw-r--r--. 1 root Root 20 2009. Bash_logout-rw-r--r--. 1 root root 176 May 20 2009. Bash_profile-rw-r--r--. 1 root root 176 Sep 23 2004. BASHRC-rw-r--r--. 1 root root Sep 23 2004. CSHRC-rw-r--r--. 1 root root 8726 Nov 3 19:57Install.log-rw-r--r--. 1 root root 3190 Nov 3 19:56Install.log.syslog-rw-r--r--. 1 root root 129 Dec 4 2004. TCSHRC Example II:-l Show file, hidden does not show [root@localhost ~]#ls-lTotal 20-RW-------. 1 root root 1098 Nov 3 19:57 anaconda-ks.cfg-rw-r--r--. 1 root root 8726 Nov 3 19:57Install.log-rw-r--r--. 1 root root 3190 Nov 3 19:56Install.log.syslog Example three: ll with LS-L[root@localhost ~]#llTotal 20-RW-------. 1 root root 1098 Nov 3 19:57 anaconda-ks.cfg-rw-r--r--. 1 root root 8726 Nov 3 19:57Install.log-rw-r--r--. 1 root root 3190 Nov 3 19:56 install.log.syslog
CP (copy files or directories)

To copy a file, use the CP command (copy). However, the CP command can be used for many purposes ~ except for simple replication, you can also create a link file (that is, shortcut ROM), a newer and older than the two files to be upgraded, and copy the entire directory, and so on functions!

[[Email protected] ~]#CP [-adfilprsu] source file (source) target file (destination)[[Email protected] ~]#CP [Options] Source1 source2 source3 .... Directoryoptions and Parameters:-A: pretty much-pdr The meaning of the PDR, please refer to the following instructions; (Common)-D: If the source file is the attribute of the link file (link file), the link file property is copied instead of the file itself;- F: For force, if the target file already exists and cannot be opened, then try again after removing it;-I: If the target file (destination) already exists, the action will be asked (common) when overwriting .-L: Create a link file for hard link, not copy the file itself;-p: Copy the past with the attributes of the file, rather than using the default properties (backup is often used);-r: Recursive continuous replication, used for the replication behavior of the directory; (Common)-s: Copy becomes the symbolic link file (symbolic link), namely "shortcut" files;-u: Upgrade destination If destination is older than source! Finally, note that if the source file has more than two, then the last destination file must be "directory"! 

Replication (CP) This command is very important, the different identity of the run this command will have different results, especially the-a,-p option, for different identities, the difference is very big!

Example one: Copy the. BASHRC in the home directory to/from the root identity.TMP, and renamed to Bashrc[root@www ~]#CP ~/.BASHRC/TMP/BASHRC[[Email protected] ~]#cp-i ~/.BASHRC/TMP/BASHRCCp:overwrite '/TMP/BASHRC '? N <==n does not overwrite, Y is overlay#repeat two times, since the BASHRC already exists under/tmp, plus the-i option,#you will be asked if the user is sure before overwriting! You can press N or y to confirm it two times! Example two: transforming directories to/tmp, and copy the/var/log/wtmp to/tmp and Observe properties: [Root@www ~]#cd/tmp[[email protected] tmp]#cp/var/log/wtmp. <== want to copy to the current directory, the last. Don't forget[[email protected] tmp]#ls-l/var/log/wtmp wtmp-rw-rw-r--1 root utmp 96384 Sep 11:54/var/log/wtmp-rw-r--r--1 root root 96384 Sep 24 14:06wtmp#Note the above special font, in the case of no option, some properties/permissions of the file will change;#This is a very important feature! Be careful! Also, the time to create files is not the same! #What if you want to copy all the features of the file together? Can add-a Oh! As shown below:[Root@www TMP]#cp-a/var/log/wtmp wtmp_2[[email protected] tmp]#ls-l/var/log/wtmp wtmp_2-rw-rw-r--1 root utmp 96384 Sep 11:54/var/log/wtmp-rw-rw-r--1 root utmp 96384 SEP 24 11:54wtmp_2#Come on! The entire data feature is exactly the same sunglass! This is a feature of-a! 

In general, if we go to copy other people's data (of course, the file you have to have Read permission to do!) ^_^), we always want to copy the data to the end of our own, so, in the default condition, the source file of CP and the permissions of the destination file is different, the owner of the destination file is usually the command operator itself. For example, the above example two, because I am root, so the copy of the file owner and the group has changed to become root!

Because of this feature, when we are doing backup, some special privilege files that need special attention, such as password file (/etc/shadow) and some configuration file, can not be copied directly with CP, but must be added with the option of-A or-p and so on to fully copy file permissions! In addition, if you want to copy the file to other users, you must also notice the permissions of the file (including read, write, run, and file owner, etc.), otherwise, other people will not be able to make changes to the files you give the action Oh! Pay attention!

Example three: Copy all contents of/etc/in this directory to/tmp under [root@www TMP]#cp/etc//tmpCp:omitting directory '/etc ' <== if the directory is not directly copied, to add-options for R [Root@www TMP]#cp-r/etc//tmp#still want to emphasize again! -R is a directory that can be replicated, but the permissions of files and directories may be changed#So, you can also use "cp-a/etc/tmp" to give orders Oh! Especially in the case of backup! Example four: Create a link file (symbolic link) with a copy of the sample BASHRC [root@www TMP]#ls-l BASHRC-rw-r--r--1 root root 176 Sep 14:02 BASHRC <==First look at the file situation [root@www TMP]#cp-s BASHRC Bashrc_slink[[email protected] tmp]#cp-l BASHRC Bashrc_hlink[[email protected] tmp]#ls-l bashrc*-rw-r--r--2 root root 176 Sep 14:02 BASHRC <==It 's not the same as the original file! -rw-r--r--2 root root 176 Sep 24 14:02bashrc_hlinklrwxrwxrwx1 root root 6 Sep 14:20 Bashrc_slink-BASHRC

Example four can be fun! The so-called link file is created using-l and-s, but there is a different case for the two types of Nexus files. What the hell is this? That-L is the so-called entity link (hard link), as-S is a symbolic link (symbolic link), simply speaking, Bashrc_slink is a "shortcut", this shortcut will be linked to BASHRC! So you'll see a symbol pointing to the right side of the file name!

As the Bashrc_hlink file and the BASHRC properties and permissions are exactly the same, and before the link is not the difference is the second column of the link number from 1 to 2!

Example five: if ~/.BASHRC than/tmp/BASHRC the new copy. [Root@www TMP]#cp-u ~/.BASHRC/TMP/BASHRC#The -u feature is only copied when the target file differs from the source file. #Therefore, more often used in "backup" of the work Oh! ^_^Example Six: Copying the bashrc_slink caused by paradigm four into bashrc_slink_1 and Bashrc_slink_2[root@www TMP]#CP Bashrc_slink Bashrc_slink_1[[email protected] tmp]#cp-d Bashrc_slink bashrc_slink_2[[email protected] tmp]#ls-l BASHRC bashrc_slink*-rw-r--r--2 root root 176 Sep 24 14:02bashrclrwxrwxrwx1 root root 6 Sep 14:20 Bashrc_slinkBASHRC-rw-r--r--1 root root 176 Sep 14:32 Bashrc_slink_1 <==same as original file lrwxrwxrwx1 root root 6 Sep 14:33 bashrc_slink_2-BASHRC <==It's a link file! #This example is also very interesting oh! The original copy is the link file, but the link file to copy the actual files .#that is, if you do not add any options, CP copies the original file, not the properties of the link file! #To copy the properties of a link file, you have to use the-D option! As shown in the Bashrc_slink_2. Example VII: Copy the. BASHRC and. Bash_history of the home directory to/tmp under [root@www TMP]#CP ~/.BASHRC ~/.bash_history/tmp#You can copy multiple data to the same directory at once! The last side must be the catalogue! 

Example: Can you use the identity of Vbird, complete copy/var/log/wtmp file to/tmp under, and renamed to Vbird_wtmp?

A: The results of the actual look are as follows:

[Email protected] ~]$ cp-a/var/log/wtmp/tmp/vbird_wtmp[vbird@www ~]$ ls-l/var/log/wtmp/tmp/vbird_wtmp -rw-rw-r--1 vbird vbird 96384  September 11:54/tmp/vbird_wtmp-rw-rw-r--1 root  utmp  96384  September 11:54/var/log/wtmp

Due to the identity of Vbird can not be arbitrarily modified file owners and groups, so although the ability to copy wtmp related permissions and time and other properties, but with the owner, group-related, the original Vbird identity can not be done, even with the-a option, it is not possible to achieve full copy permissions!

In short, because CP has a variety of file attributes and permissions, so, when copying, you have to understand clearly:

    • Do you need complete information about the source file?
    • is the source file a link file (symbolic link files)?
    • is the source file a special document such as FIFO, socket, etc.?
    • is the source file a directory?
MV (move files and directories, or rename)

This is the meaning of Move! When you want to move files or directories when, hehe! This command is very important!

[[Email protected] ~]#MV [-FIU] source destination[[Email protected] ~]#mv [options] Source1 source2 source3 .... Directoryoptions and Parameters:- F: Force coercion means that if the target file already exists, it will not be queried and overwritten directly;-I: If the target file (destination) is already present, you will be asked to overwrite it! -u: If the target file already exists and the source is newer, it will upgrade (update) example one: Copy a file, create a directory, move the file to the directory [root@www ~]#cd/tmp[[email protected] tmp]#CP ~/.BASHRC BASHRC[[email protected] tmp]#mkdir mvtest[[email protected] tmp]#MV BASHRC mvtest#move a file to a directory, that's it! Example two: renaming a newly renamed directory name to Mvtest2[root@www TMP]#MV Mvtest Mvtest2 <== that's the name! Simple ~#in fact, under Linux there is an interesting command, named Rename,#the command is renamed at the same time as multiple file names, not for a single file name change, unlike MV. Please man rename. Example Three: Create two more files, and then move them all to/tmp/Mvtest2 [Root@www TMP]#CP ~/.BASHRC BASHRC1[[email protected] tmp]#CP ~/.BASHRC BASHRC2[[email protected] tmp]#mv Bashrc1 bashrc2 mvtest2#Note this way, if there are multiple source files or directories, the last destination must be "Directory!" "#That means to move all the data to the meaning of that directory! 
RM (remove files or directories)

This is the removal of the command (remove), to be aware of, usually under the Linux system, in order to fear the file is killed, so many distributions have been added by default-i this option! And if you want to kill everything under the directory, such as a subdirectory with subdirectories, then use the-r option! However, before using the "rm-r" command, please be careful, because the directory or file "Sure" will be killed by root! Because the system will not ask you again if you want to cut off yo! So that's a super-serious order! Have to pay special attention! However, if you are sure that the directory is out of the way, it is a good idea to use rm-r to cycle through the kill!

[[Email protected] ~]#RM [-fir] File or directoryoptions and Parameters:- F: The meaning of force, ignoring the non-existent file, no warning message appears;-I: interactive mode, before the deletion will ask the user whether the action-R: Pass it back! Most commonly used in directories are deleted! This is a very dangerous option!!! Example one: Delete the BASHRC that you just created in the example of CP! [Root@www ~]#cd/tmp[[email protected] tmp]#rm-i BASHRCRm:remove regular file ' BASHRC '?y#If you add the-I option, you will be asked to avoid deleting the wrong file name! example two: through million bytes* To help, will/the file name for BASHRC under TMP is deleted: [Root@www TMP]#rm-i bashrc*#Note that the asterisk, which represents 0 to infinitely many arbitrary bytes Oh! What a good thing to do! example Three: The CP example is created in the/tmp/etc/Delete this directory! [Root@www TMP]#rmdir/tmp/etcRmdir:etc:Directory not empty <==You can't delete it! Because this is not an empty directory! [Root@www TMP]#rm-r/tmp/etcRm:descend into directory '/tmp/etc '?y .... (omitted in the middle) ....#because the identity is root, the default has been added to the-I option, so you have to press Y to delete! #If you do not want to continue pressing Y, you can press [CTRL]-C] to end the work of RM. #This is a protective action, if you are sure you want to delete this directory without asking, you can do this:[[email protected] tmp]#\rm-r/tmp/etc#with a backslash in front of the command, you can ignore the specified option for alias! As Alias, we'll talk about it in bash! Example four: delete a-file starting with [root@www TMP]#touch./-aaa-<==touch This command to create an empty file! [[email protected] tmp]#ls-l-rw-r--r--1 root root 0 Sep 15:03-aaa-<==The file size is 0, so it is an empty file [root@www TMP]#rm-aaa-Try ' rm--help ' forMore information. <== because"-"is the option! So the system misjudged! [Root@www TMP]#rm./-aaa-

In addition, example four is also a very interesting example, we talked about before, the file name is best not to use the "-" number, because "-" followed by the option, so the simple use of "rm-aaa-" system command will be misjudged! If you use the formal notation that you talk about later, there will be problems! So, only to avoid the first byte is the "-" Method! Just add this directory "./" Can! If the man RM, in fact, there is a way, that is, "rm---aaa-" can Ah!

CentOS Learning Notes-Basic commands-File and directory management

Related Article

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.