In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about what UNIX command line idioms are about. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Start the learning journey.
The find command, which has been introduced several times in previous conversational UNIX columns, 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:
The 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 containing the string, including the line number and the specific text that matches. The-H and-n options are added before each matching file name and line number, respectively. The-I option ignores case. -I (uppercase "I") skips binaries.
You may not have seen xargs before, and it will run the command you specified with all the options listed, in this case grep, one parameter at a time provided through standard input. Assuming that the / path/to/src directory contains files a, b, and c, using find is equivalent to xargs:
The code is as follows:
Grep-H-I-I-n string a
Grep-H-I-I-n string b
Grep-H-I-I-n string c
In fact, searching for filesets is a common task, so grep has the option to recursively traverse the entire file system hierarchy. You can use-d recurse or its synonyms-R or-r. For example, you can use:
The code is as follows:
$grep-H-I-I-n-R string/path/to/src
This command accomplishes the same task as find/xargs. You will find that many file-related UNIX utilities have recursive options. Ls-R can recursively list the contents of the hierarchy. Chmod, chgrp, and chown use-R to recursively apply schema, group, and ownership changes to the entire file system hierarchy. Please be careful when using chmod-R. If you delete the execution bit of a directory, such as chmod-R Amurx, you may make a directory unusable. To be more selective, you can use find. -type f | xargs chmod a Meix. )
So, when should I use find/xargs and when should I use grep? You can use find when you need to be selective. The find command has many options that allow you to select files that meet specific requirements, such as "all regular files that have been modified after midnight and are owned by Joe." Otherwise, grep-R is fine.
Another utility may be easier to use and faster than find. If you plan to find a file by name, try using locate instead of find-name. The locate command periodically (about once a day, set by the system administrator) catalogs all files in the system and builds a database of paths and file names. When you run locate, it scans its private database to try to match.
For example, running the query locate'*. 1 records will result in all files and directories whose names end with. 1. (the preceding asterisk indicates that any string is matched. For convenience, running the locate fish command is the same as running locate'* fish*'.
Currency substitution
There are many UNIX utilities that can modify files. In most cases, the modified content can be sent to standard output, and you can use the redirect operator to further process it (using the pipe "|") or capture the results (using the > or > > operator).
Other utilities (those that can usually process many files at a time) can retain the original file for security reasons and generate a new file for the modified content. For example, you can use Perl to process files directly on the command line. The following command:
The code is as follows:
$perl-i.bak-pe's /\ bdollar (s?) / buck\ 1Universe g 'file.txt
Replace "dollar" with "buck" and "dollars" with "bucks". The perl-I command modifies file.txt in place, while perl-i.bak makes a copy of the original file and adds .bak after its name to distinguish it from the new, modified version. Therefore, the following command:
The code is as follows:
Perl-i.bak-pe's /\ bdollar (s?) / buck\ 1Universe g'*
A backup is created for each file in the current directory. Suppose you have the files file1.txt, file2.txt, and file3.txt, and you'll get file1.txt.bak, file2.txt.bak, and file3.txt.bak. Errors occur frequently, so it is wise to establish a backup.
If an error occurs and the original file must be restored, you simply enter:
The code is as follows:
Mv file1.txt.bak file1.txt
. But what if there are hundreds of files that need to be renamed? Of course, you don't want to enter hundreds of separate mv commands. Instead, you can enter the following command:
The code is as follows:
Foreach file in (* .txt)
Do
Mv $file.bak $file
Done
It applies to simple situations, such as the one in this example. However, such tasks are so common that you can use another special utility that can accomplish this task more quickly. The following command:
The code is as follows:
$rename's /\ .bak $/ /'* .bak
Performed the same task. The regular expression s /\ .bak $/ deletes the .bak after each file name listed on the command line, in this example * or all files, and uses the shortened name as the target file name.
The rename command is especially useful when the file name is irregular. For example, consider the contents of the following directory, which looks like a collection of letters from freshmen.
The code is as follows:
$ls
RenT.txt bEErMoNey.txt gASmoNey.TXt
The above foreach script cannot deal with this problem because these file names are irregular. Rename can easily handle it:
The code is as follows:
$rename'yUnix Amurazip a MuhamzUnip'*
The y operator in the regular expression y/A-Z/a-z/ is used for transformation. The conversion requires two lists: an original character list and a replacement character list. If the two lists are the same size, in this text, replace the instance of the first character in the original list with the first character in the replacement list. In other words, in this example, each instance of uppercase "A" will be replaced with lowercase "a", "B" with "b", and so on. The lowercase letters in the text remain unchanged.
If you need to preview the work performed by rename first, you can add the-n option. This option shows the work performed by the command, but does not actually make these changes:
The code is as follows:
$rename-n'yUnip Amurz Uniqa a Muyzono'*
RenT.txt renamed as rent.txt
BEErMoNey.txt renamed as beermoney.txt
GASmoNey.TXt renamed as gasmoney.txt
$rename'yUnix Amurazip a MuhamzUnip'*
$ls
Beermoney.txt gasmoney.txt rent.txt
There is one drawback to avoid: in UNIX systems, file names are case-sensitive. A directory may contain Aa.Txt and aA.txT. As mentioned above, you can write a renaming rule to convert case-sensitive file names to lowercase file names, which may conflict with previously existing unique file names. What will rename do in this case? Let's take a look at this:
The code is as follows:
$rename-n'yUnip Amurz Uniqa a Muyzono'*
Aa.Txt renamed as aa.txt
AA.txT renamed as aa.txt
$rename'yUnix Amurazip a MuhamzUnip'*
AA.txT not renamed: aa.txt already exists
$ls
AA.txT aa.txt
If you want to delete an existing file during renaming, you can add the-f flag. In this example, you will get a file named aa.txt. So which file is its original file? Because rename is processed in alphabetical order, the following aA.txT file is now aa.txt. Why use-f? If the two files are the same, but with different names, rename-f deletes the duplicate files.
Do not delete duplicate files
File management is a very important work when using UNIX system. 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 Shell have similar options.
The code is as follows:
$alias mv=mv-I
$alias rm=rm-I
$set-o noclobber
The first two commands replace mv with mv-I and rm with rm-I on the command line, respectively. Interactive mode forces you to confirm the operation.
The third command provides some security in Shell. Once noclobber is enabled, you will not accidentally overwrite a file with the > redirect operator:
The code is as follows:
$ls
Secret.txt
$cat > secret.txt
Bash: secret.txt: cannot overwrite existing file
To disable noclobber, enter:
The code is as follows:
Set + o noclobber
. You can also force overrides at any time using the > | (a less than sign plus a vertical bar) redirection operator.
The 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 secrets about the place.
If you really want to find a city, then you need to visit the local public meeting place. Here are some command line combinations, the equivalent of Zagat that provide travel information.
Mkdir-p can quickly create hierarchies. When you use the-p option, mkdir creates all directories and subdirectories for the specified path:
The code is as follows:
$mkdir-p make/many/directories/at/once
$ls-R
. / make:
Many
. / make/many:
Directories
. / make/many/directories:
At
. / make/many/directories/at:
Once
. / make/many/directories/at/once:
If you need to know the time of the next payday, just enter cal. When there are no parameters, cal displays the calendar for the current month. The cal-3 command displays the calendar for the previous month, this month, and the next month, while cal 06 2009 displays the calendar for June 2009. My birthday is some Monday of that year! )
The 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
June 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 not possible to remember all the options for all utilities. In fact, sometimes you can't even remember the name of the utility.
When you encounter difficulties, you can turn to man for help. For example, to see how to use man itself, type man man. With man rm and man mv, you can also view explanations for rm and mv. And, if you are clear about the topic you are looking for, you can use man-k to find a list of man pages related to that topic.
The 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)-installcron scripts into etc/cron.*
In this example, man finds the man pages of some utilities, including a line of description that contains the word cron. These man pages may explain how to use cron, a daemon responsible for system task scheduling.
So what does the numerical value mean? Each value represents a part of the online UNIX manual. Part 1 is reserved for all commands that UNIX users can run in Shell. Part 5 describes some file formats. Part 8 catalogs system management commands. Other sections describe system calls (2), library calls (3), and so on.
As you can see, most commands produce some kind of output. Most command line commands use standard output to display the results. However, other commands use standard output and standard error and display processing and error messages sequentially. If you want to ignore this type of output (which is valuable because it usually interferes with actions performed on the command line), you can redirect the output to UNIX bit bucket,/dev/null. These seats can only be entered, not out.
Here is a simple example:
The 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 the standard output of cat to / dev/null, nothing will be displayed because all bits have been sent to the virtual "permanent vertical file". However, if an error occurs, an error message sent to standard error is displayed. If you want to ignore all output, you can use the > & operator to discard stdout and stderr.
You can also use / dev/null as a zero-length file to empty an existing file or create a new blank file:
The 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 Macintosh, you can try the open command in a terminal window. For example, if there is a file named poodle.jpg in the current working directory, the command open poodle.jpg starts Preview and opens the image viewer that poodle.jpg,Preview is built into in Mac OS X. Mac OS X open is the link between the command line and Macintosh's windowing environment, and it is much faster than with the help of Finder.
Thank you for reading! This is the end of this article on "what are the idioms of the UNIX command line?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.