Linux running time (5) (laruence's Private food notes)

Source: Internet
Author: User
Tags touch command
Linux running time (5) (laruence's Private food note)-Linux general technology-Linux technology and application information. The following is a detailed description.
Although there are a lot of things to do, the progress of learning Linux is not very fast, but after all, it is still stuck-this state makes yourself feel full. In many cases, learning is a pleasure.

7.2 manage files and directories

Ls command usage:
[Root @ linux ~] # Ls-alF? Color = never ~ (Color is not displayed, but the type indicated by this file name is displayed at the end of the file name)
-F: attaches Data Structures Based on files, directories, and other information, for example, *, indicating executable files;/, indicating directories; =, indicating socket files; l: indicating FIFO files
-A: All files are listed together with hidden files (Files starting .).
-L: Lists long data strings, including object attributes.
-T: sort by Time
./Indicates "under the current directory ".
[Root @ linux ~] # Ls-al? Full-time (the modification time of the entire file is displayed)
Copy, remove, and delete: cp (copy), rm (rename), mv (move) rm (remove delete)
1. Copy
[Root @ linux ~] # Cp [-adfilprsu] target file of the source file
[Root @ linux ~] # Cp [options] source1 source2 source3.... directory
[Root @ linux ~] # Cp ~ /. Bashrc
[Root @ linux ~] # Cp ~ /. Bashrc
[Root @ linux ~] # Cp-I ~ /. Bashrc (I indicates that if the target file already exists, I will first ask if the operation is true when overwriting)
Cp: overwrite 'basrhc '? N (ask the user to confirm before overwriting. Press the n or y key. However, if you do not want to ask, add the-f parameter to emphasize direct overwriting)

Copy/var/log/wtmp to/tmp:
[Root @ linux tmp] # cp/var/log/wtmp. (copy to the current directory tmp. Do not forget the last one)
[Root @ linux tmp] # ls-l/var/log/wtmp (without adding any parameters, the file owner will change and the permissions will also change ), if you want to copy all the attributes of the file together, you can add-
[Root @ linux tmp] # cp-a/var/log/wtmp wtmp_2
[Root @ linux tmp] # ls-l/var/log/wtmp wtmp_2
Copy all contents in the/etc/directory to/tmp
[Root @ linux tmp] # cp-r/etc/tmp (if it is a directory, you cannot copy it directly. You need to add the-r parameter, but the file and directory permissions will be changed, therefore, you can use cp-a/etc/tmp to execute the command)
Create a symbolic link for the copied bashrc)
[Root @ linux tmp] # ls-l bashrc
[Root @ linux tmp] # cp-s bashrc bashrc_slink (copy it as a symbolic connection file symbolic link, that is, a shortcut file)
[Root @ linux tmp] # cp-l bashrc bashrc_hlink (create a hard link connection file instead of copying the file itself)
[Root @ linux tmp] # ls-l bashrc *
If ~ /. If bashrc is newer than/tmp/bashrc, copy
[Root @ linux tmp] # cp-u ~ /. Bashrc/tmp/bashrc (when the target file is different from the source file, it will be copied, so it is usually used for backup)
Copy the claimed bashrc_slink to bashrc_slink_2.
[Root @ linux tmp] # cp bashrc_slink bashrc_slink_2
[Root @ linux tmp] # ls-l bashrc_slink *
(The original object to be copied is the connection file, but the actual object to be connected is copied. That is to say, if no parameter is added, the source file is copied, not the properties of the connected file. To copy the properties of the connection file, use the-d or-a parameter.
Copy. bashrc and. bash_history in the home directory to/tmp
[Root @ linux tmp] # cp ~ /. Bashrc ~ /. Bash_history/tmp (copy multiple data to the same directory at a time) (note the space)

Note: When copying a password file (/etc/shadow) and some setting files, you cannot directly copy it with cp, but you must add-a or-p, these parameters can be used to completely copy the file permission. In addition, if you want to copy files to other users, you must pay attention to the file permissions. (Including read/write execution and file owner). Otherwise, other users cannot modify your file)
During replication, you must be clear about whether the source file information needs to be fully retained,
Whether the source file is a symbolic connection file
Whether the source file is a special file, such as FIFO or socket
Whether the source file is a directory
2. rm (delete a file or directory)
[[Root @ linux ~] # Rm [-fix] file or directory
-F: force indicates force deletion.
-I: interactive mode. Before deletion, you are asked if you want to perform the operation.
-R: Submit for deletion. It is often used to delete directories.

Create a file and delete it.
[Root @ linux ~] # Cd/tmp
[Root @ linux tmp] # cp ~ /. Bashrc
[Root @ linux tmp] # rm-I bashrc (if the-I parameter is added, you will take the initiative to ask. If you do not need to ask, add the-f parameter)
Delete A non-empty directory:
[Root @ linux tmp] # mkdir test
[Root @ linux tmp] # cp ~ /. Bashrc test/
[Root @ linux tmp] # rmdir test (cannot be deleted because it is an empty directory)
[Root @ linux tmp] # rm-rf test
The DELETE command is equivalent to the del command under DOS. If you want to delete all contents in the directory, for example, if there are subdirectories in the subdirectory, you need to use the-rf parameter.

Mv (move files and directories, or rename)
[Root @ linux ~] # Mv [-fiv] source destination
[Root @ linux ~] # Mv [options] source1 source2... directory

Parameter:-f: Forced direct movement without inquiry
-I: If the target file already exists, you will be asked if it is overwritten.
-U: if the target file already exists and the source file is newer, it will be updated.
[Root @ linux ~] # Cd/tmp
[Root @ linux tmp] # cp ~ /. Bashrc
[Root @ linux tmp] # mkdir mvtest
[Root @ linux tmp] # mv bashrc mvtest
(Move the file to a directory)
Rename the directory name to mvtest2.
[Root @ linux tmp] # mv mvtest mvtest2 (rename) (rename can also be used to rename)
Create two more files and move them all to/tmp/mvtest2.
[Root @ linux tmp] # cp ~ /. Bashrc bashrc1
[Root @ linux tmp] # cp ~ /. Bashrc bashrc2
[Root @ linux tmp] # mv bashrc1 bashrc2 mvtest2 (Note: if there are multiple source files or directories, the last target file must be a directory, meaning, move all the data to this directory)
You can also use-u (update) to test the new and old files to see if they need to be moved. Another use is to change the file name. The rename command can be used to change the file names of a large number of files ,.
3. Get the file name and Directory Name of the path
Obtain the file name or directory name, which is usually determined during programming. Therefore, these commands can be used in the shelle script in the third part.
[Root @ linux ~] # Basename/etc/sysconfig/network
[Root @ linux ~] # Dirname/etc/sysconfig/network
The retrieved directory name is changed.
7. View File Content
Cat: displays the file content starting from the first line.
Tac: Display from the last line. It can be seen that tac and cat are in the opposite alphabetic order.
Nl: displays the output row number.
More: displays the file content one page at a time.
Less: similar to more, but can flip the page forward
Head: only the first few rows
Tail: only the last few rows
Od: Read File Content in binary mode

View the file content directly:
Cat (concatenate ):
View the content of the/etc/issue file:
[Root @ linux ~] # Cat/etc/issue
Show travel number by the way
[Root @ linux ~] # Cat-n/etc/issue
Display the content of/etc/xinetd. conf in full (including special characters)
[Root @ linux ~] # Cat-A/etc/xinetd. conf (cannot find this file after running ?)
Tac: reverse display
[Root @ linux ~] # Tac/etc/issue
Nl: Add row number when displaying
[Root @ linux ~] # Nl [-bnw] File
View on pages:
More: page by page
[Root @ linux ~] # More/etc/man. config
Less: one page and one page flip
[Root @ linux ~] Less/etc/man. config

Data selection: head and tail are selected as behavior units
1. head (retrieve the first few rows)
[Root @ linux ~] # Head [-n number] File
-N: Followed by a number, indicating the meaning of several rows.
[Root @ linux ~] # Head/etc/man. config (the first 10 rows are displayed by default)
[Root @ linux ~] # Head-n 20/etc/man. config
2. tail (the last few rows after removal)
Non-plain text file: od
The execution file is usually a binary file. garbled characters are generated when you use the commands mentioned earlier. In this case, you can use the od command to read
[Root @ linux ~] # Od [-t TYPE] File
A: Use the Default Characters for output.
C: use ASCII characters for output
D: output data in decimal format. Each integer occupies size bytes.
F: Use a floating point value to output data. Each number occupies size bytes.
O: Use octal to output data. Each integer occupies size bytes.
X: returns data in hexadecimal format. Each integer occupies size bytes.
[Root @ linux ~] # Od-t c/usr/bin/passwd

Modify the file time and create a letter file: touch
Modification time (mtime, modification time): This time is updated when the content data of the file is changed. (By default, ls displays the mtime of the file, that is, the time when the content of the file was last changed)
Status time (ctime, Status time): This time is updated when the file Status changes. For example, when the permissions and attributes are changed, the time is updated.
Access time (atime): This read time is updated when the file content is read. For example, use cat to read ~ /. Bashrc will update atime.
[Root @ linux ~] # Touch [-acdmt] File
-A: only modify access time
-C: only change the time security without creating a file
-D: Followed by a date, or use-date = "date or time"
-M: only modify mtime
-T: Later, you can take the time to love you, in the format of [YYMMDDhhmm]

Create an empty file:
[Root @ linux ~] # Cd/tmp
[Root @ linux tmp] # touch testtouch
[Root @ linux tmp] # ls-l testtouch
Change ~ /. Copy bashrc to bashrc. If the copy is complete, check its date.
[Root @ linux tmp] # cp ~ /. Bashrc
[Root @ linux tmp] # ll ~ /. Bashrc; 11 -- time = atime ~ /. Bashrc; ll? Time = ctime ~ /. Bashrc (ll is the alias of the ls-l command, and ";" then two commands are issued at the same time.
Modify the bashrc file and set the date to two days ago.
[Root @ linux tmp] # touch-d "2 days ago" bashrc (mtime and atime have all changed-two days ahead of schedule, but ctime has not changed)
Change the bashrc date
[Root @ linux tmp] # touch-t 0507150202 bashrc
[Root @ linux tmp] # ll bashrc; ll? Time = atime bashrc; ll? Time = ctime bashrc
The touch command can modify the date and time of a file (mtime and atime), and create an empty file. However, even if you want to copy a file and copy all attributes, you cannot copy the ctime attribute. Ctime can record the last time the status has changed. The most important file attribute time is mtime-the modification time.
Default and hidden permissions for files and directories
Default and hidden permissions for files and directories
A file has basic attributes such as r, w, x, and whether it is a directory (d), a file (-), or a connection file (l. In linux, you can also set other system security attributes, use chattr settings, and lsattr. The most important thing is to set unmodifiable features. Even the owner of the file cannot modify the file.
Default file permission: umask (specify the default attribute value when the current user creates a file or directory)
[Root @ linux ~] # Umask (view the score set for the number type permission)
[Root @ linux ~] # Umask-S (note that S is capitalized) (add the Symbolic parameter to display the permission in the Symbolic type)
Umask specifies the "permission to be removed by default" because r, w, and x are 4, 2, and 1 respectively. If you want to remove the write permission, enter 2. If you want to remove the read and execution permissions, enter 5. Because umask is 022, the user is not removed from the attribute, and the group and others attributes are removed from 2, that is, the write attribute.
[Root @ linux ~] # Umask
[Root @ linux ~] # Touch test1
[Root @ linux ~] # Mkdir test2
[Root @ linux ~] # Ll
[Root @ linux ~] # Umask 002
[Root @ linux ~] # Touch test3
[Root @ linux ~] # Mkdir test4
[Root @ linux ~] # Ll (? Running error)

File hiding Properties
Chattr (set file hiding attributes)
[Root @ linux ~] # Chattr [+-=] [Asacdistu] file or directory name
[Root @ linux ~] # Cd/tmp
[Root @ linux tmp] # touch attrtest
[Root @ linux tmp] # chattr + I attrtest (this prevents a file from being deleted, renamed, set connections, and added data. Provides great help for system security)
[Root @ linux tmp] # rm attrtest

Show the hidden property of the file: lsattr
[Root @ linux ~] # Lsattr [-aR] file or directory
-A: displays the properties of hidden files.
-R: Data in the subdirectories are also listed.
[Root @ linux tmp] # chattr + aij attrtest
[Root @ linux tmp] # lsattr
If you want to set the important password record file/etc/shadow to have the I attribute, you need to remove the I attribute when adding users.
File special permissions: SUID/SGID/Sticky Bit
[Root @ linux ~] # Ls-ld/tmp; ls-l/usr/bin/passwd
Set UID: the s and t permissions are created to allow the general user to temporarily have the permissions of the program owner when executing certain programs. The account and password are actually stored in/etc/passwd and/etc/shadow. The permission for the/etc/shadow file is-r --------, and its owner is root. In this permission, only root can be forcibly stored. However, when dmstai of a general identity updates its password, it can use the/usr/bin/passwd program to update its password. In other words, an identity user can access the/etc/shadow password file and love you. This is because of the help of S permission. When s permission is in user's x, it is similar to-r-s -- x, which becomes Set UID, SUID for short, the UID indicates the User ID, while the User indicates the owner (root) of the Program (/usr/bin/passwd ). When the dmtsai user executes/usr/bin/passwd, the root permission of the file owner can be obtained temporarily.
SUID is the permission of the object owner during execution. It can only be used for binary file security and cannot be used for batch files (shell scripts. This is because the shell script only calls many binary execution files. Therefore, the SUID permission part depends on the program settings that the shell script calls, rather than the shell script itself. SUID is invalid for the directory.
Set GID:
If the permissions of s are in the user group, Set GID can be used in two aspects:
1. file: If the SGID is set to a binary file, its valid user group (valid group) is used to execute the program no matter who the user is) the user group owner (group id) of the program)
2. Directory: If SGID is set to directory A, the user group of the files or directories created in directory A will be the user group of the Directory.
Generally, SGID is mostly used in project development by a specific multi-person team and rarely used in the system.
Sticky Bit
Currently, Sticky Bit (SBit) is only valid for directories and has no effect on files. The role of SBIT on a directory: in a directory with SBit, if the user has the w and x permissions under this directory, when the user creates a file or directory under this directory, only the file owner and root have the right to delete the file.
SUID/SGID/SBIT permission settings: You must use numbers to change the permission. Use a number to change the permission to a combination of three numbers. If you add a number before the three numbers, the first number indicates these attributes. When you use chmod xyz filename to set the filename attribute, it is assumed that there is no SUID, SGID, and Sticky Bit)
Sticky Bit: 1
Assume that you want to change the attribute of a file to "-rwsr-xr-x". Because s is in the user permission, it is SUID. Therefore, you need to add 4 before 755, that is, use chmod 4755 filename to set. In addition, there are also the generation of big S and Big T.
[Root @ linux ~] # Cd/tmp
[Root @ linux tmp] # touch test
[Root @ linux tmp] # chmod 4755 test; ls-l test
[Root @ linux tmp] # chmod 6755 test; ls-1 test
[Root @ linux tmp] # chmod 1755 test; ls-l test
Both s and t Replace the x parameter, because user, group, and others do not have the x executable flag and use 7666, so S and T are empty. SUID indicates that the file has the permission of the file owner during execution, but the file owner cannot execute the file and has no permission to use it for others. Of course, it is empty.
File Type: file
If you want to know the basic information of a file, such as ASCII, data file, or binary file, and whether the dynamic library is used, you can use the file command to view it.
[Root @ linux ~] # File ~ /. Bashrc
Search for files:
Find: slow speed and hard disk space consumption.
Whereis and locate use databases to search for data. Therefore, they are fast and do not actually search hard disks.
[Root @ linux ~] # Which passwd
[Root @ linux ~] # Which traceroute-a (-a: list all the commands that can be found, not just the names of the first commands found)
The which command searches for the file name of the execution File Based on the PATH defined in the PATH environment variable. Therefore, the focus is to find the execution file security. Which is followed by the complete file name. If the-a parameter is added, All executable files with the same name that can be found can be listed, rather than displaying only the first one.
Whereis (find a specific file)
[Root @ linux ~] # Whereis [-bmsu] file or directory name
Parameter:-B: binary file only
-M: only find the file in the manual path of the Help file.
-S: only find the source file
-U: file with no help file
[Root @ linux ~] # Whereis passwd (use whereis to find the executable file binary, then add the-B parameter.

[Root @ linux ~] # Locate filename
[Root @ linux ~] # Locate passwd (in the complete file name-including the path name, as long as there is passwd, it will be displayed. If you forget the complete file name, you can use this method)
[Root @ linux ~] # Find [PATH] [option] [action]
Chapter 8 linux disk and file system management
Load U Disk: put into the U disk, and then use the fdisk-1 to list all the partitions, will find that the system more/dev/sda [] SCSI hard disk. The USB hard disk number is also the Code of/dev/sd [a], and the first USB code is/dev/sda. If the USB hard disk has a partition, some numbers will appear. For portable hard disks, only/dev/sda1. Assume that the portable hard disk is/dev/sda1 and load it to/mnt/usb.
[Root @ linux ~] # Mkdir/mnt/usb
[Root @ linux ~] # Mount-t vfat/dev/sda1/mnt/usb
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: 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.