SHELL Command learning notes

Source: Internet
Author: User
Tags printable characters

SHELL Command learning Notes 1. mkdir command 1. Purpose: create a directory 2. parameters: (1).-p: if some directories in the path do not exist, the directory is automatically created, the default mode is determined by the umask in the execution environment, that is, mode = 0777-umask. If there is no parameter, the directory in the path does not exist. (2),-m: Specifies the directory permission; 3. Example 1 [root @ jfht ~] # Umask 0022 [root @ jfht ~] # Mkdir dir1 [root @ jfht ~] # Ls-l dir1 total 0 ls command keep up with the-d parameter to print the information of the Directory itself. [Root @ jfht ~] # Ls-ld dir1 drwxr-xr-x 2 root 4096 07-20 dir1 permission setting mode = 0755 = 0777-0022 [root @ jfht ~] # Example 2 [root @ jfht ~] # Mkdir-m 0700 dir2 [root @ jfht ~] # Ls-ld dir2 drwx ------ 2 root 4096 07-20 20:47 dir2 [root @ jfht ~] # Example 3 [root @ jfht ~] # Mkdir dir3/x/y/z mkdir: the directory "dir3/x/y/z" cannot be created: No file or directory [root @ jfht ~] # Mkdir-p dir3/x/y/z [root @ jfht ~] # Ls-ld dir3 drwxr-xr-x 3 root 4096 07-20 20:48 dir3 [root @ jfht ~] # Ls-ld dir3/x drwxr-xr-x 3 root 4096 07-20 20:48 dir3/x [root @ jfht ~] # Ls-ld dir3/x/y drwxr-xr-x 3 root 4096 07-20 20:48 dir3/x/y [root @ jfht ~] # Ls-ld dir3/x/y/z drwxr-xr-x 2 root 4096 07-20 20:48 dir3/x/y/z [root @ jfht ~] # Example 4: Create a project directory structure [root @ jfht ~] # Mkdir-p project/{lib/ext, bin, src, doc/{html, info, pdf}, demo/stat/a} [root @ jfht ~] # Tree project/| -- bin | -- demo | '-- stat |' -- a | -- doc | -- html | -- info | '-- pdf | -- lib |' -- ext '-- src 11 directories, 0 files 2. cp command 1. Purpose: copy a file or directory 2. parameter a: equivalent to-pdr;-d: if the source file is a link file, copy the link file property instead of the file itself.-f: force, if you have repeated or other questions, you will not be asked, but will be forced to copy.-I: If the destination region (destination) already exists, you will first ask if the actual action exists during overwriting! -L: Creates a hard link, instead of copying the file itself.-p: Copies the file together with its attributes, rather than using the default attributes; -r: recursive continuous replication, used for directory replication;-s: copying to a symbolic link, that is, a "shortcut" file;-u: if destination is earlier than source, update destination! 3. Example 1: Copy. bashrc under the Home Directory to/tmp and rename it bashrc [root @ linux ~] # Cd/tmp [root @ linux tmp] # cp ~ /. Bashrc [root @ linux tmp] # cp-I ~ /. Bashrc # repeat the two actions. Because bashrc already exists under/tmp and the-I parameter is added, # the user will be asked whether to confirm before overwriting! You can press n or y! # However, if you do not want to ask, add the-f parameter to forcibly overwrite it! Example 2: copy/var/log/wtmp To the bottom of/tmp [root @ linux tmp] # cp/var/log/wtmp. <= the directory to be copied. do not forget [root @ linux tmp] # ls-l/var/log/wtmp-rw-r-1 root utmp 71808 Jul 18/var/log/wtmp- rw-r-1 root 71808 Jul 18 wtmp # Have you noticed it ?! Without adding any parameters, the owner of the file will change, and the permissions will be changed ~ # This is a very important feature! Be careful! In addition, the time for establishing a file connection is not the same! # If you want to copy all the features of an archive, add-! [Root @ linux tmp] # cp-a/var/log/wtmp wtmp_2 [root @ linux tmp] # ls-l/var/log/wtmp wtmp_2-rw-r -1 root utmp 71808 Jul 18/var/log/wtmp-rw-r-1 root utmp 71808 Jul 18 46 wtmp_2 # now! The data features are exactly the same! Really nice ~ This is the feature of-! Example 3: Copy all the content in the/etc/directory to the/tmp directory [root @ linux tmp] # cp/etc/tmp cp: omitting directory '/etc' <= if it is a directory, it cannot be copied directly, add the-r parameter [root @ linux tmp] # cp-r/etc/tmp # to emphasize it again! -R indicates that directories can be copied. However, permissions on files and directories are changed ~ # Therefore, you can also use cp-a/etc/tmp to issue instructions! Example 4: Create a symbolic link for bashrc copied in Example 1) [root @ linux tmp] # ls-l bashrc-rw-r-1 root 395 Jul 18 22:08 bashrc [root @ linux tmp] # cp-s bashrc bashrc_slink [root @ linux tmp] # cp-l bashrc bashrc_hlink [root @ linux tmp] # ls-l bashrc *-rw-r-2 root 395 Jul 18 bashrc-rw- r-2 root 395 Jul 18 bashrc_hlink lrwxrwxrwx 1 root 6 Jul 18 bashrc_slink-> bashrc # The bash Rc_slink is caused by the-s parameter and creates a "shortcut". # You will see it on the rightmost side of the file, the location where the file is connected is displayed! # The bashrc_hlink is interesting! After this file is created, bashrc and bashrc_hlink # All parameters are the same, but the number of links in the second column changes to 2 ~ Instead of the original 1 Oh! # The similarities and differences between the two links will be introduced in the next chapter! Example 5: If ~ /. Bashrc/tmp/bashrc is copied to [root @ linux tmp] # cp-u ~ /. Bashrc/tmp/bashrc # This-u feature is copied only when the target file is different from the source file. # So, it is often used in "backup" work! ^_^ Example 6: copy bashrc_slink from example 4 to bashrc_slink_2 [root @ linux tmp] # cp bashrc_slink bashrc_slink_2 [root @ linux tmp] # ls-l bashrc_slink * 1_1 root 6 Jul 18 bashrc_slink -> bashrc-rw-r-1 root 395 Jul 18 bashrc_slink_2 # This example is also very interesting! The original file is a link file, but the actual file of the link file is copied. # That is to say, if no parameter is added, the source file is copied instead of the link file attribute! # To copy the attributes of a link file, you must use the-d or-a parameter! Example 7: Copy. bashrc and. bash_history in the home directory to the/tmp directory [root @ linux tmp] # cp ~ /. Bashrc ~ /. Bash_history/tmp # You can copy multiple data to the same directory at a time! This cp has many functions, and we often use this command because we are copying some data. In general, if we copy others' data (of course, you must have read permission for this file! When copying _ ^), we always want to copy the data to our own end. Therefore, in The Preset conditions, the cp source file and target file have different permissions, the owner of the target file is usually the instruction operator. For example, in the example 2 above, because I am a root user, the copied file owner and group are changed to the root user! Do you understand this ?! ^_^ Because of this feature, some special permission files, such as password files (/etc/shadow) and some configuration files, need special attention during Backup, you cannot directly copy files using cp, but you must add parameters such as-a or-p that can completely copy files! In addition, if you want to copy an archive to other users, you must also note the permissions of the archive (including read, write, execute, and owner). Otherwise, other people still cannot revise your archives! Note! As for the above example, the fourth example is the most interesting. Using-l and-s will create the so-called link file ), however, these two links do not show the same situation. What is this? The-l is the so-called hardlink, and the-s is the symboliclink. This is not introduced here, because this involves the knowledge of I-node, which we have not introduced yet, the next chapter will discuss the link issue! In short, because cp has various archive attributes and permissions, you must be clear about whether to retain the source archive information completely during replication? Is the source file symbolic link file )? Is the source file special, such as FIFO or socket? Is the source file a directory? Iii. mv command (1). Purpose: Move or rename the file or directory (2). Parameter:-B or -- backup. If you need to overwrite the document, overwrite the previous backup. -F or -- force: if the target document or directory is the same as the existing document or directory, the existing document or directory will be overwritten. Ask the user before-I or -- interactive overwriting. -S or -- suffix = are used together with the-B parameter. You can specify the end of the word to be appended to the backup document. -U or -- update when moving or changing the document name, if the target document already exists and its date is newer than the source document, the target document is not overwritten. -V or -- verbose displays detailed information during execution. -V = or -- version-control = and-B parameters can be used together to specify the backup method. -- Help: displays help. -- Version: displays the version information. (3) example [root @ xm_41 mv] # ll total 8-rw-r -- 1 root 5 10-15 into-rw-r -- 1 root 4 10 -15:25 out [root @ xm_41 mv] # mv-B into out mv: overwrite "out "? Y [root @ xm_41 mv] # ll total 8-rw-r -- 1 root 5 10-15 out-rw-r -- 1 root 4 10- 15: 25 out ~ When the-B parameter is used, the overwritten files are automatically backed up. The default value is ~; During overwriting, the [root @ xm_41 mv] # mv-B-S bak into out mv is displayed: overwrite "out "? Y [root @ xm_41 mv] # ll total 4-rw-r -- 1 root 0 10-15 out-rw-r -- 1 root 5 10- 15: 25 at, if outbak has the-B parameter, add the-S parameter at the same time, and specify the backup character [root @ xm_41 mv] # mv-B-S bak into out mv: overwrite "out "? Y [root @ xm_41 mv] # ll total 4-rw-r -- 1 root 0 10-15 out-rw-r -- 1 root 5 10- at, if outbak is added with the-f parameter, no prompt will be given; the default value is-I; [root @ xm_41 mv] # mv-u out outbak [root @ xm_41 mv] # ll total 4-rw-r -- 1 root 0 10-15 out-rw -r -- 1 root 5 10-15 outbak uses the-u parameter, it is found that out is older than outbak, only do [root @ xm_41 mv] # mv-u out outbak [root @ xm_41 mv] # ll total 4-rw-r -- 1 root 0 1 0-15 out-rw-r -- 1 root 5 10-15 outbak mv can be used to change the name, if the specified file or directory does not exist, run the rm command (1). Purpose: delete the file or directory (2) the-d or -- directory parameter directly deletes the hard connection data of the directory to be deleted to 0 and deletes the directory. -F or -- force forcibly deletes a file or directory. -I or -- interactive ask the user before deleting an existing file or directory. -R,-R, or -- recursive processing: all files and subdirectories under the specified directory are processed together. -V or -- verbose displays the command execution process. -- Help Online help. -- Version: displays version information (3). Examples (examples of only popular music videos are similar. Examples are not provided here). V. ls command (1). Purpose: List directories (2) parameter-a lists all files in the directory, including. hidden file. -B lists non-output characters in the file name in the form of a backslash and a character number (just like in C. -C: the time when the I node of the output file is modified and sorted accordingly. -D: display the directory like a file instead of the file under it. -E outputs all the information of the time, rather than the brief information. -F-U does not sort the output files. -G is useless. -I: index information of the I node of the output file. -K indicates the file size in the form of k bytes. -L list the details of a file. -M horizontal output file name, with "," as a grid character. -N is replaced by the UID and GID of the number. -O displays the details of the file except the group information. -P-F attaches a character to each file name to indicate the file type. "*" indicates an executable normal file, and "/" indicates a directory; "@" indicates the symbolic link; "|" indicates the OS; "=" indicates the intercept (sockets ). -Q used? Replace non-printable characters. -R sorts directories in reverse order. -S outputs the file size after each file name. -T is sorted by time. -U is sorted by the time when the file was last accessed. -X is output by column and sorted horizontally. -A displays all files except "." and. -B does not output "~" End of the backup file. -C is output by column and vertically ordered. -G indicates the group information of the output file. -L list the link file names instead of the linked files. -N does not limit the file length. -Q: Enclose the output file name in double quotation marks. -R: list all files in subdirectories. -S is sorted by file size. -X is sorted by the file extension (the character after the last. -One Line outputs only one file. -- Color = no: The color file name is not displayed. -- help displays help information on the standard output. -- Version outputs the version information on the standard output and exits. (3) The example only lists sub-directories 1. ls-F | grep/$ or aliassub = "ls-F | grep/$" (linux) 2. ls-l | grep "^ d" or ls-lL | grep "^ d" (Solaris) run the following command to calculate the number of files and directories in the current directory: # ls-l * | grep "^-" | wc-l ---- to count files # ls-l * | grep "^ d" | wc-l ----- to countdir display color Directories open/etc/bashrc in the list, add the following line: alias ls = "ls -- color". When bash is started next time, the colored directory list can be displayed as in Slackware. The color meaning is as follows: 1. blue --> directory 2. green --> executable file 3. red --> compressed file 4. light blue --> link file 5. Gray --> other files ls-tl -- time-style = full-iso sshd ls-ctl -- time-style = long-iso ls command meaning: list displays the file name in the current directory. Note that the parameter is not added to show the names of all files and directories except hidden files. 1) ls-a displays all files in the current directory, including hidden files] # ls-.. gnome2.nautilus... gnome2_private oracle_rpm. bash_profile. gtkrc-1.2-gnome2 tnsnames. ora. bashrc. ICEauthority types. h note that the file is hidden in the format of ". "(English ending. 2) ls-l displays the file and its details.] # Ls-l total 5-rw-r -- 1 root 1668 Oct 3 2007 anaconda-ks.cfg drwxr-xr-x 2 root 4096 Nov 6 aa display file details what do they represent? The blue area above is used as an example. Total 5 indicates that the total file size in the current directory is 5 K (the size of each directory is calculated as 4 K). drwxr-xr-x has three conditions for the first character: "-" indicates a common file, "d" indicates a directory, "l" indicates a connection file, and "B" indicates a device file. Each of the following nine characters is a group of three, representing the file owner, the user group where the file owner is located, and the permissions of other users on the file. The three characters in each group indicate the read, write, and execute permissions respectively. If no permission exists, use. The execution permission has two optional characters: "x" indicates executable, and "s" indicates a set of interface files. The following number 2 indicates the number of directory files under the "aa" Directory (This number = number of Hidden Directories + number of common Directories ). Go to the "aa" directory and run the ls-al command. (To see the hidden file, add the-a parameter.)] # ls-al total 8 drwxr-xr-x 2 root 4096 Nov 6. drwxr-x --- 14 root 4096 Nov 6 .. (2 in the above 3rd rows indicates that there are two sub-directories in the current directory, that is. and .. 14 In the above 4th rows indicates that there are 14 subdirectories in the top-level directory of this directory .) Next, the root represents the owner of the file (directory) as the user root, and the next root represents the file (directory) the user group is the root 4096 group, which indicates the file size (in bytes). The directory size is always 4096 bytes. Nov 6 indicates the modification time of the file (directory. Aa indicates the name of the file (directory. 3) The default color indicates a common file. For example, install. log Green indicates an executable file. For example, rc. news indicates the tar package file in red. For example, vim-7.1.tar.bz2 blue represents a directory file. For example, aa indicates an image file. For example, Sunset.jpg indicates a link file. For example, rc4.d (this type of file is equivalent to a shortcut) indicates a device file in yellow. For example, fd0 4. -T is sorted by the last modification time. -S is sorted by file size. (Uppercase S)-r is sorted in reverse order. -H: the file size increases readability (for example, 1 K 234 M 2G)

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.