Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the 10 good habits of UNIX

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what are the 10 good habits of UNIX". In daily operation, I believe that many people have doubts about what the 10 good habits of UNIX are. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the 10 good habits of UNIX?" Next, please follow the editor to study!

1. Automatic completion of file name

The file name auto-completion function eliminates the need to type a variety of smelly and long file names at the command prompt to avoid typing errors and improve efficiency. This feature works slightly differently from Shell to Shell, so first determine which shell you are using.

What kind of Shell am I running?

You can use the echo $0 or ps-p $$commands to display the Shell you are using.

List1: make sure your shell

$echo $0-bash $ps-p $PID TTY TIME CMD 6344 ttys000 0purl 00.02-bash

C Shell

C Shell supports the most direct filename completion function. This feature is enabled by setting the filec variable (using the command set filec). After you start typing the file name, you can press the Esc key, and Shell will complete the file name or complete as many parts as possible. For example, suppose files named file1, file2, and file3. If you type f and then press Esc, the file will be populated, after which you need to type 1, 2, or 3 to complete the corresponding file name.

Bash

Bash Shell also provides file name completion, but it uses the Tab key. You do not need to set any options in Bash Shell to enable filename completion, which is set by default. Bash also implements other functions. After typing part of the file name, press Tab, and if there are multiple files that satisfy your request, and you need to add text to select one of them, you can press Tab twice more to display a list of files that match what you are currently typing.

Using the previous examples of files named file1, file2, and file3, type f first. When the Tab key is pressed once, the Bash auto-completion shows that when file; presses the Tab key again, the list file1 file2 file3 is expanded.

Korn Shell

For Korn Shell users, the filename completion feature depends on the value of the EDITOR variable. If EDITOR is set to vi, you type a partial name, followed by a backslash (\) character by pressing the Esc key. If EDITOR is set to emacs, you type a partial name, and then press the Esc key twice to complete the file name.

two。 Use history extension

What happens if you use the same file name for a series of commands? There is a quick way to get the file name you used last time. As shown in List 2, the! $command returns the file name used by the previous command.

Search for the location of the word pickles in the file this-is-a-long-lunch-menu-file.txt. When the search is over, use the vi command to edit the this-is-a-long-lunch-menu-file.txt file without retyping the file name. Use the exclamation point (!) To access the history, and then use the dollar sign ($) to return the * field of the previous command. This is a very good tool if you need to use long file names repeatedly.

List2: use! $to get the file name used by the previous command

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

3. Reuse previous parameters

! $command returns the last file name argument used by a command. But what if a command uses multiple file names and you only want to reuse one of them?

The!: 1 operator returns the * file names used by a command.

The example in List 3 shows how this operator can be combined with the! $operator. Among the * commands, rename a file to a more meaningful name, but to keep the original file name available, a symbolic link is created. Rename the file kxp12.c to improve readability, and then use the link command to create a symbolic link to the original file name in case the file name is used elsewhere. The! $operator returns the file_system_access.c file name, while the!: 1 operator returns the kxp12.c file name, which is the * file name of the previous command.

List3: use in combination! $and!: 1

$mv kxp12.c file_system_access.c $ln-s! $! 1

4. Use pushd and popd to manage directory navigation

UNIX supports a variety of directory navigation tools, and my two favorite productivity tools are pushd and popd. You must know that the cd command is used to change the current directory. What if you want to navigate through multiple directories but want to be able to return to a location quickly?

The pushd and popd commands create a virtual directory stack, the pushd command changes your current directory and stores it on the stack, and the popd command removes the directory from the top of the stack and returns you to that location. You can use the dirs command to display the current directory stack.

List4: navigating through the directory tree using pushd and popd

$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

The pushd and popd commands also support the use of parameters to manipulate the directory stack. Use the + n or-n argument, where n is a number and you can move the stack to the left or right:

List5: rotating the directory stack

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

5. Find large files

Sometimes you need to find out what disk space is being taken up by. You can use the following tools to manage storage devices. The df command displays the total number of blocks used and the percentage of free space on each available volume.

List6: determine the usage of each volume

$df Filesystem 512-blocks Used Available Capacity Mounted on / dev/disk0s2 311909984 267275264 44122720 86% / devfs 224 224 0 100% / dev fdesc 2 20% / dev map-hosts 00% / net map auto_home 00% / home

Want to find large files? The-size parameter is attached to the find command. List 7 shows how to use the find command to find files larger than 10MB. Note that the-size parameter measures size in KB.

List7: find all files greater than 10MB

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

6. Do not use the editor to create temporary files

Here's a simple example: you need to quickly create a simple temporary file, but you don't want to start the editor. Use the cat command with the > file redirection operator.

As shown in List 8, use the cat command without a file name to echo only anything typed to the standard input; > redirect captures the input to the specified file. Note that the end-of-file character, usually Ctrl-D, must be provided at the end of the input.

List8: quickly create temporary files

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

To do the same, but attach to an existing file instead of creating a new one. As shown in List 9, use the > > operator instead. > > the file redirection operator appends content to an existing file.

List9: quickly attach content to a file

$cat > > my_temp_file.txt More text ^ D $cat my_temp_file.txt This is my temp file text More text

7. Use the curl command line utility

Want to access Web from the command line? Use the curl command! The curl command enables you to retrieve data from the server using the HTTP, HTTPS, FTP, FTPS, Gopher, DICT, TELNET, LDAP, or FILE protocols.

For example, use the curl command to download files hosted by HTTP. Use the-o parameter to specify where to save the output. "

List10: downloading files with curl

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

The curl command is very powerful, and you can learn more about its functionality through man curl.

8. The most efficient use of regular expressions

Many UNIX commands use regular expressions as arguments. From a technical point of view, a regular expression is a string that represents a pattern (that is, a sequence of letters, numbers, and symbols) that defines a string of zero or longer. Regular expressions use metacharacters (for example, asterisks [*] and question marks [?]) to match some or all of other strings. Regular expressions do not necessarily contain wildcards, but wildcards can make regular expressions play a greater role in searching patterns and processing files. Table 1 shows some basic regular expression sequences.

Table 1. Regular expression sequence

Sequence description

Delimited (^) matches the expression that appears at the beginning of the line, such as ^ A

The dollar sign ($) matches the expression that appears at the end of the line, such as A$

The backslash (\) cancels the special meaning of the next character, such as\ ^

Square brackets ([]) match any character enclosed, such as [aeiou] (use a hyphen [-] to indicate a range, such as [0-9]).

[^] matches any character except the enclosed character, for example, [^ 0-9]

Period (.) Matches any single character except the end of the line

The asterisk (*) matches zero or more leading characters or expressions

There have been x to y matches with the same content as before.

There have been x exact matches with the same content as before.

\ x,\} there have been x or more of the same content as before.

Here are some basic regular expressions that are often used with the grep command:

List11: 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'^\. [Amurz] [aMurz] 'search-file.txt

9. Determine the current user

Sometimes the problem with finding is that you need to determine whether a particular user has run your administrative script. To find out, you can use the whoami command to return the name of the current user:

List12: using whoami from the command line

$whoami John

List13: a Bash script that ensures that the current user is not the root user with whoami

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

10. Using awk to process data

The awk command always seems to be in the shadow of Perl, but it is a fast and practical tool for simple, command-line-based data processing. List 14 shows how to start using the awk command.

To get the length of each line of text in the file, use the length () function. To see if the string ing appears in the file text, use the index () function, which returns the location where ing * * appears so that you can use it for further string processing. To tokenize (that is, split a line into word-length fragments) a string, use the split () function.

List14: basic awk processing

$cat text testing the awk command $awk'{I = length ($0); print I} 'text 23$ awk' {I = index ($0, "ing"); print I} 'text 5$ awk' BEGIN {I = 1} {n = split ($0split a, "); while (I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report