Basic shell teaching in Linux

Source: Internet
Author: User
Article title: Basic shell teaching in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
The Linux shell is like the dos command. COM. it builds a bridge between the user and the system, allowing the user to conveniently operate the system. Since Linux is a fully open platform, many people write Shell programs for it. The BASH (Baurne Again SHell) contains many features of other Shell programs and has comprehensive functions. so we will take it as an example to explain some Shell usage.
Before you start to explain some Linux usage, we will introduce some commands for your reference in the future.
1. ls command
Format: ls [option] [file list]
Option: None. the current directory file is displayed, similar to the dir/w command under DOS;
-L: displays the details of a file, including the file type, file permission, link or directory count, owner, all groups, file size, file date, and file name.
-R: list all files in the down directory from the specified directory, similar to the dir/s command in DOS.
2. who command
Format: who
Option: None. it shows which users are currently using Linux.
3. cat command
Format: cat [file list]
Purpose: display the content of a file in the file list.
4. passwd command
Format: passwd
Purpose: Set the user password.
5. pwd command
Format: pwd
Purpose: display the current path. Similar to the cd command without parameters in DOS.
6. cd command
Format: cd [path name]
Purpose: change the current path, which is the same as the cd command in DOS.
7. rm command
Format: rm [file list]
Purpose: delete a file from the file list. The same as the del command in DOS.
8. wc command
Format: wc [option] [file name]
Option: None. the number of lines, words, and characters of the file are displayed.
-W: only the number of words in the file is displayed.
Next we will introduce some usage of Linux Shell.
I. edit the command line
In Linux, when we Enter a command line but do not press Enter, we find that the input is incorrect. what should we do? Linux provides us with two special symbols @ and #.
@ Is used to abolish the entire line. For example:
$ Wwho @
Who
Root tty1 Dec 3 10:22
Cl tty2 Dec 3 11: 57
Cxp tty3 Dec 3 :54
In this example, the first character of the command is incorrect. use @ to abolish this line and re-enter the who command.
# The role of the number is to abolish the previous character. For example:
$ Ww # hho # o
Root tty1 Dec 3 10:22
Cl tty2 Dec 3 11: 57
Cxp tty3 Dec 3 :54
In this example, the first one is used to abolish the w with multiple writes, the second one is used to delete o, and the third one is used to delete the h with multiple writes, and then add o, which is the who command.
In this way, Linux operations are very troublesome. In fact, these two functions are mostly used on those old keyboards. It is much more convenient on the current keyboard. You can use the left and right keys, Del keys, and backspace keys to edit command lines, just as easily as editing a line of text in a text file, which is more convenient than DOS.
II. command completion
After you enter a part of a command, press the Tab key and Shell will try to complete the command. For example:
$ Pass <Tab>
Bash knows that this is a command and finds the command starting with pass. when it finds that only passwd meets the conditions, Bash will complete the command. If multiple commands starting with the input string are found, an alarm is reported. click the Tab key to display the commands starting with this string. For example:
$ Pas <Tab>
Passwd paste
Add characters to the original input string to avoid ambiguity. Then, use the Tab key to complete the command.
Bash can not only complete commands, but also complete the file names of command parameters, such:
$ Ls-l/home/public/c <Tab>
Clfile cxpfile
$ Ls-l/home/public/cl <Tab>
-Rw-r -- 2 cl user 21 Dec 3 12: 37 clfile
In this example, press the first Tab key. the second Tab key is used to display the name of the file starting with c in the/home/public directory, and then add the character l, press the Tab key and Bash will complete the file name, that is, clfile.
3. history list)
Bash records all commands entered in the history table for future use. A history table can hold up to 500 commands. This large capacity is enough for our use. When you log out, Bash saves the contents of the history table. in the bash_history file. the contents of bash_history are written to the history table, which is like the last logon. we can use the history command to display the records in the history table. For example:
$ History
210 who
211 passwd
212 cd public
213 cp/home/file/clfile.
214 cp/home/file/cxpfile.
215 ls-l/home/pubic
From the example, we can see that each command occupies one line and is called an event. The first part of each event is called the event number, and the last part is the command.
If we want to execute commands in the history table, we can replace the operator (!) with history (!) Add the event number. For example:
$! 210
Who
Root tty1 Dec 3 10:22
Cl tty2 Dec 3 11: 57
Cxp tty3 Dec 3 :54
In this example, we run the who command with the event number 210 in the history table.
If you want to execute the last command, enter the following:
$ !!
Who
Root tty1 Dec 3 10:22
Cl tty2 Dec 3 11: 57
Cxp tty3 Dec 3 :54
We can also use it! Used with commands to repeat commands that have been executed in the past. This advantage is that you do not need to write command parameters. For example:
$! Ls
Ls-l/home/pubic
-Rw-r -- 2 cl user 21 Dec 3 12: 37 clfile
-Rw-r -- 2 cxp user 27 Dec 3 12: 33 cxpfile
Another way is to find the same command as the specified input at any location. The specific method is to use a pair? Enclose the string to be searched, for example:
$ !? -L?
Ls-l/home/pubic
-Rw-r -- 2 cl user 21 Dec 3 12: 37 clfile
-Rw-r -- 2 cxp user 27 Dec 3 12: 33 cxpfile
In this command, Bash searches for the-l string in each event. when the ls-l/home/pubic command with the-l string is found, it is executed.
If there are no other things after the second question mark, the second question mark can be omitted. Therefore, the above example can also be written:
$ !? -L
If you want to repeat the command but make some changes, you can use s/old/new to replace the string of the old part with the string of the new part. For example:
$ !? -L? : S/public/cl
Ls-l/home/cl
Drwxrw-r -- 1 cl user 1067 Apr 19 23: 35mp
-Rw-r -- 2 cl user 21 Dec 3 13: 41 clfile
In this example, replace public in the ls-l/home/public Command with cl and then execute. Execute ls-l/home/cl.
In fact, the above commands also have a very simple implementation method, that is, press the up key, the previous command will appear after the prompt. Therefore, we can search for commands from the end of the history table by pressing the upper key. After finding the command to be executed, you can directly press Enter to execute the command, or use the command line editing method described earlier to modify the command. The downward key is the opposite of the upward key. it is used to search the historical table from the forward to the backward. You can understand their usage in practical use.
IV. pathname extension
I would like to hear about the * and? No stranger. In Linux, they are used exactly the same.
* Represents any string, for example:
$ Ls-l/home/public/c *
-Rw-r -- 2 cl user 21 Dec 3 12: 37 clfile
-Rw-r -- 2 cxp user 27 Dec 3 12: 33 cxpfile
In this example, Bash converts * extension to clfile and cxpfile.
? Represents any character, for example:
$ Cat/home/public/c? File
User name is CaoLei.
In this command, Bash will? Run the cat/home/public/clfile command.
Like DOS, Linux allows repeated use of * and? in a row ?, For example:
$ Ls c * f *
Clfile cxpfile
$ Ls c? Pf? Le
Cxpfile
In Linux, there is another path name extension method than DOS, that is, using [] to enclose matching characters, such:
$ Rm dirfle [12346789]
This command is used to delete dirfile1, dirfile2 ,... , Dirfile4, dirfile6 ,... And dirfile9 files. We can also use a simple method to express the above command:
$ Rm dirfile [1-46-9]
You can use numbers or letters:
$ Rm dirfile [abcdefghijklmnopqrstuvwxyz]
$ Rm dirfile [a-z]
These two commands share the same meaning. they all name dirfilea ,... And dirfilez.
V. input/output redirection
Linux commands generally obtain information from the standard output device. after being processed by the command program, the result is sent to the standard output device. The keyboard is a standard output device for Linux and a display is a standard input device for Linux.
Sometimes, you need to obtain information from the file or send the results to the file. Because each command does not necessarily have the corresponding parameters, Bash provides the standard output of the Command and the standard input redirection function, which can be expressed by the corresponding symbol.
1.> and>
> The Command output is sent to a file. If the file does not exist, create the file. if the file exists, overwrite the file, for example:
$ Ls-l> dirfile
$ Cat dirfile
-Rw-r -- 2 cl user 21 Dec 3 12: 37 clfile
-Rw-r -- 2 cxp user 27 Dec 3 12: 33 cxpfile
In this example, the first half of the command is to list the file list of a directory. In general, the file list is displayed on the screen. However, the second part contains the ">" sign. Bash defines the standard output as a file named dirfilr and saves the display result to this file. The second command is used to view the content of the dirfile file. its content is the same as the result of using ls-l directly.
> Functions are similar to>. The difference is that if a file exists, it does not overwrite the file, but is followed by the file added in the result. For example:
$ Ls-l> dirfile
$ Cat dirfile
-Rw-r -- 2 cl user 21 Dec 3 12: 37 clfile
-Rw-r -- 2 cxp user 27 Dec 3 12: 33 cxpfile
-Rw-r -- 2 cl user 21 Dec
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.