Advanced bash Programming Guide (5)

Source: Internet
Author: User
Tags tar extract

I. Internal commands and built-in commands

1. built-in commands refer to the commands included in the bash toolkit, literally built in, mainly considering the execution efficiency. The built-in commands are faster than the external commands, in part because the external commands are usually fork for execution by a sub-process, and the other part is that the specific built-in commands can directly access the shell kernel.

2. It is worth noting that a built-in command usually has the same name as a system command, but Bash re-implements these commands, but they are the same in most cases.

3. keywords are reserved words that have special meanings and are used to build shell Syntax structures.


(1) echo-n "\ v" # result: \ v-N option does not wrap

(2) echo-e "\ v" # result: 3 vertical tabs-E Option escape

(3) echo $ '\ v \ V' # result: 3 vertical tabs

(4) echo "\" dsfadf \ "" # result:"Dsfadf"

(5) echo 'LS-l' # Delete the line break generated by the "ls-L" command

(6) echo "sdffs

Fdsdaf "# line feed. Here, a line break is added in the center of-N, but there is no line feed at last.

(7) varr = "fdsa

Safd "Echo $ varr # No line break, changed to Space

5. printf supports formatting output.

6. Read

(1)-A reads array Variables

(2) read without parameters saves the read content to the special variable $ reply

(3) input \ to read and press enter to generate a new row. -The R option will allow \ escape

(4)-S does not print output-nn only accepts n characters-P reads the prompt after the input is printed

Read-s N1-P "hit a key" keypress. Do not press ENTER

(5)Direction key encoding

(6)-T specified time input

(7) use the READ command through file redirection

7. set command

(1) set 'uname-a' # using set with positional parameters

Echo $1 # The first string of the 'uname-a' result

Echo $2

# The second string of the 'uname-a' result

(2) Using set with the -- Option explicitly assigns the contents

A variable to the positional parameters. When no variable follow

--, It unsets the positional parameters.

Var = "One Two"

Echo "$1" # One

Echo "$2" # Two

8. unset

The unset command deletes a shell variable, please tively setting it to null.

Note that this command does not affect positional parameters

9. The export Command makes availables to all child processes of the running script or shell. Unfortunately, there is no way to export variables back to the parent process,

The process that called or invoked the script or shell. One import use of the Export command is in startup files, to initialize and make accessible environmental variables to subsequent user processes.

10. Jobs command

Lists the jobs running in the background, giving the job number.

External filter,ProgramAnd commands

2. Basic commands

1. Several options of the LS command

-R recursive option. All files are listed in the directory tree.

-S will list all objects by file size

-T modification time

-I: inode OF THE DISPLAY file

-F mark the directory of the file type to end with/. File *

2. Cat, Tac

-N: insert row numbers before all rows

-B: Same as-N, but no blank rows are numbered.

-S: You can compress multiple empty rows into one empty row.

Tac, starting from the end of the file

3. Rev

(1) bash $Cat file1.txt

This is Line 1.

This is Line 2.

(2)Bash $Tac file1.txt This is Line 2. This is Line 1.
(3)Bash $Rev file1.txt . 1 enil Si siht. 2 enil Si siht

4. Rename the MV command

5. rm

-F: Force delete a file, even if the file is read-only

Rm./-filename or Rm -- filename When deleting a file starting "-"

6. ln [-S] oldfile newfile is a link of oldfile.

-S option soft link

Hard link: like the inode of the original file, changes to the content of any file will be reflected in another file ., However

Deleting or renaming a file does not affect another file.

Soft link: Different inode, but soft links can be linked to files in different file systems or directories.

Three Complex commands

1. Find command

(1) Find ~ /-Name '*. txt'

(2) Find ~ /-Mtime 1 # List objects modified on the last day.

(3) Find ~ /-Name 'core * '-exec RM {} \; # delete all files starting with core

Execute the command after exec in each matching file. It is worth noting that it ends.

";" Is an escape character to ensure that the characters passed by shell to the find command will not be

If the command after exec contains {}, the find command will use all matching files

To replace {}

2. head and tail commands

Head/tail-N number filename # display the content of the first/second number lines of the file filename

-C n # display the first and foremost n characters of the file

Tail-F log.txt # displays the last 10 lines of files, and automatically displays the new file content, especially in

When monitoring logs, you can always display new log information on the screen.

3. expr command

(1) expr 3 + 5 # returns 8. Note the space.

(2) expr 3 \ * 5 # * escape

(3) y = 'expr $ Y + 1' is equivalent to let y = Y + 1, y = $ ($ Y + 1 ))

(4) B = 'expr $ A \> $ B '# test, >,<, >=, <= escape

Four text processing commands

1. Sort inputfile | uniq-c | sort-NR

Sort the file inputfile by row, remove duplicate rows, and calculate

Number of times, sorted in reverse order of numbers.

2. Expand converts each tab to a space, which is opposite to unexpand.

3. WC

WC-WCount the number of words.

WC-lCount the number of rows.

WC-CCount the number of bytes.

WC-mCount the number of characters.

WC-lThe longest line length in the file.

4. tr character conversion Filter

It is reasonable to use quotation marks or brackets. References can prevent shell from re-interpreting special characters that appear in the tr command. Brackets can prevent shell extension.

TR "A-Z" ** "<FILENAME or Tr A-Z \ * <FILENAME

More common practice tr A-Z '[**]' <FILENAME

5. File and archiving commands

1.tar command

(1) tar-CF 1.tar *. jpg: Pack all. jpg files in the current directory into 1.tar.

(2) extract tar-ZF 1.tar to the current directory.

(3) tar-CZF 1.tar *. jpg # package and use gzip for compression. Use Bzip2 for compression when Z is changed to J.

(4) tar-xzf 1.tar # (3) Inverse Process

(5) several other useful options

Tar-TF 1.tar extract the files in 1.tar

Tar-UF 1.tar logo.jpg updated the logo.jpg file in 1.tar.

Tar-RF 1.tar *. gif upload all files to 1.tar

2. File: confirm the question and add type.

3. Which command determines the complete path of the command

4. In addition to the complete path, whereis command also provides the complete path of the man page of the command.

5. Whatis lists system commands and explanations of important configuration files. Equivalent to the name of the man command result

Item content

6. vdir = LS-l

7. query the Filename Path in locate filename

8. readlink: displays the file pointed to by the symbolic link.

9. Diff file comparison tool

-- Side-by-side option, left-right split form output,-c-u Option


The CMP command only specifies the locations that are different and does not display details.

10. Remove the path information from the basename and output only the file name.

Dirname removes the file name and only outputs the path information.

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: 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.