Linux command collation (self-used)

Source: Internet
Author: User

Linux Common Commands
1. There are two types of users under Linux: Superuser (root), normal user.
A) Super users: You can do anything under the Linux system without restrictions.
b) Ordinary users: do a limited amount of things under Linux.
The command prompt for the superuser is "#", and the normal user's command prompt is "$".
Command: su [user name]
Function: Switch user.
For example, to switch from the root user to a normal user, use the SU user.
To switch from a normal user to the root user, use Su root (root can be omitted) and the system will prompt for the root user's password.
2. Add Users.
Command: Useradd user name
Function: Add a normal user.
For example, to add a normal user user1, you can use the
Useradd user1. With this command, a directory named User1 is created under the directory "/home".
3. Set Password
Command: passwd user name
Function: Sets or modifies the password for the user name.
For example, to set a password of 123456 for the User1 user you just created, use passwd user1, and you'll be prompted to enter a 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 that directory. For files, file names and other information are listed. The Linux file system does not execute the file according to the suffix name, but is based on whether the file has executable permissions.
The common options are:
-a displays all subdirectories and files under the specified directory. For example, if you list all subdirectories and files under the "/root/home" directory, use the
Ls–a/root/home.
-L lists the details of all directories and files under the specified directory. For example, if you list all subdirectories and files under the "/root/home" directory, use the
Ls–l/root/home. The detailed information listed in each row is:
File type with permissions connection number file owner file belongs to group file size last modified time file name.
The information displayed using the LS–L command begins with a 10-letter string with the first character representing the file type, which can be one of the following types:
- : Normal file
D : Directory
L : Symbolic Links
B : Block device files
C : Character device files
P : Named Pipes
S : Socket file
The following 9 characters represent the access rights of the file, divided into 3 groups and 3 bits per group.
The first group represents the permissions of the creator of the file, the second group represents the permissions of the same group of users, and the third group represents the permissions of the other users. The three characters of each group represent the read, write, and execute permissions on the file, respectively.
The permissions are as follows: R (Read), W (write), X (Execute), _ (No permissions set). Each group can be represented by a number, such as R_x:5, Rw_:6
R__:4, then the three groups can be represented by 3 numbers, such as rwxr_xr_x:755, rw_r__r__:644.
LS output content is color, such as: The directory is blue, compressed file is a red display, if there is no color, you can add parameter--color=never means that the output is not color, and--color=auto is automatic,--color=always means always have color.
If you need more detailed parameter descriptions, you can get the help of LS in the following ways: 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 the
cd/home/user/0718.
Common Switch Directory commands:
Cd.. To the parent directory
CD/To root directory
CD ~ To the user's home directory ~ Same as directly performing CD effect
cd– to the last catalog
6. Displays the current working directory.
Command : pwd
Function : Displays the directory where the user is currently located. For example, when we use the command
cd/home/user/0718, then use the command pwd to display the command line
/home/user/0718.
7. Create a Directory
Command: mkdir [options] 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 will create a 07181 directory in the current directory, which can be viewed using ls–l.
8. Delete Directory
Command: rmdir [options] DirName
Function: In the current working directory, delete directory named "dirname" subdirectory. The subdirectory must be an empty directory at this time. We have just created an empty directory 07181, if we want to delete it, then use RmDir 07181. At this point again use Ls–l enumeration, then found that 07181 has been 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 destination file or directory
Function: Copy the specified source file to the target file or copy multiple source files to the target directory.
Common parameters:
-F If a file with the same name as the source file exists in the destination directory, it is overwritten directly without prompting. For example, copy the Main.c file under the current directory to "/home/user/0718" and overwrite it if there is a name, using:
Cp–f./main.c/home/user/0718.
If the source file is renamed at the same time as the copy, such as copying the main.c file under the current directory to the "/home/user/0718" directory and named Main1.c, use:
Cp–f./main.c/home/user/0718/main1.c.
-I. In contrast to-F, when a file exists in the destination file with the same name as the source file, the system prompts you to overwrite it when you copy it. In the example above, if in the copy process, the target file has the same name as the source file, you need to prompt whether to overwrite, you only need to change the above example of-F to –I. For example
Cp–i./main.c/home/user/0718.
Cp–i./main.c/home/user/0718/main1.c.
-R If you are copying a directory, subdirectories and files under that directory will be copied at the same time. The destination file must be a directory at this time. For example, if you copy all files and directories under the "/home/user/0718" directory to the "/home/user1" directory, use the
Cp–r/home/user/0718/home/user1.
10. Move a file or directory.
Command: MV [option] source file or directory destination file or directory
Function: Depending on the second parameter type in the MV command (target file or target directory), the MV command renames the file or moves it to a new directory. When the second parameter type is a file, the MV command completes the file rename, at which point the source file can only have one (or the source directory name), which renames the given source file or directory to the specified destination file name. When the second parameter is a directory name that already exists, the source file or directory parameter can have more than one, and the MV command moves the source files specified by each parameter to the destination directory. When you move a file across a file system, the MV copies the original file, and the link to the file is lost.
Parameters:
-I if there is a duplicate name in the process of the move, you are prompted to overwrite it.
-F If there is a duplicate name in the process of the move, it will be overwritten directly without prompting.
For example, to rename the Main.c file under "/home/user/0718" to main.cpp, use the
Mv/home/user/0718/main.c/home/user/0718/main.cpp.
To move all content under "/home/user/0718" to "/home/user/0719", use the
mv–f/home/user/0718/home/user/0719.
11. Delete Files or directories
Command: RM [options] File or directory
Features: It's easy to create files in Linux, and files are obsolete and useless at any time in the system. The user can delete it using the RM command. The function of this command is to delete one or more files or directories in a directory, and it can delete all files and subdirectories under a directory. For linked files, only the links are deleted and the original files remain unchanged. If you do not delete the-r option, the directory will not be killed.
Parameters:
-F does not give a hint during the delete process.
An interactive prompt is given during the-I removal process.
-R If you delete a directory, the directories and subdirectories under that directory are deleted.
For example, to remove main.cc Main.exe in the "/home/user/0718" directory
is used,
Rm-f/home/user/0718/main.cc/home/user/0718/main.exe
If you want to delete the "/home/user/0718" directory, use the
rm–rf/home/user/0718.
12. View File Contents
Command: Cat [Options] [file]
Function: View the contents of the target file
Parameters:
-B number of non-null output lines
-e displays $ at the end of each line
-N to the output of all line numbering
-S does not output multiple lines of blank lines.
For example, to view the contents of the main.cc in the current directory
Then use the cat main.cc.
Standard input and output and redirection:
A file descriptor is an integer that represents an open file, a standard three descriptor:
Standard input: General refers to the keyboard, the descriptor is: 0
Standard output: Generally refers to the screen output, the descriptor is: 1
Error output: Also screen, descriptor: 2
REDIRECT symbol:
< redirect input, > redirect output, >> add output, 2> error redirection, &> error and information redirection
Cat is often used with redirects. where > represents the creation of,>> to indicate the end of an append,<<
If there are no parameters in the cat's command line, it reads the data from the standard input and sends it to the standard output.
Four ways to create empty files in Linux:
Way 1:echo > A.txt (as if there would be a byte)
Way 2:touch B.txt
Mode 3:cat > C.txt Press CTRL + C key combination to exit; ctrl+d
Mode 4:vi D.txt enters: Wq exits.
13. Display the first few lines of a file's contents
Command: Head–n row numeric file name
Features: Displays the first few lines of the destination file.
For example, to display the first 10 rows of main.cc in the current directory, use the
Head–n main.cc.
14. Display the following lines of a file
Command: Tail–n The value file name of the row.
Function: Displays the last few lines of the destination file.
For example, to display the last 10 lines of the main.cc file in the "/home/user/0718/" directory. The Tail–n 10/home/user/0718/main.cc is used.
15. Pipeline and command substitution
Pipe: Is a redirect, like a conduit, that outputs the output of one program or command as input to another program or command. Eg: #ls –l/etc | Wc–w
Command substitution: A bit similar to redirection, but the difference is that command substitution is the output of one command as a parameter to another command. Common formats are: Command1 ' Command2 ' or Command1 $ (command2)
Example:
Start by listing all current information and redirecting to the AA file:
#ls | Cat > AA or ls > AA
Then, by command substitution, list all the file information in the AA file
#ls –l ' cat aa ' or with ls–l $ (cat aa)


16. Create a mask for a file or directory
Umask refers to the file (0666) or directory (0777) when created in all permissions to remove some of the permissions, ordinary users default Umask value is 002, the Superuser 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 other group permission write attributes are removed
After creating a file, the default permissions for normal users are 664 super users: 644
After creating a directory, the default permissions for normal users are 775 super users: 755
You can view the default mask value by Umask. The Mask value is modified by Umask 001.
17. Change permissions for a directory or file
function: The chmod command is very important for changing the access rights of a file or directory. Users use it to control access to files or directories.
Syntax: This command has two uses. One is a text-setting method that contains letters and operator expressions, and the other is a digital setting method that contains numbers.
Description: We use the ls–l long format to list the basic information of a file or directory as follows:
File type and Permissions link Number file Owner file group file size last modified time name
For permissions, the first group represents the permissions of the file owner, the second group represents the permissions of the same group of users, and the third group represents the permissions of the other users. Each group of three characters represents the read, write, and execute permissions on the file, respectively. Permissions can be modified through chmod.
1. Text Setting method
chmod [Who][+|-|=][mode] File name Modify who's permissions in the specified file name increase/remove/assign to mode
Parameters:
Action object who is either or a combination of the following letters:
U means "user", which is the owner of the file or directory.
G means "same group user", that is, all users who have the same group ID as the file owner.
O means "other (others) users".
A means "all users". It is the system default value. That is, chmod +x 1.c means that everyone has executable permissions.
The operation symbols can be:
+ Add a permission.
-Cancels a permission.
= gives the given permission and cancels all other permissions, if any.
The permissions represented by mode can be set by any combination of the following letters (who cannot be less when combined):
R readable
W Writable
X executable
File name: A list of files separated by spaces to change permissions, and wildcard characters are supported.
Multiple permission methods can be given in one command line, separated by commas. For example: chmod g+r,o+r example enables the same group and other users to have read access to the file example.
2. Digital Setting method
We must first understand the meaning of the attributes represented by numbers: 0 means no permissions, 1 means executable permissions, 2 is writable, 4 is read, and then it is added. So the format of the numeric attribute should be 3 octal numbers from 0 to 7, in the Order of (U) (g) (O).
For example, if you want a file owner to have "read/write" Two permissions, you need to put 4 (readable) +2 (writable) =6 (read/write).
The general form of the digital setting method is: chmod [mode] file name
  Example:
(1) Text setting method:
Example 1:$ chmod a+x sort
The properties of the set file sort are:
File owner (u) Increase execution permissions
Increase execution permissions with the file owner in the same group of users (g)
Additional users (O) Increased execution permissions
Example 2:$ chmod ug+w,o-x text
The property of the set file text is:
File owner (u) increases Write permissions
Add write permissions to the same group of users as the file owner (g)
Other users (O) Remove Execute permissions
Example 3:$ chmod a–x mm.txt
$ chmod–x Mm.txt
$ chmod ugo–x Mm.txt
All of the above three commands delete the execution permission of the file Mm.txt, and it sets the object to be all the users.
(2) Digital setting Method:
Example 1: $ chmod 644 Mm.txt
$ ls–l
The properties of the set file Mm.txt are:
-rw-r--r--1 Inin users 1155 Nov 5 11:22 Mm.txt
File owner (U) inin has read and write permissions
Read access to the same group user as the file owner (g)
Other people (O) have read access
Example 2: $ chmod wch.txt
$ ls–l
-rwxr-x---1 inin users 44137 Nov 9:22 wchtxt
That is, set wchtxt the properties of this file are:
File owner (U) inin readable/writable/executable rights
(g) readable/enforceable rights with the main group of files
Other people (O) do not have any permissions
18. File Lookup
Command: Find start directory lookup condition action
Function: Searches for a price in the specified directory structure and performs the specified action.
The search criteria for this command can be a compound condition consisting of a logical operator not, an and, or.
(1) and: logical AND, in the command with-a, indicating that only when the given conditions are satisfied, the search condition is satisfied. For example, in the "/home/user" directory, look for a file named 0718 type is a directory. Then use
Find/home/user–name 0718–a-type D
(2) or: Logical OR, in the command with-O, indicating that as long as the given bar has a satisfaction, the search condition is satisfied. For example, under the "/home/user" directory, look for a file with the name main.cc or the name MAIN.C. Then use
Find/home/user-name main.cc-o-name main.c.
(3) Not: Logic is not, use in the command! Represents a file that finds a condition that does not meet the given criteria. For example, under "/home/user" to find a file whose name is not main.c, use the
Find/home/user! –name main.cc.
Common search criteria are:
(1) Find by name and file attributes.
-name ' string ' Find file name matches all files of the given string, wildcard characters * 、?、 [] in the string.
-gid N finds all files belonging to the user group with ID number n.
-uid N finds all files belonging to a user with ID number n.
-group ' string ' finds all the files that belong to the user group named the given string.
The-user ' string ' finds all the files that belong to the user name of the given string.
-empty look for a directory or file of size 0.
-perm permissions find files and directories with the specified permissions, which can be represented as 711,644.
-size n[bckw] Finds files of the specified file size, the characters following n represent units, the default is B, which represents 512-byte blocks.
-type x finds a file of type X, and X is one of the following characters:
B-Block device files
C-Character device files
D catalog File
P Named Pipes (FIFO)
F Common Files
L Symbolic Link file (Symbolic links)
s socket file
(2) Search by Time
-Amin N finds all files that were accessed N minutes ago. (+ = n minutes ago,-or no write means n minutes)
-Cmin n finds all files that have been modified for n minutes before the file status.
-Mmin n finds all files where the contents of the file have been modified before n minutes.
-Atime n finds all files that were accessed N days ago.
-CTime n finds all files that have been modified by the file status N days ago.
-Mtime n finds all files where the contents of the file have been modified before n days.
(3) an executable operation.
-EXEC command name {}: Executes the given Linux command on a qualifying file without asking the user if the command needs to be executed. {} indicates that the argument to the command is the found file; the end of the command must be "\;" End.
For example, in the "/home/user" directory, look for details called MAIN.C files and display these files, use the
Find/home/user–name main.c-exec ls–l {} \;
-OK command name {}: Executing the given Linux command on a qualifying file, unlike Exec, it asks the user if the command needs to be executed.
19. Search file Contents grep
Command: grep [option] [Find mode] [file name 1, file name 2, ...]
Function: The grep filter looks for a file with the specified character pattern and displays all rows that contain this pattern. The pattern being looked up is called a regular expression.
Some regular expressions that are commonly used
^: Starting with what, for example Ls–l | grep ^d Displays details of all subdirectories under the current directory.
$ : At what end.  such as Ls–l | grep C $ Displays the file that ends in the current directory with the end.
Common parameters:
-E Each pattern is treated as an extended regular expression.
-F each pattern treated as a fixed string
-C displays only the number of matching rows.
-I comparisons are case insensitive.
-n precede the output with the line number where the matching string is located.
20. Document Management
Command: tar [main option + secondary option] Destination document source file or directory
Features: Tar can create archives for files and directories. With tar, users can create files (backup files) for a specific file, or they can change files in the file or add new files to the file. Tar was originally used to create files on tape, and now users can create files on any device, such as a floppy disk.
With the tar command, you can package a whole bunch of files and directories into a single file, which is useful for backing up files or combining several files into one file for easy network transmission.
Common parameters:
C: Create a new profile.
R: To append the archived file to the end of the archive file.
X: Release the file from the archive file.
F: Use an archive file or device.
V: Displays the processed files during the archiving process.
Z: Compress/Unzip files with gzip, suffix. gz, plus this option to compress the archive file.
For example, to archive all suffixes under "/home/user/0718" to. c to Source.tar, use the tar cvf source.tar/home/user/0718/*.c
If you want to compress in the process of archiving, use the
Tar czvf source.tar.gz/home/user/0718/*.c.
If you want to release the archived file Source.tar, use the
Tar XVF Source.tar
If you release the archived compressed file, use the
Tar XZVF source.tar.gz.
21. File Compression Decompression
Command: GZIP/BZIP2 [option] to compress or decompress a file name
Features: gzip is used to compress files into compressed files that are suffixed with. GZ, or to unzip files with a. gz suffix. The BZIP2 is used to compress the file into a compressed file with the suffix. bz2, or to unzip the compressed file with the suffix. bz2.
Common parameters:
-D: Unzip the compressed file.
-V: The extracted or compressed files are displayed during compression or decompression.
For example, to compress main.c, use the
Gzip/bzip2–v main.c.
The MAIN.C will be compressed into main.c.gz or main.c.bz2.
If you unzip the compressed file just now, use the
GZIP–DV main.c.gz, or BZIP2–DV main.c.bz2.

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.