UNIX skills: another 10 habits of UNIX experts-become a master of Unix command lines

Source: Internet
Author: User
Tags ldap file transfer protocol

Excellent as Michael StutzArticleIn the future, this article will provide another 10 good habits to improve your Unix command line efficiency. Understand Common Errors and methods to overcome them, and learn exactly why these 10 UNIX habits are worth using.

Let's face the reality: bad habits are hard to change. However, the habits you are already familiar with may be more difficult to overcome. Sometimes, you may encounter "Aha, I didn't expect it to do this !" . Excellent article in Michael Stutz"Ten habits of UNIX expertsThis article provides 10 other Unix Command Line commands, tools, and technologies to make you a more efficient Unix Command Line expert.

The other 10 good habits you should adopt include:

  • Use the file name completion function ).
  • Use historical extensions.
  • Reuse previous parameters.
  • UsepushdAndpopdManage directory navigation.
  • Search for large files.
  • Do not use the editor to create temporary files.
  • UsecurlCommand line utility.
  • Use regular expressions most effectively.
  • Determine the current user.
  • UseawkProcess data.
Common acronyms
  • MB:MB
  • HTTP:Hypertext Transfer Protocol
  • Https:HTTP over Secure Sockets Layer
  • FTP:File Transfer Protocol
  • Ftps:FTP over Secure Sockets Layer
  • LDAP:Lightweight Directory Access Protocol

Complete with file name

Isn't that great if you don't need to type long and confusing file names at the command prompt? Indeed, you do not need to do this. Instead, you can configure the most popular UNIX shell to use the file name. This function works in a slightly different way in various shells, so I will show you how to use file names in the most popular shell. After the file name is complete, you can enter the file more quickly and avoid errors. Laziness? Maybe. More efficient? Of course!

Which shell is running?

What if you don't know which shell is used currently? Although this tip is not a formal component of the other 10 good habits, it is still useful. For exampleListing 1You can useecho $0Orps -p $$Command to display the shell you are using. For me, bash shell is running.

Listing 1. Determine your shell

                $ echo $0-bash$ ps –p $$PID TTY           TIME CMD6344 ttys000    0:00.02 –bash

C Shell

CShell supports the most direct file name completion function. SetfilecVariable to enable this function. (You can use the commandset filec.) After you start typing the file name, you can pressESCKey, shell will complete the file name, or complete as many parts as possible. For example, assume that you haveFile1, file2AndFile3. If you typef, And then pressESCKey to fillFileAnd you must enter1,2Or3To complete the corresponding file name.


BASH Shell also provides a complete file name, but uses the tab key instead of the ESC key. You can enable file name completion without setting any options in bash shell. This option is set by default. Bash also implements other functions. After you type a part of the file name, pressTabKey. If multiple files meet your request and you need to add text to select one of the files, you can pressTabKey twice to display the list of files that match the content you have currently typed. Before usingFile1, file2AndFile3First, enterf. When you pressTabBash is completeFile; PressTabThe list is displayed.File1 file2 file3.

Korn Shell

For the Korn shell user, the file name is determinedEDITORVariable value. IfEDITORSetVI, Enter a part of the name, and then pressESCKey followed by a backslash (/. IfEDITORSetEmacs, Enter a part of the name, and then pressESCKey to complete the file name.

Use History extension

What happens if you use the same file name for a series of commands? Of course, there is a shortcut to quickly obtain the file name you used last time. For exampleListing 2As shown in,!$The command returns the file name used by the previous command. Slave FileThis-is-a-long-lunch-menu-file.txtSearch words inPickles. UseviCommand to edit the this-is-a-long-lunch-menu-file.txt file without re-typing the file name. You use an exclamation point (!And then use the dollar sign ($) Returns the last field of the previous command. If you repeatedly use long file names, this is a very good tool.

List 2. Use! $ Get the file name used by the previous command

                $ grep pickles this-is-a-long-lunch-menu-file.txtpastrami on rye with pickles and onions$ vi !$    

Reuse previous Parameters

!$Command to return the previous file name parameter used by a command. But what if a command uses multiple file names and you only want to reuse one of them?!:1Returns the first file name used by a command.Listing 3The example in shows how to associate this operator!$Operator combination. In the first command, a file is renamed as a more meaningful name, but a symbolic link is created to keep the original file name available. Rename a fileKxp12.cTo improve readability, and then uselinkCommand to create a symbolic link to the original file name, in case the file name is used elsewhere.!$Operator returnFile_system_access.cFile Name, while!:1Operator returnKxp12.cFile name, which is the first file name of the previous command.

Listing 3. Combined use! $ And! : 1

                $ mv kxp12.c file_system_access.c$ ln –s !$ !:1

Manage directory navigation using pushd and popd

UNIX supports various directory navigation tools. My two favorite tools to improve work efficiency are:pushdAndpopd. Of course you understandcdCommand to change your current directory. If you want to navigate to multiple directories but want to quickly return to a location, what should you do?pushdAndpopdCommand to create a virtual directory stack,pushdCommand to change your current directory and store it in the stackpopdCommand to remove the directory from the top of the stack and return you to this location. You can usedirsCommand to display the current directory stack without pressing in or popping up a new directory.Listing 4Show how to usepushdAndpopdCommand to quickly navigate in the directory tree.

Listing 4. Use pushd and popd to navigate to the directory tree

                $ pushd .~ ~$ pushd /etc/etc ~ ~$ pushd /var/var /etc ~ ~$ pushd /usr/local/bin/usr/local/bin /var /etc ~ ~$ dirs/usr/local/bin /var /etc ~ ~$ popd/var /etc ~ ~$ popd/etc ~ ~$ popd~ ~$ popd

pushdAndpopdThe command also supports using parameters to process directory stacks. Use+n Or-n Parameters, whereNIs a number, you can move the stack to the left or right, suchListing 5.

Listing 5. Rotating directory Stack

                $ dirs/usr/local/bin /var /etc ~ ~$ pushd +1/var /etc ~ ~ /usr/local/bin$ pushd -1~ /usr/local/bin /var /etc ~

Search for large files

Do you need to find out what is occupied by all your idle disk space? You can use the following tools to manage your storage devices. For exampleListing 6As shown in,dfCommand to display the total number of used blocks and the percentage of free space on each available volume.

Listing 6. determine the volume usage

                $ dfFilesystem                            512-blocks      Used  Available Capacity  Mounted on/dev/disk0s2                           311909984 267275264   44122720    86%    /devfs                                        224       224          0   100%    /devfdesc                                          2         2          0   100%    /devmap -hosts                                     0         0          0   100%    /netmap auto_home                                  0         0          0   100%    /home

Do you want to search for large files? UsefindCommand-sizeParameters.Listing 7Shows how to usefindCommand to find files larger than 10 MB. Please note that,-sizeThe parameter is measured in KB.

Listing 7. Search for all files larger than 10 MB

                  $ find / -size +10000k –xdev –exec ls –lh {}/;

Do not use the editor to create temporary files

The following is a simple example: You need to quickly create a simple temporary file without starting your editor. Use>File redirection OperatorcatCommand. For exampleListing 8As shown incatThe command only displays anything typed into the standard input;>Redirection captures the input to the specified file. Note that you must provide the end character of the file when typing the end, usually Ctrl-D.

Listing 8. quickly create a temporary file

                $ cat > my_temp_file.txtThis is my temp file text^D$ cat my_temp_file.txtThis is my temp file text

You need to perform the same operation, but attach it to an existing file instead of creating a new file. For exampleListing 9As shown in, use>>Operator.>>The file redirection operator attaches content to an existing file.

Listing 9. Quickly Append content to a file

                $ cat >> my_temp_file.txtMore text^D$ cat my_temp_file.txtThis is my temp file textMore text

Use curl command line utility

Can I access the Web from a command line? Are you crazy? No, this iscurlUsage!curlYou can use HTTP, https, FTP, ftps, Gopher, dict, telnet, LDAP, or file to retrieve data from the server. For exampleListing 10As shown in, I can usecurlCommand to learn about the current weather conditions in my location (bufaro, New York) from the US National Meteorological Administration. WhengrepWhen using command combinations, I can retrieve weather conditions in bufaro city. Use-sCommand Line option to disablecurlProcessing output.

Listing 10. Use curl to retrieve the current weather condition

                $ curl –s http://www.srh.noaa.gov/data/ALY/RWRALY | grep BUFFALOBUFFALO        MOSUNNY   43  22  43 NE13      30.10R

For exampleListing 11You can also usecurlCommand to download an http-hosted file. Use-oParameter to specify the location where the output is saved.

Listing 11. Using curl to download files hosted by HTTP

                $ curl -o archive.tar http://www.somesite.com/archive.tar

This is actually just what you usecurlCommand to complete the operation prompt. You only need to enterman curlDisplaycurlFor more information.

Use regular expressions most effectively

A large number of Unix commands use regular expressions as parameters. From a technical point of view,Regular ExpressionIs a string that represents a certain pattern (that is, a string consisting of letters, numbers, and symbols), used to define a string of zero or longer. Regular Expressions use metacharacters (for example, asterisks [*] And question mark [?]) To match part or all of other strings. Regular expressions do not necessarily contain wildcards, but they can make regular expressions play a greater role in search mode and file processing.Table 1Displays some basic regular expression sequences.

Table 1. Regular Expression Sequence

Sequence Description
Escape Character (^) Match the expression that appears at the beginning of a row, for example^A
Dollar sign ($) Matches the expression at the end of a row, for exampleA$
Backslash (/) Cancels the special meaning of the next character, for example/^
Square brackets ([]) Match any character, such[aeiou](Use a hyphen [-] Indicates the range, for example[0-9]).
[^ ] Match any character except the enclosed characters, for example[^0-9]
Period (.) Match any single character except the end of the line
Asterisk (*) Matches zero or multiple precursor characters or expressions
/{x,y/} MatchedXToYSame content as before
/{x/} Exact matchXSame content as before
/{x,/} MatchedXOr more with the same content

List 12ThegrepSome basic regular expressions used by the command.

Listing 12. Using Regular Expressions and grep

                $ # Lists your mail$ grep '^From: ' /usr/mail/$USER   $ # Any line with at least one letter  $ grep '[a-zA-Z]'  search-file.txt$ # Anything not a letter or number$ grep '[^a-zA-Z0-9] search-file.txt$ # Find phone numbers in the form 999-9999 $ grep '[0-9]/{3/}-[0-9]/{4/}' search-file.txt$ # Find lines with exactly one character$ grep '^.$' search-file.txt$ #  Find any line that starts with a period "."          $ grep '^/.' search-file.txt $ # Find lines that  start with a "." and 2 lowercase letters$ grep '^/.[a-z][a-z]' search-file.txt

There are a large number of books dedicated to regular expressions. We recommend that you read the"Dialog UNIX, Part 1: Regular Expressions."

Determine current user

Sometimes you may want to determine whether a specific user has run your management script. To find the answer, you can usewhoamiCommand to return the name of the current user.Listing 13DisplayswhoamiCommand;Listing 14Show usagewhoamiMake sure that the current user is not an excerpt from the root user's bash script.

Listing 13. Using whoami from the command line

                $ whoamiJohn

Listing 14. Using whoami in the script

                if [ $(whoami) = "root" ]then   echo "You cannot run this script as root."   exit 1fi

Use awk to process data

awkThe command seems to be always in the shadow of Perl, but it is a fast and practical tool for simple and command line-based data processing.Listing 15Show how to start usingawkCommand. To obtain the length of each line of text in a file, uselength()Function. To view stringsIngUseindex()Function, which returnsIngThis allows you to use it for further string processing. ToTokenize(That is to say, split a row into word-length segments.) For a string, usesplit()Function.

Listing 15. Basic awk Processing

                $ cat texttesting the awk command$ awk '{ i = length($0); print i }' text23$ awk '{ i = index($0,”ing”); print i}' text5$ awk 'BEGIN { i = 1 } { n = split($0,a," "); while (i <= n) {print a[i]; i++;} }' texttesting theawkcommand

Printing specified fields in a text file is simple.awkTask. InListing 16Medium,SalesThe file contains the name of each salesperson, followed by the monthly sales number. You can useawkCommand to quickly obtain the total sales per month. By default,awkEach value separated by commas is considered as a different field. You use$n To access each field.

Listing 16. Use awk to summarize data

                $cat salesGene,12,23,7Dawn,10,25,15Renee,15,13,18David,8,21,17$ awk -F, '{print $1,$2+$3+$4}' salesGene 42Dawn 50Renee 46David 46

awkCommands can be complex and widely used in scenarios. To learn more completelyawkCommand.man awkAnd seeReferencesSome resources.


Some practices are required to become a command line expert. It is easy to solve the problem in the same way, because you are used to it. Expanding your command line resources can significantly improve your work efficiency and lead you to the direction of Unix Command Line experts!



  • You can refer toOriginal ENGLISH.

  • "Ten habits of UNIX experts": Use 10 good habits that can improve the efficiency of your UNIX (r) command line-and get rid of bad usage patterns in this process. This article guides you step by step to learn several techniques used for command line operations, which are very good but are often ignored.
  • "Dialog Unix: Powerful command line": Through this article, you will learn about the basics of Unix shell and how to use the command line to synthesize infinite data for a limited Unix utility group.
  • "System Management Toolkit: standardize your Unix Command Line tools": This article describes how to standardize the interface to simplify moving between different Unix (r) systems. If you manage multiple UNIX systems (especially in heterogeneous environments), the most difficult task may be to switch between different environments and execute different tasks, all the differences between systems must also be considered.
  • "Use free software within using cial Unix": This is a short getting started book on the use of many open-source software for commercial Unix implementations.
  • "Working in BASH Shell": Provides an introductory tutorial on the popular bash shell.
  • "Gawk Introduction: awk language basics": This article describes how to use the awk language to manipulate text.
  • "Skills for constructing Regular Expression Patterns": Understand how to usegrep.
  • UNIX skills: Read other articles in the Unix skills series.
  • "Common Threads: awk by example, part 1","Common Threads: awk by example, part 2","Common Threads: awk by example, Part 3": GetawkAn excellent introduction to usage.
  • "Dialog UNIX, Part 1: Regular Expressions": Learn how to use regular expressions to obtain precision.
  • AIX and Unix Zone: The "Aix and Unix zone" on developerworks provides a wealth of information related to all aspects of Aix system management that you can use to extend your UNIX skills.
  • Getting started with AIX and Unix: Visit the "Aix AIX" page to learn more about AIX and UNIX.
  • Summary of Aix and Unix topics: The AIX and Unix area has already introduced many technical topics for you and summarized many popular knowledge points. We will continue to launch many related hot topics later. To facilitate your access, we will summarize all the topics in this area here, it makes it easier for you to find the content you need.
  • Technical bookstore: Browse the technology bookstore to learn about these and other technology topics.

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.