UNIX command line idioms full ibm official version

Source: Internet
Author: User
UNIX has its own dialect, and its command vocabulary is very large. However, you do not need to master all the content at a time. This article introduces many command line combinations to facilitate your understanding of the UNIX language.

When you travel to countries in different languages, you may need to know some key everyday terms, such as "How much is this ?" "What is this meat ?" And "Where is the restroom ?". Remember these short daily terms to make sure that you don't charge too much for the sandwiches you order, and you know where to go when you need to go to the bathroom.

UNIX®It also has its own dialect. in the past six months, the UNIX series provides a quick tutorial for UNIX command line idioms. This month we will introduce some useful phrases that can make you an authentic UNIX user immediately. Wear your toothbrush, comfortable shoes, and update your idioms. We are going out to welcome the sun, the beach, and the shells. (In the sunshine and beach, face the beach, open the laptop, and read this column. Do not forget to use sunscreen .)

Start learning

The find command has been introduced many times in the previous UNIX column (see references). It is a very useful utility that can be used to scan and process various files, even the entire UNIX file system. For example, I often use find with grep or Perl to process a large number of files. Do you need to know where variables or constants are defined in a large piece of code? You can try the following command:


Copy codeThe code is as follows:
$ Find/path/to/src-type f | xargs grep-H-I-I-n string

The output of this command is a list of file names, including string, including the line number and matched specific text. The-H and-n options are added before each matching file name and row number. -The I option is case-insensitive. -I (uppercase "I") skips binary files.

You may have never seen xargs before. it uses all the listed options to run the command you specified. In this example, it is grep, and each time it uses a parameter provided through standard input. Assume that the/path/to/src directory contains files a, B, and c. The use of find and xargs is equivalent:


Copy codeThe code is as follows:
Grep-H-I-I-n string
Grep-H-I-I-n string B
Grep-H-I-I-n string c

In fact, searching for a file set is a common task, so grep has options to recursively traverse the entire file system hierarchy. You can use-d recurse or its synonym-R or-r. For example, you can use:


Copy codeThe code is as follows:
$ Grep-H-I-I-n-R string/path/to/src

This command is the same as find/xargs to complete the task. (You will find that many file-related UNIX utilities have recursive options. Ls-R can recursively list the content in a hierarchy. Chmod, chgrp, and chown use-R to recursively apply pattern, group, and ownership changes to the entire file system hierarchy. Be careful when using chmod-R. If you delete the directory execution bit, such as chmod-R a-x, you may make a directory unavailable. To be more selective, you can use find.-type f | xargs chmod a-x .)

So when should we use find/xargs and grep? You can use find when you need to be selective. The find command has many options so that you can select files that meet specific requirements, such as "all regular files modified after midnight and owned by Joe ". Otherwise, use grep-R.

Another utility may be more convenient and faster than find. If you want to find a file by name, you can use locate instead of find-name. The locate command periodically (once every day, set by the system administrator) compiles directories for all files in the system and constructs a database composed of paths and file names. When you run locate, it will scan its private database and try to match.

For example, run the query locate '*. 1' to get all the files and directories whose names end with. 1. (The asterisk above indicates matching any string .) For convenience, running the locate fish command is the same as running the locate '* fish *' command.

Currency replacement

There are many UNIX utilities that can modify files. In most cases, the modified content can be sent to the standard output, and you can use the redirection operator to further process it (using the pipeline "| ") or capture the results (using the> or> operator ).

Other utilities (tools that can normally process many files at a time) can retain the original file for security considerations and generate a new file for the modified content. For example, you can use Perl in the command line to process files. Run the following command:


Copy codeThe code is as follows:
$ Perl-I. bak-pe's/\ bdollar (s ?) /Buck \ 1/g' file.txt

Replace dollar with buck and dollars with bucks ". The perl-I command is used to modify file.txt, while the perl-I. bak creates a copy of the original file and adds it after its name. bak, which is different from the new and modified version. Therefore, the following command:


Copy codeThe code is as follows:
Perl-I. bak-pe's/\ bdollar (s ?) /Buck \ 1/g '*

A backup is created for each file in the current directory. Suppose there are file1.txt1_file2.txt and file3.txt files, you will get file1.txt.bak1_file2.txt. bak and file3.txt. bak. Errors often occur, so it is wise to create a backup.

If an error occurs and the original file must be restored, you only need to enter:


Copy codeThe code is as follows:
Mv file1.txt. bak file1.txt

. However, what should I do if there are hundreds of files to be renamed? Of course, you do not want to enter hundreds of separate mv commands. Instead, you can enter the following command:


Copy codeThe code is as follows:
Foreach file in (*. txt)
Do
Mv $ file. bak $ file
Done

It applies to some simple situations, such as the situations in this example. However, this type of task is very common. you can use another special utility that can accomplish this task more quickly. Run the following command:


Copy codeThe code is as follows:
$ Rename's/\. bak $ // '*. bak

The same task is executed. Regular expression s /\. bak $ // after each file name listed in the command line. delete bak. In this example, it is * or all files and the shortened name is used as the target file name.

The rename command is particularly useful when there are no regular file names. For example, you can consider the contents in the following Directory. it looks like a collection of letters from a freshman.


Copy codeThe code is as follows:
$ Ls
Cmd.txt bEErMoNey.txt gASmoNey. TXt

The preceding foreach script cannot solve this problem because these file names are irregular. Rename can easily process it:


Copy codeThe code is as follows:
$ Rename 'Y/A-Z/a-z /'*

The y operator in the regular expression y/A-Z/a-z/is used for conversion. Conversion requires two lists: one original character list and one replacement character list. If the two lists are of the same size, replace the instance with the first character in the original list with the first character in the list. In other words, in this example, each instance with uppercase "A" is replaced with lowercase "a" and "B" with "B", and so on. The lowercase letters in the text remain unchanged.

If you need to preview the work executed by rename, you can add the-n option. This option displays the jobs executed by the command, but does not actually make these changes:


Copy codeThe code is as follows:
$ Rename-n 'y/A-Z/a-z /'*
Cmd.txt renamed as cmd.txt
BEErMoNey.txt renamed as beermoney.txt
GASmoNey. TXt renamed as gasmoney.txt
$ Rename 'Y/A-Z/a-z /'*
$ Ls
Beermoney.txt gasmoney.txt example .txt

One disadvantage is that file names are case sensitive in UNIX systems. A directory may contain Aa. Txt and aA. txT. As mentioned above, you can write a renaming rule to convert a case-sensitive file name to a lower-case file name, which may conflict with a unique existing file name. In this case, what will rename do? Let's take a look:


Copy codeThe code is as follows:
$ Rename-n 'y/A-Z/a-z /'*
Aa. Txt renamed as aa.txt
AA. txT renamed as aa.txt
$ Rename 'Y/A-Z/a-z /'*
AA. txT not renamed: aa.txt already exists
$ Ls
AA. txT aa.txt

If you want to delete an existing file during the rename process, you can add the-f flag. In this example, a file named aa.txt is obtained. Which file is its original file? Since rename is processed alphabetically, the following aA. txT file is the current aa.txt file. Why use-f? If the two files are the same and only have different names, rename-f will delete the duplicate files.

Do not delete duplicate files

File management is a very important task when using UNIX systems. The system contains a large number of configuration files. You may have a lot of data files and personal files. You may need to delete or overwrite a valuable file from time to time. Shell and some file management utilities can help you avoid disasters.

Enter the following command at the Shell prompt. These commands can be executed in bash, but zsh and other shells have similar options.


Copy codeThe code is as follows:
$ Alias mv = mv-I
$ Alias rm = rm-I
$ Set-o noclobber

The first two commands replace mv with mv-I in the command line, and rm with rm-I. The interactive mode forces you to confirm the operation.

The Third Command provides certain security in Shell. After noclobber is enabled, you will not accidentally use> rewrite operator to overwrite a file:


Copy codeThe code is as follows:
$ Ls
Secret.txt
$ Cat> secret.txt
Bash: secret.txt: cannot overwrite existing file

To disable noclobber, enter:


Copy codeThe code is as follows:
Set + o noclobber

. You can also use the> | (a minor sign plus a vertical line) redirection operator to force overwrite at any time.


Copy codeThe code is as follows:
$ Cat secret.txt
I love green eggs and ham.
$ Echo "No more secrets"> | secret.txt
$ Cat secret.txt
No more secrets

Some local secrets

If you really want to find a city, visit the local public forum. The following is a combination of command lines, which is equivalent to Zagat that provides travel information.

Mkdir-p can quickly create a hierarchy. With The-p option, mkdir will create all directories and subdirectories for the specified path:


Copy codeThe code is as follows:
$ Mkdir-p make/packages/directories/at/once
$ Ls-R
./Make:
Bytes

./Make/configure:
Directories

./Make/movie/directories:
At

./Make/movie/directories/:
Once

./Make/movie/directories/at/once:

If you need to know the time of the next pay-as-you-go day, you only need to enter cal. If no parameter is specified, cal displays the calendar of the current month. The cal-3 Command displays the calendar of last month, this month, and next month, while cal 06 2009 displays the calendar of June 2009. (My birthday is a Monday of that year !)


Copy codeThe code is as follows:
$ Cal

November 2006
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
$ Cal 06, 2009

Jun 2009
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Because UNIX has many commands, it is unlikely to remember all the options of all utilities. In fact, sometimes you cannot even remember the name of a utility.

Man can be used in case of any difficulty. For example, to view how to use man itself, enter man. With man rm and man mv, you can also view explanations about rm and mv. In addition, if you want to find a topic, you can use man-k to find the man page list related to the topic.


Copy codeThe code is as follows:
$ Man-k cron
Cron (8)-daemon to execute scheduled commands (Vixie Cron)
Crontab (1)-maintain crontab files for individual users (V3)
Crontab (5)-tables for driving cron
Dh_installcron (1)-install cron scripts into etc/cron .*

In this example, man finds the man page of some utility tools. one line of description contains the word cron. These man pages may explain how to use cron, a daemon for System Task scheduling.

So what does the value mean? Each value represents a part of the online UNIX manual. Part 1 retains all commands that UNIX users can run in Shell. Part 1 describes some file formats. Part 1 lists system management commands. Other parts describe system call (2), Library call (3), and so on.

As you can see, most commands generate some types of output. Most command line commands use standard output to display results. However, some other commands use standard output and standard errors, and display the processing process and error messages in sequence. If you want to ignore this type of output (this is very valuable because it can usually intervene in the operations executed in the command line), you can redirect the output to the UNIX bit bucket, /dev/null. These bits can only be input and cannot be output.

The following is a simple example:


Copy codeThe code is as follows:
$ Ls
Secret.txt
$ Cat secret.txt
I am the Walrus.
$ Cat secret.txt>/dev/null
$ Cat socrates.txt>/dev/null
Cat: socrates.txt: No such file or directory
$ Cat socrates.txt> &/dev/null
$ Echo Done.
Done.

If you redirect cat's standard output to/dev/null, no content will be displayed because all bits have been sent to the virtual "permanent vertical file ". However, if an error occurs, the error message sent to the standard error is displayed. If you want to ignore all outputs, you can use the> & operator to discard stdout and stderr.

You can also use/dev/null as a zero-length file to clear existing files or create new blank files:


Copy codeThe code is as follows:
$ Cat secret.txt
Anakin Skywalker is Darth Vader.
$ Cp/dev/null secret.txt
$ Cat secret.txt
$ Echo "The moon is made of cheese! "> Secret.txt
$ Cat secret.txt
The moon is made of cheese!
$ Cat/dev/null> secret.txt
$ Cat secret.txt
$ Cp/dev/null newsecret.txt
$ Cat newsecret.txt
$ Echo Done.

Done. By the way, if you use UNIX in the Macintosh, you can try the open command in a terminal window. For example, if the current working directory contains a file named poodle.jpg, the command open poodle.jpg will start Preview and open poodle.jpg. Preview is the built-in image viewer in Mac OS X. Mac OS X open is the bond between the command line and the Macintosh window environment, and it is much faster than Finder.

The following is a summary!
Oh! Although this is a high-speed train, you are now ready to explore UNIX in depth. You even know that you should discard some content when you do not need it.

As before, there is more to be introduced. In the next few months, the UNIX series of dialogs will explore job control and regular expressions (a strange dialect, but not hard to grasp), how to compile a new utility for downloading from the Internet, and so on.

Don't forget to put some sunscreen oil on it!

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.