File and directory management for Linux

Source: Internet
Author: User

1 Viewing files and directories: LS

[Email protected] ~]#ls [-aadffhilnrrst] Directory name[Email protected] ~]#ls [--color={never,auto,always}] Directory name[Email protected] ~]#ls [--full-time] Directory name options and parameters:  -a: All files are listed together with the hidden file (files that begin with.) (common)  -A: All files, together with hidden documents, but not included. And.. The two directories  -d: Lists only the directory itself, not the file data in the directory (common)  -F: Lists the results directly without sorting (LS The default is to sort by file name!  F: According to files, directories and other information, to give additional data structure, such as: *: representative of the executable file;/: representative Directory; =: represents a socket file; |: Represents a FIFO file; h: Lists the file capacity in a human-readable manner (e.g. GB, KB, etc.); : List inode numbers, the meaning of the inode is described in the next chapter;  -l: Long data string list, including file properties and permissions, etc. (Common)  -N: Lists the UID and GID instead of the name of the user and group (UID and GID will be mentioned in account management!). )-R: Reverse 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 that 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 to determine whether to give color--full-time: In full time mode (including year, month, day, hour, minute Output--time={atime,ctime}: Output access time or Change permission attribute time (CTime) instead of content change time (modification times)  Example :
Example One: List all the files in the home directory (with attributes and hidden documents) ls-al ~Total 156drwxr-x---  4 root root  . Drwxr-xr-x root root.   -RW-------  1 root root  1474 Sep  4 18:27 anaconda-ks.cfg-rw-------  1 root root   955 Sep 00:08. ba sh_history-rw-r--r--  1 root root  6  . bash_logout-rw-r--r--  1 root root   191 Jan  6  . bash_profile-rw-r--r--  1 root root   176 Jan  6  . bashrcdrwx------  3 root root  4096 Sep  . gconf-rw-r--r--  1 root root 42304 Sep  4 18:26 install.log-rw-r--r--  1 root root  5661 Sep  4 18:25 install.log.syslog# This time you'll see a few files starting with., as well as a directory file ( .) (..) . gconf Wait, # However, the directory file name is shown in dark blue, which is a bit difficult to see clearly. 

2 Copying files and directories CP

-D Keep the original link when copying
-P preserves the properties of the source file or directory, including the owner, the owning group, permissions and time, and so on
-R recursive processing, the files and subdirectories under the directory are processed together
-A is equivalent to the combination of DPR, which copies the Mode,group owner (if it has permission).

-F forcibly copy a file or directory, regardless of whether the destination file or directory already exists
-I ask the user before overwriting the file, interactive replication
-L hard link, not copy file
-s copy becomes a symbolic link file (symbolic link), which is to create a "shortcut"

Example

Example one: Copy the. BASHRC from the home directory to/TMP under root, and rename it to BASHRC[Email protected] ~]#CP ~/.BASHRC/TMP/BASHRC[Email protected] ~]#cp-i ~/.BASHRC/TMP/BASHRCCp:overwrite '/TMP/BASHRC '?N  <==n not covered, Y is covered# Repeat two times, since the BASHRC already exists under/TMP, plus the-i option, the # will ask the user if it is OK before overwriting! You can press N or y to confirm it two times! Example Two: Transform the directory to/TMP and copy the/VAR/LOG/WTMP to/TMP and observe the properties:[Email protected] ~]#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 rootutmp96384 SEP 2411:54/var/log/wtmp-rw-r--r--1 rootRoot96384 SEP 2414:06WtmpNote 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! # So what if you want to copy all the features of the file together? Can add-a Oh! As shown below:[Email protected] 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 11:54 wtmp_2# Come on! The entire data feature is exactly the same sunglass! This is a feature of-a! 
example Three: Copy all the contents of the/etc/directory to/TMP[Email protected] tmp]#cp/etc//tmpCp:omitting directory '/etc '<== If the directory is not directly copied, to add the-r option[Email protected] tmp]#cp-r/etc//tmp# still want to emphasize again Oh! -R is a directory that can be copied, however, the permissions of the file and directory 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 Nexus file (symbolic link) with a copy of the sample BASHRC[Email protected] tmp]#ls-l BASHRC-rw-r--r--1 root root 176 Sep 14:02 BASHRC<== First look at the file situation[Email protected] tmp]#cp-s BASHRC Bashrc_slink[Email protected] tmp]#cp-l BASHRC Bashrc_hlink[Email protected] tmp]#ls-l bashrc*-rw-r--r--2Root root 176 Sep 14:02 BASHRC<== is not the same as the original file! -rw-r--r--2Root root 176 Sep 14:02 bashrc_hlinklrwxrwxrwx 1 root root 6 Sep 14:20 Bashrc_slink-BASHRC
Example Five: If ~/.BASHRC is newer than/TMP/BASHRC, copy it.[Email protected] tmp]#cp-u ~/.BASHRC/TMP/BASHRC# this-u feature is only copied when the target file differs from the source file. # So, it's more often used in "Backup" work! ^_^Example Six: Copying the bashrc_slink caused by paradigm four into bashrc_slink_1 and Bashrc_slink_2[Email protected] 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 14:02 bashrclrwxrwxrwx 1 root root 6 Sep 14:20 Bashrc_slink-bashrc-rw-r--r-- 1 root root 176 Sep 14:32 Bashrc_slink_1<== the same as the original filelrwxrwxrwx 1 root root 6 Sep 14:33 bashrc_slink_2-BASHRC<== is the link file! # This example is also very interesting! The original copy is a link file, but the actual file of the link is copied over the # that is, if there is no option, CP is copying the original file, not the properties of the link file! # to copy the properties of a linked file, you have to use the-D option! As shown in the Bashrc_slink_2. Example VII: Copy the home directory's. BASHRC and. Bash_history to/tmp[Email protected] tmp]#CP ~/.BASHRC ~/.bash_history/tmp# Multiple data can be copied to the same directory at once! The last side must be the catalogue! 


3 Deleting a file or directory RM

-I ask for confirmation before deleting
-F Direct Delete, no need to confirm each
-R recursively Delete directories and subdirectories (often used when deleting directories)
Example

Example One: Delete the BASHRC that you just created in the example of CP! [Email protected] ~]#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: With the help of the million bytes *, the name of the file that starts at/tmp and BASHRC is deleted:[Email protected] tmp]#rm-i bashrc*# Note that the asterisk, which represents 0 to infinite number of arbitrary bytes Oh! What a good thing to do! example Three: Remove the/tmp/etc/directory created in the CP paradigm! [Email protected] tmp]#rmdir/tmp/etcRmdir:etc:Directory not empty<==, you can't delete it! Because this is not an empty directory! [Email protected] tmp]#rm-r/tmp/etcRm:descend into directory '/tmp/etc '?y.... (middle omitted) .... # 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're 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 alias's specified options! As Alias, we'll talk about it in bash! Example four: Delete a file with a-start[Email protected] 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-<== File size is 0, so it is empty file[Email protected] tmp]#rm-aaa-Try ' rm--help ' for more information.<== because "-" is the option! So the system misjudged! [Email protected] tmp]#rm./-aaa-


4 Moving or renaming files

-I interactive mode operation. If the MV operation will cause an overwrite of the target file that already exists, the system asks whether to override it and asks the user to answer Y or N, which avoids overwriting the file by mistake
-F disables interactive operation. Do not give any hint when the MV operation is to overwrite an existing target file
-U updates are made if the destination file already exists and the source file is newer. Equivalent to update

Example

Example One: Copy a file, create a directory, move the file to the directory[Email protected] ~]#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[Email protected] tmp]#MV Mvtest Mvtest2 <== that's the name! Simple ~ (because Mvtest2 does not exist)In fact, there is an interesting command under Linux, 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 move them all to/tmp/mvtest2[Email protected] tmp]#CP ~/.BASHRC BASHRC1[Email protected] tmp]#CP ~/.BASHRC BASHRC2[Email protected] tmp]#mv Bashrc1 bashrc2 mvtest2# Notice this way, if there are multiple source files or directories, the last destination must be "Directory!" "# meaning to say, move all the data to the meaning of that directory! 

5 Get path file name and directory name

basename/etc/sysconfig/networkNetwork         <== is simple! Get the last file name ~dirname/etc/sysconfig/network/etc/sysconfig  <== made into a directory name! 

Source: http://cjjwzs.iteye.com/blog/1035763 Http://vbird.dic.ksu.edu.tw/linux_basic/0220filemanager.php#ls

File and directory management for Linux

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.