linux basic commands and permissions

Table of contents

1. Shell commands and operating principles

2. Common Linux commands

1. ls command

2. pwd command

3.cd command

4. touch command

5.mkdir command (important)

6.rmdir command&& rm command (important)

7.man command (important)

8.cp command (important)

9.mv command (important)

10.cat

11.less directive (important)

12.head command

14. Time-related instructions

15.Cal instructions

16.fifind command: (very important) -name

17. grep command

18.zip/unzip command: ​​​​​​​

19.tar command (important)

20.uname -r command:

21. Several important hot keys [Tab], [ctrl]-c, [ctrl]-d

22. Shut down

3.Linux permissions

1. Classification of file visitors (people)

2. File type and access permissions (thing attributes)

3. Representation method of file permission value

4. Related setting methods for file access permissions

5.fifile command

6. Directory permissions

7. Sticky bit



1. Shell commands and operating principles

Strictly speaking, Linux is an operating system, which we call the "kernel", but we ordinary users cannot use the kernel directly. Instead, it communicates with the kernel through the kernel's "shell" program, the so-called shell. How to understand? Why can't I use the kernel directly?
From a technical perspective, the simplest definition of Shell: command interpreter (command Interpreter) mainly includes:
         Translate user commands to the kernel for processing
        At the same time, the core processing results are translated to the user.
Compared with the Windows GUI , when we operate Windows, we do not directly operate the Windows kernel, but click through the graphical interface to complete our operations (for example, to enter the D drive, we usually double-click the D drive letter or run an application) .
The shell has the same function for Linux , mainly parsing our instructions and parsing the instructions to the Linux kernel. The feedback results are run through the kernel and parsed to the user through the shell.
Help to understand: If you are a boring and shy programmer, then the shell is like a matchmaker, and the operating system kernel is the beautiful little girl in your village who makes your heart beat. You have fallen in love with Xiaohua, but you are too embarrassed to confess directly. Then let your family find a matchmaker to help you propose marriage. You should communicate everything directly with the matchmaker, and the matchmaker will convey your feelings to Xiaohua. We found the matchmaker whose surname is Wang. , so we call it Wangpo, which corresponds to the bash we often use.

2. Common Linux commands

1. ls command

Syntax : ls [options][directory or file]
Function : For a directory, this command lists all subdirectories and files in the directory. For files, the file name is listed along with other information.
Common options:
-a Lists all files in the directory, including implicit files starting with .
-d displays the directory as a file, rather than displaying the files below it. For example: ls –d specifies the directory
-i Outputs the index information of the i node of the file. For example, ls –ai specifies the file
-k indicates the size of the file in k bytes. ls –alk specifies the file
-l List file details.
-n Use numeric UID, GID instead of name. (Introducing UID, GID)
-F Attach a character after each file name to indicate the type of the file. "*" indicates an executable ordinary file; "/" indicates a directory; "@" indicates
represents a symbolic link; "|" represents FIFOs; "=" represents sockets. (Directory type identification)
-r Sort the directory in reverse order.
-t Sort by time.
-s outputs the size of the file after the l file name. (Size sorting, how to find the largest file in a directory)
-R List files in all subdirectories. (recursion)
-1 outputs only one file per line.

2. pwd command

Syntax : pwd
Function : Display the directory where the user is currently located

 Displays that the kiana user is currently under the "/home" path.

3.cd command

In the Linux system, the files and directories on the disk are organized into a directory tree, and each node is a directory or file.

Syntax : cd directory name
Function : Change the working directory. Change the current working directory to the specified directory.
Example:
cd .. : Return to the upper directory
cd /home/litao/linux/ : absolute path
cd ../day02/ : relative path
cd ~: Enter the user's home directory
cd -: Return to the recently visited directory

4. touch command

Syntax : touch [options]... file...
Function : The touch command parameters can change the date and time of a document or directory, including access time and change time, or create a new file that does not exist.
Common options :
-a or --time=atime or --time=access or --time=use only changes the access time.
-c or --no-create Do not create any documentation.
-d Use the specified date and time instead of the current time.
-f This parameter will be ignored and will not be processed. It is only responsible for solving the compatibility problem of the BSD version of the touch command.
-m or --time=mtime or --time=modify only change the change time.
-r sets the date and time of the specified document or directory to be the same as the date and time of the reference document or directory.
-t Use the specified date and time instead of the current time

5.mkdir command (important)

Syntax : mkdir [options] dirname...
Function : Create a directory named "dirname" in the current directory
Common options :
-p, --parents can be a path name. At this time, if some directories in the path do not yet exist, after adding this option, the system will automatically create those directories that do not yet exist, that is, multiple directories can be created at one time;
Example :
mkdir –p test/test1: Create multiple directories recursively

6.rmdir command&& rm command (important)

rmdir is a command corresponding to mkdir. mkdir is to create a directory, and rmdir is a delete command.
Syntax : rmdir [-p][dirName]
Applicable objects : All users with permission to operate the current directory
Function : Delete empty directories
Common options :
-p When the subdirectory is deleted, if the parent directory also becomes an empty directory, the parent directory will be deleted together.
The rm command can delete files or directories at the same time
Syntax : rm [-firv][dirName/dir]
Applicable to : All users
Function : Delete files or directories
Common options :
-f Even if the file attribute is read-only (i.e. write-protected), delete it directly
-i Ask for confirmation one by one before deleting
-r deletes the directory and all files under it

7.man command (important)

Linux commands have many parameters, and it is impossible for us to remember them all. We can get help by checking the online manual. The command to access the Linux man page is man
Syntax : man [options] command
Common options :
-k Search online help based on keywords
num is only found in chapter num
-a displays all chapters, such as man printf. By default, it starts searching from the first chapter and stops when it is known. Use the a option. When pressing q to exit, it will continue to search until all chapters are searched. .
Explain, the online manual is divided into 8 chapters
1 is an ordinary command;
2 is a system call, such as open, write and so on (through this, at least you can easily find out what header file needs to be added to call this function);
3 is library function, such as printf, fread;
4 is special files, which are various device files under /dev;
5 refers to the format of the file, such as passwd, which will explain the meaning of each field in the file;
6 is reserved for games and is defined by each game;
7 is the attachment and there are some variables, such as global variables such as environ, which are explained here;
8 is a command used for system management. These commands can only be used by root, such as ifconfig;

8.cp command (important)

Syntax : cp [options] source file or directory target file or directory
Function : Copy files or directories
Description : The cp command is used to copy files or directories. If two or more files or directories are specified at the same time, and the final destination is an existing directory, it will copy all the previously specified files or directories to this directory. . If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear.

9.mv command (important)

The mv command is the abbreviation of move, which can be used to move files or rename files (move (rename) fifiles). It is a commonly used command under Linux systems and is often used to back up files or directories.
Syntax : mv [options] source file or directory target file or directory
Function :
1. Depending on the type of the second parameter in the mv command (whether it is a target file or a target directory), the mv command renames the file or moves it to a new directory.
2. When the second parameter type is a file, the mv command completes the file renaming. At this time, there can only be one source file (it can also be the source directory name). It will rename the given source file or directory to the given one. specified target file name.
3. When the second parameter is the name of an existing directory, there can be multiple source files or directory parameters, and the mv command will move all the source files specified by each parameter to the target directory.
Common options :
-f: force means force. If the target file already exists, it will be overwritten directly without asking.
-i: If the destination file (destination) already exists, you will be asked whether to overwrite it!
Example :
将rm改造成mv
vim ~/.bashrc #修改这个文件
mkdir -p ~/.trash
alias rm=trash
alias ur=undelfile
undelfile() {
 mv -i ~/.trash/\$@ ./
}
trash() {
 mv $@ ~/.trash/
}

10.cat

Syntax : cat [options][file]
Function : View the contents of the target file
Common options :
-b output line numbers for non-empty lines
-n numbers all lines of output
-s does not output multiple blank lines
10.more instruction
Syntax : more [options][file]
Function : more command, similar to cat in function
Common options :
-n numbers all lines of output
q exitmore

11.less directive (important)

The less tool is also a tool for paging display of files or other output. It should be said that it is an orthodox tool for viewing file contents in Linux and is extremely powerful.
The usage of less is more flexible than that of more. When it comes to more, we have no way to turn forward, we can only look back.
But if you use less, you can use the [pageup][pagedown] and other key functions to browse files forward and backward, which is easier to view the contents of a file!
In addition, you can have more search functions in less, not only you can search down, but you can also search up.
Syntax: less [parameter] file
Function :
Less is similar to more, but with less you can browse the file at will, while more can only move forward, not backward, and less does not load the entire file before viewing.
Options :
-i ignore case when searching
-N displays the line number of each line
/string: function to drill down for "string"
?String: Function to search upward for "string"
n: Repeat the previous search (related to / or ?)
N: Repeat the previous search in reverse (related to / or ?)
q:quit

12.head command

Head and tail are as easy to understand as their names. They are used to display a certain number of text blocks at the beginning or end. Head is used to display the beginning of the file to the standard output, while tail is used to read the file. end.
Syntax: head [parameter]... [file]... 
Function :
head is used to display the beginning of the file to the standard output. The default head command prints the first 10 lines of its corresponding file. 
Options :
-n<number of lines> Number of lines to display
13.tail command
The tail command writes the file to the standard output starting from the specified point. Use the -f option of the tail command to conveniently check the changing log file. tail - f fifilename will display the last content of fifilename on the screen, and not only refresh , allowing you to see the latest file content.
Syntax: tail[required parameters][selected parameters][file] 
Function: Used to display the content at the end of the specified file. When no file is specified, it will be processed as input information. Commonly used to view log files.
Options:
-f loop reading
-n<number of lines> displays the number of lines
Example
There is a file with a total of 100 lines of content. Please take out the 50th line of content<br>
seq 1 100 > test # Generate a sequence from 1 to 100 and load it into test
Method 1 head -n50 test > tmp #Load the first 50 lines into the temporary file tmp
tail -n1 tmp #Get China Construction Bank
Method 2 head -n50 test | tail -n1

14. Time-related instructions

date display

Date specifies the format to display time: date +%Y:%m:%d

date Usage: date [OPTION]... [+FORMAT]
1. In terms of display, the user can set the format to be displayed. The format is set to a plus sign followed by several marks. The list of commonly used marks is as follows:
%H : hours (00..23)
%M : Minutes (00..59)
%S : seconds (00..61)
%X: equivalent to %H:%M:%S
%d : day(01..31)
%m: month (01..12)
%Y: Complete year (0000..9999)
%F: equivalent to %Y-%m-%d
2. In terms of setting time
date -s //Set the current time. Only root permissions can set it, others can only view it.
date -s 20080523 //Set to 20080523, which will set the specific time to empty 00:00:00
date -s 01:01:01 //Set the specific time and the date will not be changed.
date -s “01:01:01 2008-05-23″ //This can set the entire time
date -s “01:01:01 20080523″ //This way you can set the entire time
date -s “2008-05-23 01:01:01″ //This way you can set the entire time
date -s “20080523 01:01:01″ //This way you can set the entire time
3. Timestamp
Time->Timestamp: date +%s
Timestamp->Time: date -d@1508749502
The Unix timestamp (Unix epoch, Unix time, POSIX time or Unix timestamp in English) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), regardless of leap seconds.

15.Cal instructions

The cal command can be used to display the Gregorian (Solar) calendar. The Gregorian calendar is the calendar currently used internationally, also known as the Gregorian calendar and commonly known as the Gregorian calendar. The "Gregorian calendar", also known as the "solar calendar", is based on the earth's orbit around the sun as one year. It is common in Western countries, so it is also called the "Western calendar".
Command format : cal [parameter][month][year]
Function : Used to view calendar and other time information. If there is only one parameter, it represents the year (1-9999). If there are two parameters, it represents the month and year.
Common options :
-3 Display the system’s calendar of the previous month, current month, and next month
-j displays the day of the year (the date of the year is calculated in days, starting from January 1st, and the number of days of the current month in the year is displayed by default)
-y displays the calendar for the current year

16.fifind command: (very important) -name

The fifind command under Linux searches for files in the directory structure and performs the specified operation.
The fifind command under Linux provides quite a few search conditions and is very powerful. Because fifind has powerful features, so does its options
There are many, and most of the options are worth our time to look at.
Even if the system contains a Network File System (NFS), the fifind command is also valid in the file system, as long as you have the corresponding permissions.
When running a very resource-consuming fifind command, many people tend to execute it in the background because it traverses a large file system.
The system may take a long time (here refers to file systems above 30G bytes).
Syntax: fifind pathname -options
Function: Used to search for files in the file tree and process them accordingly (possibly accessing the disk)

17. grep command

Syntax: grep [options] Search string file
Function: Search for a string in the file and print out the found lines
Common options:
-i: Ignore the difference in case, so the cases are treated as the same
-n: Output the line number by the way
-v: reverse selection, that is, display the line without the 'search string' content

18.zip/unzip command: ​​​​​​​

Syntax: zip compressed file.zip directory or file
Function: Compress directories or files into zip format
Common options:
-r recursively processes all files and subdirectories in the specified directory.
Example :
Compress the test2 directory: zip test2.zip test2/*
Unzip to the tmp directory: unzip test2.zip -d /tmp

19.tar command (important)

Pack/unpack, do not open it, look at the content directly; tar [-cxtzjvf] files and directories....
Parameters :
-c: Parameter command to create a compressed file (meaning create);
-x: Parameter command to unpack a compressed file!
-t: View the files in tarfifile!
-z: Does it also have the attributes of gzip? That is, do I need to use gzip compression?
-j: Does it also have the attributes of bzip2? That is, do I need to use bzip2 compression?
-v: Display files during compression! This is commonly used, but it is not recommended to be used in background execution processes!
-f: Use the file name. Please note that the file name must be followed immediately after f! Don't add any more parameters!
-C: Extract to the specified directory

example:

Example 1: Pack all the files in the entire /etc directory into `/tmp/etc.tar`
[root@linux ~]# tar -cvf /tmp/etc.tar /etc<== only packaging, not compression!
[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <==After packaging, compress with gzip
[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <==After packaging, compress it with bzip2
Pay special attention to the fact that the file name after parameter f is chosen by yourself. We are accustomed to use .tar as identification.
If the z parameter is added, .tar.gz or .tgz will represent the gzip-compressed tar file ~
If you add the j parameter, .tar.bz2 will be used as the file extension~
When the above command is executed, a warning message will be displayed:
『`tar: Removing leading `/" from member names`』 That is a special setting about absolute paths.
Example 2: Check what files are in the above /tmp/etc.tar.gz file?
[root@linux ~]# tar -ztvf /tmp/etc.tar.gz
Since we use gzip compression, when we want to check the files in the tar file, we have to add the z parameter! This is very important!
Example 3: Extract the /tmp/etc.tar.gz file under /usr/local/src
[root@linux ~]# cd /usr/local/src
[root@linux src]# tar -zxvf /tmp/etc.tar.gz
By default, we can unzip the compressed file anywhere! For this example,
I first change the working directory to /usr/local/src and unzip /tmp/etc.tar.gz,
The unlocked directory will be in /usr/local/src/etc! Also, if you go into /usr/local/src/etc
You will find that the file attributes in this directory may be different from /etc/!
Example 4: Under /tmp, I just want to unlock etc/passwd in /tmp/etc.tar.gz [root@linux ~]# cd /tmp
[root@linux tmp]# tar -zxvf /tmp/etc.tar.gz etc/passwd
I can use tar -ztvf to check the file names in the tarfile. If I only want one file,
You can submit it through this method! Notice! The root directory / in etc.tar.gz has been removed!
Example 5: Back up all files in /etc/ and save their permissions!
[root@linux ~]# tar -zxvpf /tmp/etc.tar.gz /etc
This -p attribute is very important, especially when you want to retain the attributes of the original file!
Example 6: In /home, only files newer than 2005/06/01 are backed up.
[root@linux ~]# tar -N "2005/06/01" -zcvf home.tar.gz /home
Example 7: I want to back up /home, /etc, but not /home/dmtsai
[root@linux ~]# tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc
Example 8: Pack /etc/ and unpack it directly under /tmp without generating a file!
[root@linux ~]# cd /tmp
[root@linux tmp]# tar -cvf - /etc | tar -xvf -
This action is a bit like cp -r /etc /tmp ~ it still has its purpose!
What should be noted is that the output file becomes - and the input file also becomes -, and there is another |
These represent standard output, standard input and pipeline commands respectively!

20.uname -r command:

Syntax : uname [options] 
Function : uname is used to obtain information about the computer and operating system.
Additional explanation : uname can display basic information such as the version of the operating system used by the Linux host and the name of the hardware.
Common options:
-a or –all output all information in detail, in order: kernel name, host name, kernel version number, kernel version, hardware name, processor type, hardware platform type, operating system name

21. Several important hot keys [Tab], [ctrl]-c, [ctrl]-d

[Tab] button---has the functions of "command completion" and "file completion"
[Ctrl]-c key---"stop" the current program
[Ctrl]-d key---usually represents: "End Of File, EOF or End OfInput"; in addition, it can also be used to replace exit

22. Shut down


Syntax :
shutdown [options] ** Common options: **
-h: After stopping the system service, shut down immediately.
-r: Restart the system service after stopping it
-t sec: Add the number of seconds after -t, which means "shut down after a few seconds"​​​​​​​
The following commands are available as extensions:
◆ Installation and login commands : login, shutdown, halt, reboot, install, mount, umount, chsh, exit, last;
◆ File processing commands: fifile, mkdir, grep, dd, fifind, mv, ls, diffff, cat, ln;
◆ System management related commands : df, top, free, quota, at, lp, adduser, groupadd, kill, crontab;
◆ Network operation commands : ifconfig, ip, ping, netstat, telnet, ftp, route, rlogin, rcp, fifinger, mail, nslookup;
◆ System security related commands: passwd, su, umask, chgrp, chmod, chown, chattr, sudo ps, who;
◆ Other commands: tar, unzip, gunzip, unarj, mtools, man, unendcode, uudecode.

3.Linux permissions

There are two types of users under Linux: super user (root) and ordinary user.
Super user: can do anything under the Linux system without restrictions
Ordinary users: Do limited things under Linux.
The command prompt of the super user is "#", and the command prompt of the ordinary user is "$".
Command : su [username]
Function : Switch users.
For example, to switch from the root user to the ordinary user, use su user. To switch from the ordinary user user to the root user, use su root (root can be omitted). At this time, the system will prompt you to enter the password of the root user.

1. Classification of file visitors (people)

Owner of files and file directories: u---User (Chinese civilian legal issues)
The user of the group where the owner of the file and file directory belongs: g---Group (not much to say)
Other users: o---Others (foreigners)

2. File type and access permissions (thing attributes)

​​​​​​​

 

a) File type
d: folder
-: Ordinary file
l: Soft link (similar to Windows shortcut)
b: Block device file (such as hard disk, optical drive, etc.)
p: pipe file
c: character device file (such as screen and other serial devices)
s: socket file
b) Basic permissions
i. Read (r/4): For files, Read has the permission to read the file content; for directories, it has the permission to browse the directory information.
ii. Write (w/2): For files, Write has the permission to modify the file content; for directories, it has the permission to delete files in the moved directory.
iii. Execute (x/1): For files, execute has the permission to execute the file; for directories, it has the permission to enter the directory.
iv. “—” means that you do not have the permission

3. Representation method of file permission value

(1) Character representation method

(2) Octal numerical representation method

​​​​​​​

4. Related setting methods for file access permissions

a)chmod
Function: Set file access permissions
Format: chmod [parameter] permission file name
Commonly used options :
R -> Recursively modify the permissions of directory files
Note: Only the owner of the file and root can change the permissions of the file.
chmod
① User identifier +/-= permission character
+: Add the permission represented by the permission code to the permission scope
-: Cancel the permission represented by the permission code from the permission range
=: Grant the permission represented by the permission code to the permission range
User symbols:  
u: owner
g: For use by the same group as the owner
o: other users
a: All users​​​​​​​
Example:
# chmod u+w /home/abc.txt
# chmod ox /home/abc.txt
②Three octal numbers
Example:
# chmod 664 /home/abc.txt
# chmod 640 /home/abc.txt
b)chown
Function : Modify the owner of the file
Format : chown [parameter] username file name
Example:
# chown user1 f1
# chown -R user1 filegroup1
c)chgrp
Function : Modify the group to which a file or directory belongs
Format : chgrp [parameter] user group name file name
Commonly used options : -R recursively modify the group to which a file or directory belongs
Example:
# chgrp users /abc/f2
d)umask
Function :
View or modify file masks
Default permissions for new folders=666
The default permission of the new directory is 777
But in fact, the permissions you see on the files and directories you create are often not the above values. The reason is that when creating files or directories, they are also affected by umask. Assuming that the default permission is mask, the actually created file permissions are: mask & ~umask
Format : umask permission value
Description : Subtract the permission mask from the existing access permissions to generate the default permissions when creating a file. The default mask value for super users is 0022, and for ordinary users, it is 0002.

5.fifile command

Function description : Identify file type.
Syntax : fifile [options] file or directory... 
Common options :
-c displays the instruction execution process in detail to facilitate troubleshooting or analysis of program execution.
-z Attempt to decipher the contents of the compressed file.
Use sudo to assign rights
(1) Modify the /etc/sudoers file allocation file
  # chmod 740 /etc/sudoers
        # vi /etc/sudoer
Format: The host where the user who accepted the permission logged in = (the user who executed the command) command
(2) Use sudo to call authorized commands
       $ sudo –u username command

6. Directory permissions

Executable permissions: If the directory does not have executable permissions, you cannot cd into the directory.
Readable permissions: If the directory does not have readable permissions, you cannot use commands such as ls to view the contents of the files in the directory.
Writable permissions: If a directory does not have writable permissions, files cannot be created in the directory and files cannot be deleted in the directory.
So, here comes the question~~
In other words, as long as the user has write permissions for the directory, the user can delete the files in the directory, regardless of whether the user has write permissions for the file. This does not seem very scientific. A file created by Zhang San , why can you, Li Si, delete it? Let’s use the following process to verify it.
[root@localhost ~]# chmod 0777 /home/
[root@localhost ~]# ls /home/ -ld
drwxrwxrwx. 3 root root 4096 9月 19 15:58 /home/
[root@localhost ~]# touch /home/root.c
[root@localhost ~]# ls -l /home/
Total usage 4
-rw-r--r--. 1 root root 0 Sep 19 15:58 abc.c
drwxr-xr-x. 27 litao litao 4096 September 19 15:53 ​​litao
-rw-r--r--. 1 root root 0 9月 19 15:59 root.c
[root@localhost ~]# su - litao
[litao@localhost ~]$ rm /home/root.c #litao can delete files created by root
rm: Delete the write-protected ordinary empty file "/home/root.c"? y
[litao@localhost ~]$ exit
logout
In order to solve this unscientific problem, Linux introduced the concept of sticky bits .

7. Sticky bit

When a directory is set to the "sticky bit" (using chmod +t), the files in the directory can only be accessed by

[root@localhost ~]# chmod +t /home/ # Add sticky bit
[root@localhost ~]# ls -ld /home/
drwxrwxrwt. 3 root root 4096 9月 19 4:00 PM /home/
[root@localhost ~]# su - litao
[litao@localhost ~]$ rm /home/abc.c #litao cannot delete other people’s files
rm: Delete the write-protected ordinary empty file "/home/abc.c"? y
rm: cannot delete "/home/abc.c": Operation not allowed
1. Delete by super administrator
2. Delete the owner of the directory
3. Delete the owner of the file
Summary about permissions
The executable permissions of a directory indicate whether you can execute commands in the directory.
If the directory does not have -x permissions, you cannot execute any commands on the directory, or even cd into the directory, even if the directory still has -r read permissions (it is easy to make a mistake here, thinking that you can enter the directory and read the files in the directory if you have read permissions) ) And if the directory has -x permissions but does not have -r permissions, the user can execute commands and cd into the directory. However, since there is no read permission for the directory, even if you can execute the ls command in the directory, you still do not have permission to read the documents in the directory.

Guess you like

Origin blog.csdn.net/weixin_65592314/article/details/129223106