90-minute QuickStart Linux--linux Guide for Developments Learning notes

Source: Internet
Author: User
Tags clear screen create directory gz file stdin terminates

System Information: CentOS 64-bit.

    • A picture of command prompt and command line

    • Some practical small commands

mkdir (make directory, create directory), LS (list, list the contents of the current directory), RM (remove, delete files, if you delete the directory, you need to add the parameter-R, which means recursive-recursive delete), Man (Manual, Manual, Follow the command to open the Manual of the command, enter after typing /-parameters : Find how to use the parameters,n find the next,Q quit the user manual), ctrl+l (Clear screen),pwd (print working Dir, show work path), CTRL + A: Reach the beginning of the naming

More information can be found in books: The second and sixth chapters of the Linux command line (what is a shell, using commands)

    • Jump in File system

CD (change directory),. Represents the current directory,./subdirectories that represent the current directory 、.. Represents the parent directory of the current directory, CD/(into the root directory via an absolute path), CD-(back to the original directory)

TIPS:CD directory Name (same as CD./directory name) can be done by tab key, if there are multiple alternates, double-click tab to list all matching options

See also book: Chapter III of the Linux Command line (jump in file system)

    • Manipulating Files and directories
Copy: $ cp file1 file2 $CP-R dir1 Dir2 move:  $ mv File Dir/rename: $ mv file1 file2 $MV dir1 dir2
   # if DIR2 exists, move action Remove: RM file   $rm-R dir Create file:   $ touch a.txt  $ >a.txt View File : $ cat file to create directory:  $mkdir dir   view file type: $file a.txt

Create hidden files/directories: touch. FileName or mkdir. Mydir, as you can see through ls-a.

Use wildcards to delete all files in the current directory: RM-RF * string matching, listing all files in the/bin directory that contain the "pin" string: Ls/bin|grep Pin

Download Imooc home: wget http://imooc.com

Paging through the contents of the file: less filename (press and hold J scroll down the screen and press the K scroll up/character: For character lookup, type N to find the next double-click G to reach the file header click G to reach the end of the file Q exit), the man Command bottom page is used less, So all the shortcuts in less are available in man.

Zoom in/Zoom out/Restore the terminal font: CTRL + + CTRL +-CTRL + 0

Do not want to execute after entering the command: CTRL + C

Unzip zip file: Unzip file name compressed into zip file: Zip-r file name after compression. zip compressed file name

Unzip the *.tar.gz file: Tar zxvf file name is compressed into tar files: Tar zcvf compressed after file name. tar.gz Compressed file name

Unzip the *.tar.bz2 file: Tar jxvf file name is compressed into tar files: Tar jcvf compressed after file name. tar.gz Compressed file name

    • redirect

Output redirection (redirect stdout):>

Save the program's output to a file: date >output.txt (note: date is a system-brought program, output current time)

Cat File1 >file indicates that the contents of the file1 are copied to file in cat File1 >>file to append the contents of file1 to file. Cat file1 file2 >file indicates file1 and file 2 Connecting to File

Standard error Redirection (redirect stderr):2>

Standard input Redirect (redirect stdin):<

(Ps:linux has a tradition: everything is a file, such as a directory is a directory files, all kinds of devices are abstracted into files in the directory file/dev, the system has three very special files, because they are open, Therefore, the system assigns them three fixed file descriptor, the file descriptor 0 is called the standard input file (stdin), 1 is called the standard output file (stdout), 2 is called the standard error output file (stderr), So you'll see the standard error output redirect identifier:2>)

In general, standard input redirects are less frequently used than pipelines, such as: Cat File1|uniq|grep str|sort (Uniq ignores duplicate rows, grep prints matching rows, sort sorts each line of string)

    • User and file permissions

There is a tradition in Linux: Home is writable and not outside.

Three kinds of permissions: The user mainly has three kinds of permissions to the file: R (Reading), W (writing), X (executing).

Three types of users: Owner, Group, World

The discussion of permission is to look at the r/w/x permission of owner, group and world to the file.

See File permissions in Terminal: $ ls-l a.txt, Get results (Note: View directory permissions, Ls-ld Mydir):

-rw-rw-rw-. 1 Xpjiang Xpjiang 6 December 21:31 a.txt

The information conveyed by the Bank is as follows:

The r permission on the file indicates that the contents of the file can be viewed (cat file), the R permission on the directory can view the contents of the directory (LS mydir), the file's W permission means that the contents of the file can be edited, and the W permission on the directory indicates that we can create, delete, rename the file in this directory The X permission on the file indicates that it can be executed as a program, and the X permission on the directory indicates that we can enter the directory (CD Mydir).

Add Write permission to the current directory: chmod +w.

Create a script file with the VIM editor A.sh:vim a.sh (Type I into insert mode, write a simple print command: Echo HELLO,ESC exit insert mode, two caps Z save file), execute the script:. A.sh, in general, we do not have execute permissions, can do: chmod +x a.sh, execute the script, terminal output: Hello.

chmod (change file mode): Chmod 666 a.txt (666 for the octal representation of file mode)

    • Process

Get the process number (pid,process ID)

Start vim in the terminal, it will occupy the current shell, Knock ctrl+shift+t (edit-like keyboard shortcuts to see the shortcut keys used in the terminal, you can also customize shortcut keys, such as ctrl+shift+c copy content, ctrl+shift+ V paste) Open a new Terminal tab, PS aux view all the process on the system, because the output is longer, we can add a pipe symbol (|) behind the PS aux, and use less to do the paging device: PS aux|less, the PID field in the output is the process number we are concerned about. You can see the process number of the VIM program by typing/vim, but the more general way to view the process number is to use the grep command to find the VIM keyword: PS aux|grep vim, using the kill vim process number to end the VIM program (Ctrl+pgup, Switch to the previous Terminal tab to see that the VIM program has been killed).

Background execution

You can also start the graphical program on the command line, such as Firefox,alt+tab can switch back to the command line, you can see that Firefox occupies the current shell, how to deal with this situation? One simple way is Firefox &, so Firefox automatically executes in the background, and the command line can still be used after switching back to the terminal. But if Firefox has appeared in the shell this situation, you can Ctrl + Z, found that Firefox has not responded, cut back to the terminal, type BG, so as in the case of Firefox &, FG Switch back to the foreground, CTRL + C End Firefox (bg: Put a task in the background, FG: Put a task in the foreground to execute).

Kill

The commonly used signals in the KILL command are 2, 9, 15. Where 2 means interrupt, implementation and CTRL + C function, sent by the terminal, usually it will terminate a program, 15 means termination, is the default signal sent by the KILL command, if the program is still "alive", can accept the signal, 9 means kill, this signal is very special, because the process may choose different ways To handle the signal sent to it, which also includes ignoring the signal so that the Signal No. 9th is not sent to the target process, but the kernel immediately terminates the process. When a process terminates in this way, it does not have the opportunity to do "clean up" work, or to preserve the fruits of labor. For this reason, you can think of signal number 9th as the killer (kill-9 process number), and then use it when the other termination signal fails.

In addition to terminating the process through kill, many programs also define their own exit methods, such as the shell can be exited by the Exit command, which is equivalent to Ctrl+d.

By the way, in a worse case, an unusual program consumes a lot of memory, causing the entire desktop system to get stuck, so we can't enter commands in the terminal, what do we do now? Linux is a ctrl+alt+f2 that runs seven workstations at the same time to reach one of the workstations where the process is killed and ctrl+alt+f1 back to the workbench that originally ran the graphical interface.

    • Find

File and string search is a very important part of a developer's daily work, let's look at what commands are available in the shell for us to use to achieve our needs:

Locate-Find files in the system global scope by name

Example: The Locate Vimrc,locate command also supports regular expressions: Locate--regexp VIM[A-Z]RC

During use, sometimes a strange phenomenon is found, such as Touch Aa.txt first, then locate aa.txt, found no output in the terminal, through the man locate learned locate reads one or more databases prepared by UpdateDB, in other words, locate is not directly searching the entire file system, but reading data from a database, which is why locate run so far, the command updatedb in the system will put the data in the system into the database, But UpdateDB is usually run automatically once a day, so the file we just created locate is not found, the solution is to run it manually: sudo updatedb.

Find-searches for files in the directory hierarchy, supports many parameters, and is often used with string lookup tool grep to work with the list of searched files.

Example: Find. Lists all directories and files in the current directory, including subdirectories of files and directories.

Find. | grep. txt, listing only names containing. txt directories and files

Find. -type F, list only files.

Find. -type f-exec ls-l ' {} '; ',-exec's Terminator is '; ', and the two can enter any system command between them. ' {} ' represents the file name we found earlier.

echo Hello >a.txt echo Hello >b.txt find. -type f-exec grep-n hell0 ' {} '; '-print,-print prints the file where the match (containing the string "Hello") is located,-n prints the line number where the match is located, and if you want to ignore the case, either in grep followed by the parameter-I, or directly -ni.

    • Network operation

The network operation mainly involves two pieces of content: remote operation tool SSH and data transmission tool rsync. Because there is no network host available for operation, this part of the content is temporarily skipped. More information can be found in the book: The Linux Command line, chapter 17th (network System).

    • Software Installation

There are three main ways of installing software on CentOS:

    1. Manually download the compiled software or the source code to install

    2. Download RPM package installation (similar to Ubuntu system installation Deb package)

    3. Download and install from the official repository with Yum (similar to download from Ubuntu website, install with Apt-get)

Manually download the compiled software (executable program) Installation instance:

From the URL http://sublimetext.com/, download the Sublimetext editor. Using the tar jxvf sublime\ text\ 2.0.2.tar.bz2 decompression, but the executor must use the full path to execute the Sublime_text in the package, if it is convenient to use, turn it into a system command, you need to move the entire unpacked package of sub-files to the PATH contains a directory (using the command echo $ path to view), or you can not move, by doing a symbolic link: $ ln-s sublime\ text\ 2.0.2/sublime_text ~/bin/subl, The Sublimetext editor can be opened by entering SUBL directly in the terminal.

Manually download the source installation example:

First, from the URL http://ftp.gnu.org/gnu/hello/, download hello-2.2.tar.bz2, execute the command sequentially:

12345 tar  jxvf hello-2.2. tar .bz2 CD  hello-2.2 $ . /configure make sudo  make   install

Execute $ hello in terminal, output: Hello, world!. here. The role of/configure is to check whether the current environment satisfies the dependency of the software being installed, and make is used to compile from the makefile, while the make install is used to read the instruction from the makefile and install the software to the specified location.

Install RPM Package: Rpm-i RPM Package

Download and install using Yum: To install git: Execute commands $ sudo yum install git,git software rpm package is downloaded to the local hard drive, while the Yum command wraps the rpm command, so the installation configuration process is completed, that is, a yum Insta ll git command, git can be used directly, if you want to uninstall git, just execute the sudo yum remove git.
    • Shell script Programming

It is called shell scripting programming to centralize many commands into one file as a script.

About shell scripting we are divided into the following sections:

Specifying the parser

Scripts can be written in a language such as Bash, Ruby, or Python, as long as you specify what syntax to parse in the first line of the script (the syntax of Bash is very robotic and many details are obscure, so you can write scripts in a more human language like Ruby). Such as:

#!/usr/bin/env bash or #!/usr/bin/env Ruby or #!/usr/bin/env python

A command is a statement, a statement is a command

Because each command is a legitimate script statement, you can list multiple statements directly into the script or wrap them in a function:

1234567891011 #!/usr/bin/env bashsay_hello(){  echo"hello"}create_file(){  toucha.txt}say_hello # 通过呼叫函数名来执行create_file

In addition, some elements are part of a script statement:

1234 if[ $# != 1 ] then   echo"hello" fi 
Notice that it [ $# != 1 ] can actually be put in the terminal to perform 
123 $[ $# != 1 ]echo$?0
Viewing return means 0 indicates that the command executed successfully. As long as the if is followed by a command that executes correctly, the result is true, and if you follow a command that performs an error, the result is false. In addition, because the command is sensitive to whitespace, this causes the script statement to be sensitive to whitespace as well.

Position parameters

Execute the following script: $./xpjiang.sh a.txt B.txt (requires $ chmod +x xpjiang.sh before execution):

123456 #!/usr/bin/env bashecho"Number of arguments: $#The program name is: $0The first argument is: $1"

The output is as follows:

123 Number of arguments: 2The program name is: ./xpjiang.shThe first argument is: a.txt

Here, the $ A represents the command name, which represents the first argument, and the sum represents the second argument ..., $# represents the total number of parameters when the command executes.

The script is no longer executed in the current shell

Each time a script is started, the system opens a child shell for the current shell and executes the statements in the script in the child shell. For example, an environment variable is exported in the script:

1234 #!/usr/bin/env bashexportDirect="/home/xpjiang/mydir/"cd $Directtouchhello.txt

Execute $./xpjiang.sh will find that the output from the Echo $Direct in the current shell is empty and the working directory in the current shell has not changed, which means that the script is not executed in the current shell. If a script is required to execute in the current shell, execute $ source xpjiang.sh, and you can find that the directory is switched to Mydir, while the output of the Echo $Direct is/home/xpjiang/mydir.

Loop control

Write script rename.sh, change mode to executable:

123456 CD   $1 echo  i am  in   " pwd '   # ' pwd ' will take out the output of the command for  file  in   " ls do   MV  $ file   $ file .txt done

The

is added to ~/bin to make it a system command. Execution:

12345 mkdirmydircd mydir; touch a b c; cd ..$ rename.sh mydir  I am in /home/xpjiang/Desktop/mydirlsmydira.txt b.txt c.txt

Remote control

SSH telnet to the server, you need to log in and then execute the command, in practice every time this is cumbersome. You can write a script locally to execute the relevant commands in the script on the server.

Confirm execution

Let the program pause during execution, report some information, the user according to the information provided by the program to determine whether to allow the program to continue to do the following operations, this is the confirmation of execution.

123456789 #!/usr/bin/env bashecho-n "Do you want to say hello? (y/n): "readAAAif"${AAA:-y}""y" ]; then  echohelloelse  echoNothing done, bye.fi

Reference:mooc_liunx Guide for developments. 

More commands to see. NET development must-have Linux commands and I've collated the. NET cross-platform (CentOS)-related documentation for everyone.

Original link: http://www.cnblogs.com/xpjiang/p/5089137.html

90 Minute QuickStart Linux--linux Guide for developments Learning notes

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.