Another 10 habits of UNIX experts

Source: Internet
Author: User
Tags file transfer protocol
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 have to review some things. I didn't expect it to do this! . In MichaelStutz's "10 habits of UNIX masters ",

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 !" . Based on Michael Stutz's "Ten habits of UNIX Masters", this article provides 10 other UNIX command line commands, tools, and technologies, this allows you to become 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.
  • Use pushd and popd to manage directory navigation.
  • Search for large files.
  • Do not use the editor to create temporary files.
  • Use the curl command line utility.
  • Use regular expressions most effectively.
  • Determine the current user.
  • Use awk to process 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. As shown in listing 1, you can use the echo $0 or ps-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

C Shell supports the most direct file name completion function. Set the filec variable to enable this function. (You can run the set filec command .) After you start typing a file name, you can press Esc to complete the file name or finish as many parts as possible. For example, assume that you have a file named file1, file2, and file3. If you type f and press Esc, the file is filled, and you must type 1, 2, or 3 to complete the file name.

Bash

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, press the Tab key. if multiple files meet your request and you need to add text to select one of the files, you can press the Tab key twice to display the list of files that match your current input. Use a File example named file1, file2, and file3. first, Type f. When you press the Tab key once, Bash completes file. when you press the Tab key again, the list of file1 file2 file3 is displayed.

Korn Shell

For a Korn Shell user, the file name is completed depending on the value of the EDITOR variable. If the EDITOR is set to vi, enter a part of the name, press Esc, followed by a backslash. If the EDITOR is set to emacs, type a part of the name and press Esc twice 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. As shown in list 2 ,! $ The Command returns the file name used by the previous command. Search the position where the word pickles appears from the file this-is-a-long-lunch-menu-file.txt. After the search ends, use the vi command to edit the this-is-a-long-lunch-menu-file.txt file without retyping the file name. You use an exclamation point (!) And then use the dollar sign ($) to return 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 returns 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 ?! : 1 operator returns the first file name used by a command. The example in listing 3 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 the file kxp12.c to improve readability. Then, use the link command to create a symbolic link to the original file name to avoid using the file name elsewhere .! $ Operator returns the file_system_access.c file name, and! The: 1 operator returns the kxp12.c file 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. Two of my favorite productivity improvement tools are pushd and popd. Of course, you know how to use the cd command 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? The pushd and popd commands create a virtual directory stack. the pushd command is used to change your current directory and store it in the stack, the popd command is used to remove the directory from the top of the stack and let you return this location. You can use the dirs command to display the current directory stack without pressing in or popping up a new directory. Listing 4 shows how to use the pushd and popd commands to quickly navigate to 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

Pushd and popd commands also support processing directory stacks using parameters. Use the + n or-n parameter, where n is a number, you can move the stack left or right, as shown in listing 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. As shown in listing 6, the df command shows you the total number of blocks used on each available volume and the percentage of free space.

  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? The-size parameter is attached to the find command. Listing 7 shows how to use the find command to find files larger than 10 MB. Note that the-size 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 the cat command with the> file redirection operator. As shown in listing 8, use the cat command without a file name to display only any content typed into the standard input;> redirect to capture 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. As shown in listing 9, use the> operator instead.> 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 is the purpose of curl! The curl Command allows you to use HTTP, HTTPS, FTP, FTPS, Gopher, DICT, TELNET, LDAP, or FILE protocols to retrieve data from the server. As shown in list 10, I can use the curl Command to learn about the current weather conditions in my location (bufaro, New York) from the US National Meteorological Administration. When used in combination with the grep command, I can retrieve weather conditions in bufaro city. Use the-s command line option to disable curl from processing 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

As shown in listing 11, you can also use the curl Command to download an HTTP-hosted file. Use the-o parameter 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 a prompt that you can complete the operation using the curl Command. You only need to enter man curl at the command prompt to display the complete usage information of the curl command.

Use regular expressions most effectively

A large number of UNIX commands use regular expressions as parameters. Technically speaking, a regular expression is a string that represents a certain pattern (that is, a string consisting of letters, numbers, and symbols) and is used to define a string of zero or longer length. Regular expressions use metacharacters (for example, asterisks [*] and question marks [?]) 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 1 shows 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

Listing 12 shows some basic regular expressions used with the grep 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. For an in-depth description of the command line regular expression, we recommend that you read the developerWorks article "speaking 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 out the answer, you can use the whoami command to return the name of the current user. Listing 13 shows the whoami command that runs independently; listing 14 shows an excerpt from the Bash script that uses whoami to ensure that the current user is not the root user.

  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

The awk 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 15 shows how to start using the awk command. To obtain the length of each line of text in a file, use the length () function. To check whether the string ing appears in the file text, use the index () function. This function returns the position of the first appearance of ing, in this way, you can use it for further string processing. To tokenize a string (that is, to split a row into segments of the word length), use the split () 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 a simple awk task. In listing 16, the sales file contains the name of each salesperson, followed by the monthly sales number. You can use the awk command to quickly obtain the total sales volume per month. By default, awk treats each value separated by commas as a different field. You can use the $ n operator 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 awkStart.

Conclusion

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!

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.