Shell
In computer science, Shell, commonly known as Shell (used to distinguish it from core), refers to software (command interpreter) that "provides users with an operating interface". It is similar to the command under DOS COM and later CMD exe. It receives user commands and then calls corresponding applications.
At the same time, it is a programming language. As a command language, it interactively interprets and executes commands entered by users or automatically interprets and executes a series of preset commands; As a programming language, it defines various variables and parameters, and provides many control structures only available in high-level languages, including loops and branches.
The shell in the traditional sense refers to the command-line shell. In the future, if it is not specially noted, the shell refers to the command-line shell.
The main interface between the text operating system and the outside is called shell. The shell is the outermost layer of the operating system. The shell manages your interaction with the operating system: waiting for your input, interpreting your input to the operating system, and processing various operating system outputs. In fact, shell is a command interpreter, which interprets commands entered by users and sends them to the kernel.
The shell provides a way for you to communicate with the operating system. This communication can be performed interactively (input from the keyboard and get an immediate response) or in shell script (non interactive). Shell script is a string of shell and operating system commands placed in a file, which can be reused. In essence, shell script is a simple combination of command line commands into a file.
Shell is basically a command interpreter, similar to command under DOS COM. It receives commands from users, such as ls, and then calls corresponding applications. The more common shells are the standard Bourne shell and C Shell.
Interactive shell and non interactive shell
Interactive mode is that the shell waits for the user's input and executes the commands submitted by the user. This mode is called interactive because the shell interacts with the user. This mode is also very familiar to most users: login, execute some commands and sign out. When the user checks out, the shell also terminates.
The shell can also run in another mode: non interactive mode. In this mode, the shell does not interact with the user, but reads the commands stored in the file and executes them. When it reads to the end of the file, the shell terminates.
Shell interaction tool
Command line interface
Command line interface (English: command line interface, abbreviation: CLI) is the most widely used user interface before the popularity of graphical user interface. It usually does not support the mouse. Users input instructions through the keyboard, and the computer will execute them after receiving the instructions.
The server version of linux only provides a command line interface. Users use the keyboard for input and the display for output;
terminal
The meaning of the word terminal itself is "terminal; terminal; (circuit) terminal, wire connector". In the computer field, the terminal is a machine used to let users input data to the computer and display its calculation results.
In other words, the terminal is only an input and output device used to interact with the computer, and it does not provide operation processing function.
Now, the commonly used terminal refers to the Terminal Emulator in the graphical interface. For those command-line (CLI) programs, the Terminal Emulator will "pretend" to be a traditional terminal device; For modern graphical interfaces, the terminal simulator will "pretend" to be a GUI program. The standard workflow of a terminal simulator is as follows:
1.Capture your keyboard input; 2.Send the input to the command line program (the program will think it is input from a real terminal device); 3.Get the output of the command line program( STDOUT as well as STDERR); 4.Call graphical interface (e.g X11),Render the output to the display.
Shell program
A Shell program is a file that contains several lines of Shell or linux commands. It consists of commands listed in the file. This program is edited in the editor (although it can also be written directly under the command line, online scripting), which is composed of UNIX commands and basic program structures, such as variable assignment, test conditions, and loops. We don't need to compile Shell command files. The Shell itself interprets every line in the command file, just as it is entered by the keyboard. The Shell is responsible for interpreting commands, and the user must know what these commands can do.
The basic commands that make up a Shell program
file management
ls - view file information
The full name of ls: list files. The ls command is used to display the contents under the specified working directory (list the files and subdirectories contained in the current working directory).
ls [-alrtAFR] [name...]
ls parameter:
-a : All files, together with hidden files( Start with . File of) List them together~ -A : All files, together with hidden files, but excluding . And .. These two directories are listed together~ -d : Only the directory itself is listed, not the file data in the directory -f : List the results directly without sorting (ls By default, it will be sorted by file name!) -F : According to the information such as files and directories, additional data structures are given, such as: *: Represents executable file; /: Representative directory; =: representative socket Archives; |: representative FIFO Archives; -h : File capacity in a way that is easier for humans to read(for example GB, KB wait)List; -i : list inode Location instead of listing file attributes; -l : Long data serial output, including file attributes and other data; -n : list UID And GID Not the names of users and groups (UID And GID It will be mentioned in account management!) -r : Output the sorting result in reverse, for example: the original file name is from small to large, and the reverse is from large to small; -R : List together with the contents of the subdirectory; -S : Sort by file capacity! -t : Sort by time --color=never : Do not give color display according to file characteristics; --color=always : Display color --color=auto : Let the system judge whether to give color according to the setting --full-time : In full time mode (Including year, month, day, hour and minute) output --time={atime,ctime} : output access Time or change permission attribute time (ctime) ,Not the time of content change (modification time)
alias can be used to write abbreviations of common combined commands. Currently, the default user-defined commands are:
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls -CF' alias la='ls -A' alias ll='ls -alF' alias ls='ls --color=auto'
ll='ls -alF 'is often used to view all common file information of the file;
If you need to view the information of soft link and hard link, you need to add - i;
You can see the iNode node information, foo Txt hard link foo points to 922030, the same as foo Txt points to the same node; foo. The soft link of TXT is the new iNode928887, and the internal record points to foo Txt iNode;
ln link
The full name of ln is link files;
The function of ln is to establish a synchronous link for a file in another location. The linked file can be regarded as a reference or alias of the source file.
When we need to use the same file in different directories, we don't need to put the same file in each required directory. We just need to put the file in a fixed directory, and then link it with ln command in other directories, without repeatedly occupying disk space.
ln [parameter][Source file or directory][Target file or directory]
Parameter list:
-b Delete, overwrite previously established links -d Allow super users to make hard links to directories -f Enforcement -i In interactive mode, if the file exists, the user will be prompted whether to overwrite it -n Treat symbolic links as general directories -s Soft link(Symbolic link) -v Displays the detailed processing procedure -S "-S<Suffix backup string> "or "--suffix=<Suffix backup string>" -V "-V<Backup mode>"or"--version-control=<Backup mode>" --help display help information --version display version information
In Linux system, inode number is the unique identification of the file, not the file name. The file name is only for the convenience of people's memory and application.
The first column in the "ls -li" result is the inode number of the file. The system looks for the correct file data block through the inode number.
The actual contents of the file are saved in the block (the size can be 1KB, 2KB or 4KB). The default size of an inode is 128B (in the Ext3 file system). Recording a block consumes 4B. When the inode of the file is full, the Linux system will automatically allocate a block to record the information of other block blocks like the inode. In this way, the contents of each block can be concatenated and the user can read the complete file content.
You can use stat / | grep "IO Block" to view the size of a block in the file system;
In Linux file system, links can be divided into two types: hard link and symbolic link.
Neither hard link nor soft link will copy the original file, which will only occupy a very small amount of hard disk space.
Hard link
Hard link means that a file can have multiple names. The newly created hard link file is only a reference of the source file in the current file. Note: hard link must exist in the same file system.
To create a hard link, you only need to use the ln source file link file name
be careful:
1.Whether you modify the source file or the hard link file, the data in another file will change. 2.Whether you delete the source file or delete the hard link file, as long as there is another file, the file can be accessed. 3.Hard links do not create new ones inode Information will not be changed inode Total number of. 4.Hard links cannot be established across file systems (partitions), because in different file systems, inode The number is recalculated. 5.Hard links cannot link directories, because if hard links are established for directories, not only the directory itself needs to be re established, but also all sub files under the directory, including all sub files in the sub directory, need to establish hard links, which is of great significance to the current Linux It's too complicated for me
Soft link
Soft link is to generate a special file whose content records the location pointing to another file. Unlike hard links, soft links can span different file systems.
To create a soft link, you only need to use the ln -s source file link file name
be careful:
1.Whether you modify the source file or the hard link file, the data in another file will change. 2.Soft links will create their own inode Information and block,Just in block The actual file data is not stored in, but the file name and of the source file are stored inode number. 3.Delete the soft link file, and the source file will not be affected. When the original file is deleted, the soft link file will not find the actual data, so it shows that the file does not exist. 4.Soft links can link directories. 5.Soft links can span partitions.
rm delete file
rm: remove, used to delete a file or directory.
rm [options] name...
Parameter list:
-i Ask for confirmation one by one before deleting. -f Even if the original file attribute is set to read only, it can be deleted directly without confirming one by one. -r Delete the directory and the following files one by one.
Note: to delete a folder using rm, you need to use the parameter - r;
Use rm to delete multiple files:
Delete folders using rm
If - r is not used, an error message will be prompted;
Use the - r parameter to delete the folder dir1;
cp copy
mv move / rename
chmod - modify file permissions
The full name of chmod is change mode, which is a command to control the user's permissions on files.
The file calling permission in linux is divided into three levels: file Owner, user Group and Other Users.
RWX corresponds to read write execute and 22, 21 and 20 respectively; Different file permissions are combined into different values. For example, 7 represents 111, i.e. RWE, and 3 represents 011, i.e. - WE;
chmod [-cfvR] [--help] [--version] mode file...
parameter list
-c : If the file permission has indeed been changed, its change action will be displayed -f : If the file permissions cannot be changed, do not display an error message -v : Show details of permission changes -R : Make the same permission change to all files and subdirectories in the current directory(That is, change one by one in a recursive manner) --help : Show auxiliary description --version : Display version
Where mode represents the value or mode, specifically
[ugoa...][[+-=][rwxX]...][,...]
Of which:
who | customer type | explain |
---|---|---|
u | user | Owner of the file |
g | group | Belong to the same group as the owner of the file |
o | others | Others |
a | all | all |
Operator | explain |
---|---|
+ | Add permissions |
- | Cancel permission |
= | Unique setting permission |
pattern | name | explain |
---|---|---|
r | read | Set to readable permissions |
w | write | Set to writable |
x | Execution Authority | Set to executable |
X | Special Execution Authority | Only when the file is a directory file or other types of users have executable permissions, can the file permissions be set to executable |
s | setuid/gid | When the file is executed, set the setuid or setgid permission of the file according to the user type specified by the who parameter |
t | Paste bit | Set the paste bit. Only the super user can set this bit, and only the file owner u can use this bit |
Set permissions for foo files in the folder, ugo remove the read permission and o increase the execution permission;
System settings
alias
The alias command sets the alias of the instruction.
The user can use alias to customize the alias of the instruction.
If you only enter alias, you can list all current alias settings.
Note: the effect of alias only applies to the login operation. If you want to set the alias automatically every time you log in, you can profile or Set the alias of the instruction in cshrc.
alias[alias]=[Instruction name]
Use alias to view the current list of all aliases:
Disk management
Tree view tree structure
Use whatis to view the function of tree. Tree uses tree structure to display the files and folder structure in the current folder;
tree [-aACdDfFgilnNpqstux][-I <Template style>][-P <Template style>][catalogue...]
The tree parameters are:
-a Displays all files and directories. -A use ASNI The drawing character displays a tree view instead of a ASCII Character combination. -C Add color to the list of documents and directories to distinguish various types. -d Displays the directory name instead of the contents. -D Lists when the file or directory was changed. -f Displays the full relative path name before each file or directory. -F In the execution file, directory, Socket,Symbolic connection, pipe name, each plus"*","/","=","@","|"number. -g List the group name of the file or directory. If there is no corresponding name, the group ID will be displayed. -i File or directory names are not listed in steps. -L level Limit directory display levels. -l If you encounter a directory that is a symbolic connection, directly list the original directory that the connection points to. -n Do not add color to the list of documents and directories. -N Directly list file and directory names, including control characters. -p List permission labels. -P<Template style> Displays only the file or directory names that match the template style. -q use"?"The number replaces the control character and lists the file and directory names. -s Lists the file or directory size. -t Sort with the change time of files and directories. -u List the owner name of the file or directory. If there is no corresponding name, the user ID is displayed. -x Limit the scope to the current file system. If some subdirectories under the specified directory are stored on another file system, the subdirectories will be excluded from the search scope.
Generally, the tree command needs to be installed and can be installed according to the prompt;
My installation command under Ubuntu is: sudo apt install tree;
View the contents of the current folder in the command line. The display effect is as follows:
mkdir
The full name of mkdir is make directory, which is used to create directories.
mkdir [-p] dirName -p Ensure that the directory name exists, and create one if it does not exist.
In the current directory, create an empty folder and directly use mkdir without the parameter - P. if you create a folder that does not exist in multiple layers at one time, you need to use the parameter - p;
In the current directory, create an empty folder dir, and then create an empty folder dir1 in dir;
wang@myubuntu:~/projects$ tree . └── foo.txt 0 directories, 1 file wang@myubuntu:~/projects$ mkdir -p dir/dir1 wang@myubuntu:~/projects$ tree . ├── dir │ └── dir1 └── foo.txt 2 directories, 1 file
rmdir
Used to delete empty directories.
rmdir [-p] dirName -p When the subdirectory is deleted and becomes an empty directory, it will be deleted at the same time.
Delete the empty folder and directly rmdir dir1;
If there is something in dir1 folder, it will fail to delete;
wang@myubuntu:~/projects$ rmdir dir1/ rmdir: failed to remove 'dir1/': Directory not empty
If there are only empty folders, you can use the - p parameter to delete the last empty folder first. If the next last folder is also empty, it will also be deleted;
wang@myubuntu:~/projects$ tree . ├── dir1 │ └── dir └── foo.txt 2 directories, 1 file wang@myubuntu:~/projects$ rmdir dir1/dir/ wang@myubuntu:~/projects$ tree . └── foo.txt 0 directories, 1 file
Create an empty folder dir1 under the dir folder and a text file hello txt;
Use rmdir - P/ dir/dir1;
wang@myubuntu:~/projects$ tree . ├── dir │ ├── dir1 │ └── hello.txt └── foo.txt 2 directories, 2 files wang@myubuntu:~/projects$ rmdir -p dir/dir1/ rmdir: failed to remove directory 'dir': Directory not empty wang@myubuntu:~/projects$ tree . ├── dir │ └── hello.txt └── foo.txt 1 directory, 2 files
You can see the error prompt "rmdir: failed to remove directory 'dir': Directory not empty". Only the dir1 folder is deleted;
rm -r can be used instead;
reference material
1.Shell Baidu Encyclopedia
2.ln - difference between soft link and hard link
3.The difference between linux soft connection and hard link