Linux Command sorting (commonly used) and linux Command sorting

Source: Internet
Author: User

Linux Command sorting (commonly used) and linux Command sorting

Common Linux commands
1. There are two types of users in Linux: Super Users (root) and normal users.
A) Super User: You can do anything in linux without any restrictions.
B) common users: Do limited things in linux.
The Super User command prompt is "#", and the normal USER command prompt is "$ ".
Command: su [user name]
Function: Switch users.
For example, to switch from the root user to the normal user, use su user.
To switch from a common user to a root user, use su root (root can be omitted). The system will prompt you to enter the password of the root user.
2. Add a user.
Command: useradd Username
Function: Add a common user.
For example, to add a common user user1, you can use
Useradd user1. After using this command, the system will create a directory named user1 under the directory "/home.
3. Set the password
Command: passwd User Name
Function: set or modify the password of the user name.
For example, if we want to set a password 123456 for the user1 user we just created, passwd user1 will be used, and the system will prompt you to enter the new password.
4. view files or directories
Command: ls [Option] [directory or file]
Function: For a directory, this command lists all subdirectories and files under the directory. File names and other information are listed. The Linux File System does not execute a file based on the suffix, but depends on whether the file has the executable permission.
Common options include:
-A: displays all subdirectories and files in the specified directory. For example, to list all subdirectories and files under the "/root/home" directory, use
Ls-a/root/home.
-L list the details of all directories and files under the specified directory. For example, to list all subdirectories and files under the "/root/home" directory, use
Ls-l/root/home. The details listed in each row are as follows:
File type and number of permission connections file owner file group file size last modified file name.
In the information displayed using the ls-l command, the start is a string consisting of 10 letters. The first character indicates the file type, which can be one of the following types:
-: Common File
D: Directory
L: Symbolic Link
B: block Device Files
C: character Device File
P: Named Pipe
S: socket File
The following nine characters indicate the object access permissions. They are divided into three groups, each with three characters.
The first group indicates the permissions of the file creator, the second group indicates the permissions of users in the same group, and the third group indicates the permissions of other users. The three characters in each group indicate the read, write, and execute permissions for the object.
The permissions are as follows: r (read), w (write), x (execution), and _ (no permission is set ). Each group can be represented by a number, such as r_x: 5, rw _: 6
R__: 4, the three groups can be represented by three numbers, such as rwxr_xr_x: 755, rw_r _ r__: 644.
The ls output content is colored. For example, the directory is blue and the compressed file is red. If there is no color, you can add the -- color = never parameter to indicate that the output is not colored, -- color = auto indicates automatic, -- color = always indicates that there is always a color.
For more detailed parameter descriptions, you can use the following method to get ls help: man ls.
5. Change the working directory.
Command: cd directory name
Function: change the working directory. Change the current working directory to the specified directory. For example, to switch the current directory to the "/home/user/0718" directory, use
Cd/home/user/0718.
Common directory switch commands:
Cd .. to parent directory
Cd/to the root directory
Cd ~ Go to the user's home directory ~ The same effect as running cd directly
Cd-to the last directory
6. display the current working directory.
Command: pwd
Function: displays the current directory of the user. For example
When cd/home/user/0718 is run, the command line displays
/Home/user/0718.
7. Create a directory
Command: mkdir [Option] dirname
Function: create a directory named "dirname" under the current directory. For example, to create a directory named "07181" under the current directory, use the command
Mkdir 07181. The system creates a 07181 directory under the current directory. You can use ls-l to view the directory.
8. delete a directory
Command: rmdir [Option] dirname
Function: Delete the subdirectory "dirname" under the current working directory. This subdirectory must be an empty directory. We just created an empty directory 07181. If we want to delete it, use rmdir 07181. In this case, use ls-l to list the data. In this case, 07181 is deleted. If you use this command to delete a non-empty directory, the deletion fails.
9. Copy files or directories
Command cp [Option] source file or directory target file or directory
Function: Copies the specified source file to the target file or multiple source files to the target directory.
Common parameters:
-F if a file with the same name as the source file exists in the target directory, the file will be overwritten directly. For example, copy the main. c file in the current directory to "/home/user/0718", and overwrite the file if the same name exists. Use:
Cp-f./main. c/home/user/0718.
If you rename the source file while copying the file, for example, copying the main. c file under the current directory to the "/home/user/0718" directory and naming it main1.c, use:
Cp-f./main. c/home/user/0718/main1.c.
-I is opposite to-f. When the target file exists in a file with the same name as the source file, the system will prompt whether to overwrite the file. In the above example, if a file with the same name as the source file exists in the target file during the copy process, You Need To prompt whether to overwrite the file, you only need to change-f in the above example to-I. For example
Cp-I./main. c/home/user/0718.
Cp-I./main. c/home/user/0718/main1.c.
-R if you want to copy a directory, the subdirectories and files under the directory will be copied at the same time. The target file must be a directory. For example, if you copy all the files and directories under the "/home/user/0718" directory to the "/home/user1" directory, use
Cp-r/home/user/0718/home/user1.
10. Move the file or directory.
Command: mv [Option] source file or directory target file or directory
Function: depending on the type of the second parameter in the mv command (target file or directory), the mv command renames the file or moves it to a new directory. When the second parameter type is file, the mv command renames the file. At this time, only one source file (or the source directory name) can be used ), it renames the given source file or directory to the given target file name. When the second parameter is an existing directory name, there may be multiple source files or directory parameters. The mv command moves the source files specified by each parameter to the target directory. When a file is moved across file systems, the mv copies the file first, and then deletes the original file. The link to the file will also be lost.
Parameters:
-I if there are duplicate names during the movement, the system will prompt whether to overwrite them.
-F if there is a duplicate name in the process of moving, it will be overwritten directly without a prompt.
For example, to rename the main. c file under "/home/user/0718" to main. cpp, use
Mv/home/user/0718/main. c/home/user/0718/main. cpp.
To move all content under "/home/user/0718" to "/home/user/0719", use
Mv-f/home/user/0718/home/user/0719.
11. delete files or directories
Command: rm [Option] file or directory
Function: it is easy to create files in linux. Files may become obsolete and useless at any time in the system. You can use the rm command to delete it. This command is used to delete one or more files or directories in a directory. It can also delete a directory and all its files and subdirectories. For linked files, only the links are deleted, and the original files remain unchanged. If the-r option is not available at the time of deletion, the directory will not be killed.
Parameters:
-F No prompt is given during the deletion process.
-I: an interactive prompt is displayed during the deletion process.
-R: If a directory is deleted, all directories and subdirectories under the directory are deleted.
For example, to delete main. cc main.exe under the "/home/user/0718" Directory
Use,
Rm-f/home/user/0718/main. cc/home/user/0718/main.exe
If you want to delete the "/home/user/0718" directory, use
Rm-rf/home/user/0718.
12. View File Content
Command: cat [Option] [file]
Function: view the content of the target file.
Parameters:
-B-to-Null Output row number
-E: display at the end of each line $
-N: Number of all rows output
-S does not output multiple empty rows.
For example, to view the content of main. cc in the current directory
It is used, cat main. cc.
Standard Input/Output and redirection:
The file descriptor is an integer that represents an open file. The standard three descriptive symbols are as follows:
Standard Input: generally refers to the keyboard, descriptor: 0
Standard output: generally refers to screen output, descriptor: 1
Error output: screen, descriptor: 2
Redirection symbol:
<Redirect input,> redirect output,> Add output, 2> error redirection, &> error and information redirection
Cat is often used together with redirection. Where,> indicates creation,> indicates append, <indicates end of an append
If cat has no parameters in the command line, it reads data from the standard input and sends it to the standard output.
There are four ways to create an empty file in linux:
Method 1: echo> a.txt (it seems that there is a byte)
Method 2: touch B .txt
Mode 3: cat> c.txt press ctrl + c to exit; or Ctrl + d
Method 4: After vi d.txt is entered, wq is exited.
13. display the first few lines of the file content
Command: head-n line numeric file name
Function: displays the first few rows of the target file.
For example, to display the first 10 lines of main. cc in the current directory, use
Head-n 10 main. cc.
14. display the last few lines of the file
Command: The value File Name of the tail-n line.
Function: displays the last few lines of the target file.
For example, the last 10 lines of the main. cc file under the "/home/user/0718/" directory are displayed. Use tail-n 10/home/user/0718/main. cc.
15. Pipeline and command replacement
Pipeline: a type of redirection. Like a catheter, the output of one program or command is used as the input of another program or command. Eg: # ls-l/etc | wc-w
Command replacement: it is similar to redirection, but the difference is that command replacement uses the output of one command as the parameter of another command. Commonly used formats: command1 'command2' or command1 $ (command2)
Example:
First, list all current information and redirect it to the aa file:
# Ls | cat> aa or ls> aa
Then, use command replacement to list all file information in the aa file.
# Ls-l 'cat A' or use ls-l $ (cat aa)


16. Create a mask for a file or directory
Umask indicates some permissions to be removed from all permissions when a file (0666) or directory (0777) is created. The value of umask for common users is 002, and that for Super Users is 022.
002 indicates that the owner's permissions are not removed when the directory is created, the group permissions are not removed, and the write attributes of other groups are removed.
After a file is created, the default permissions of normal users are 664 Super Users: 644.
After a directory is created, the default permissions of normal users are 775 Super Users: 755.
You can use umask to view the default mask value. Modify the mask value through umask 001.
17. Change directory or File Permissions
Function: the chmod command is very important for changing the access permissions of files or directories. You can use it to control access to files or directories.
Syntax: This command can be used in two ways. One is the text setting method that contains letters and operator expressions, and the other is the number setting method that contains numbers.
Note: The basic information about using ls-l long format to list files or directories is as follows:
File type and number of permission links name of the last modified file size in the file owner File Group
For permissions, the first group indicates the permissions of the file owner, the second group indicates the permissions of users in the same group, and the third group indicates the permissions of other users. The three characters in each group indicate the read, write, and execute permissions for the file. You can use chmod to modify permissions.
1. text setting method
Chmod [who] [+ |-| =] [mode] File Name // modify the permission of who in the specified file name Add/Remove/assign a value to mode
Parameters:
The operator who is one of the following letters or their combination:
U indicates "user", that is, the owner of a file or directory.
G indicates "same group users", that is, all users with the same group ID as the file owner.
O indicates "Other (others) Users ".
A Indicates "all (all) Users ". It is the default value of the system. That is, chmod + x 1. c indicates that all users have executable permissions.
The operation symbol can be:
+ Add a permission.
-Cancel a permission.
= Grant the given permission and cancel all other permissions (if any ).
Set the permissions indicated by mode to any combination of the following letters (who cannot be less used when the combination is used ):
R readable
W writable
X executable
File Name: list of files separated by spaces to change permissions. Wildcards are supported.
Multiple permission methods can be provided in a command line, separated by commas. For example, chmod g + r, o + r example enables the same group and other users to have the read permission on the file example.
2. Number setting method
First, we must understand the meaning of the property represented by numbers: 0 indicates no permission, 1 indicates executable permission, 2 indicates writable permission, 4 indicates readable permission, and then add it. Therefore, the format of the numeric attribute should be three Octal numbers from 0 to 7. The order is (u) (g) (o ).
For example, if you want the owner of a file to have two permissions: "read/write", you need to set 4 (read) + 2 (writable) to 6 (read/write ).
The general format of the number setting method is: chmod [mode] File Name
Example:
(1) text setting method:
Example 1: $ chmod a + x sort
That is, set the property of the file sort:
The file owner (u) adds the execution permission.
The execution permission is added to users in the same group as the file owner (g ).
Other users (o) add execution Permissions
Example 2: $ chmod ug + w, o-x text
That is, set the text attribute of the file:
Add write permission to file owner (u)
Add write permission for users in the same group as the file owner (g)
Other users (o) delete execution Permissions
Example 3: $ chmod a-x mm.txt
$ Chmod-x mm.txt
$ Chmod ugo-x mm.txt
The preceding three commands Delete the execution permission of the mm.txt file, and set the object to all users.
(2) number setting method:
Example 1: $ chmod 644 mm.txt
$ Ls-l
Set the attributes of the file mm.txt:
-Rw-r -- 1 inin users 1155 Nov 5 mm.txt
File owner (u) inin has read and write permissions
Users in the same group as the file owner (g) have read permission
Others (o) have read permission
Example 2: $ chmod 750 wch.txt
$ Ls-l
-Rwxr-x --- 1 inin users 44137 Nov 12 9:22 wchtxt
That is, set the properties of the wchtxt file:
File owner (u) inin readable/writable/executable
Readable/executable permission of persons in the same group as the file owner
Others (o) do not have any Permissions
18. File Search
Command: find start Directory Search Condition operation
Function: Search for the price in the specified directory structure and perform the specified operation.
The Search Condition of this command can be a combination of logical operators not, and, or.
(1) and: logical and, represented by-a in the command, indicates that the search condition is met only when all conditions are met. For example, in the "/home/user" directory, find the file named 0718 as a directory. Use
Find/home/user-name 0718-a-type d
(2) or: logical or, expressed in-o in the command, indicating that the search condition is met as long as one of the given items is satisfied. For example, in the "/home/user" directory, find the file named main. cc or main. c. Use
Find/home/user-name main. cc-o-name main. c.
(3) not: logic is not used in the command! Searches for files that do not meet the given conditions. For example, if you find a file named not main. c Under "/home/user", use
Find/home/user! -Name main. cc.
Common search conditions include:
(1) Search by name and file attributes.
-Name 'string': searches for all files whose names match the given strings. Wildcards *,? , [].
-Gid n: searches for all files belonging to the user group with ID n.
-Uid n: searches for all files belonging to users with ID n.
-Group 'string': searches for all files belonging to the string given by the user group name.
-User 'string': searches for all files belonging to the string given by the user name.
-Empty: searches for directories or files with a size of 0.
-Perm permission: searches for files and directories with the specified permission. The permission expression can be 711,644.
-Size n [bckw]: searches for objects of the specified file size. The character after n indicates the unit. The default value is B, which indicates the block size of 512 bytes.
-Type x: Search for objects of the x type. x is one of the following characters:
Block B Device Files
C character Device File
D directory file
P named pipe (FIFO)
F Common File
L symbolic links)
S socket File
(2) search by Time
-Amin n: searches for all files that were accessed n minutes ago. (+ Indicates n minutes ago,-or n minutes later)
-Cmin n: searches for all files whose status has been modified n minutes ago.
-Mmin n: searches for all files whose contents have been modified n minutes ago.
-Atime n: searches for all files that have been accessed n days ago.
-Ctime n: searches for all files whose status has been modified n days ago.
-Mtime n: searches for all files whose contents have been modified n days ago.
(3) executable operations.
-Exec command name {}: Execute the Linux Command for the file that meets the conditions, without asking whether the user needs to execute the command. {} Indicates that the command parameter is the file found. The end of the command must end.
For example, in the "/home/user" directory, find the file named main. c and display the detailed information of these files, use
Find/home/user-name main. c-exec ls-l {}\;
-OK command name {}: run the Linux Command for the file that meets the conditions. Unlike exec, it will ask whether the user needs to execute the command.
19. Search for the file content grep
Command: grep [Option] [search mode] [file name 1, file name 2,…]
Function: The grep filter searches for files in the specified character mode and displays all rows containing this mode. The pattern to be searched is called a regular expression.
Common Regular Expressions
^: Start with what, for example, ls-l | grep ^ d: displays the details of all subdirectories in the current directory.
$: What is the end. For example, ls-l | grep c $ displays the files ending with c in the current directory.
Common parameters:
-E each pattern is treated as an extended regular expression.
-F treat each mode as a fixed string
-C only displays the number of matched rows.
-I Comparison is case insensitive.
-N: add the row number of the matching string before the output.
20. Document Management
Command: tar [main option + auxiliary option] source file or directory of the target document
Function: You can use tar to create files and directories. Using tar, you can create a file (backup file) for a specific file, change the file in the file, or add a new file to the file. Tar was originally used to create files on tape. Now, you can create files, such as floppy disks, on any device.
Using the tar command, you can package a large number of files and directories into one file, which is very useful for backing up files or combining several files into one file for network transmission.
Common parameters:
C: Create a new archive file.
R: append the archived file to the end of the file.
X: release a file from the file.
F: Use a file or device.
V: displays the processed files during the archiving process.
Z: Use gzip to compress/uncompress the file with the suffix .gz. With this option, you can compress the file.
For example, use tar cvf source.tar/home/user/0718/*. c to archive all. c files to source.tar.
If compression is required during the archiving process, use
Tar czvf source.tar.gz/home/user/0718/*. c.
If you want to release the archive file source.tar, use
Tar xvf source.tar
If the archive compressed file is released, use
Tar xzvf source.tar.gz.
21. File compression and decompression
Command: gzip/bzip2 [Option] compressed or decompressed file name
Function: gzipused to compress the file into a file suffixed with .gz, and decompress the file suffixed with .gz. Bzip2is used to compress the file into a file suffixed with .bz2, and decompress the compressed file suffixed with .bz2.
Common parameters:
-D: Decompress the compressed file.
-V: displays the files to be decompressed or compressed during compression or decompression.
For example, to compress main. c, use
Gzip/bzip2-v main. c.
The main. c is compressed into main.c.gz or main.c.bz2.
If you extract the compressed file, use
Gzip-dv main.c.gz, or bzip2-dv main.c.bz2.


Common linux commands

The landlord should also want to learn how to operate the Linux system. It is recommended that you first look at the linux private house dish of laruence,
After learning some basic commands, you can find the corresponding commands according to specific requirements.
How can I find it? I also have a linux Command book. If you want to leave a mailbox, I can send it to you.

How to clear system cache using shell commands in linux

There are no ready-made commands available. You have to compile your own scripts.

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.