Unix command line Idioms

Source: Internet
Author: User
Tags chmod command line file system

UNIX® has its own dialect, and its command vocabulary is very large. But you don't need to master all the content at once. This article describes a number of command line combinations that promote your mastery of UNIX languages.

When you travel to a country that uses different languages, you may need to master some of the key everyday phrases, such as "How much is this thing?" "and" What is this meat? " "and" Where is the restroom? ”。 Remember these short everyday words to make sure you don't ask for too much of the sandwiches you ordered, and you know where to go when you need to pee.

UNIX® also has its own dialect, in the past 6 months, the UNIX series provides a quick tutorial on Unix command-line idioms. This month we'll introduce some useful phrases that will enable you to become an authentic UNIX user immediately. Bring your toothbrush, put on your comfortable shoes, and update your idioms. We're going out to meet the sun, the sand and the shells. (In the sun and sand, facing the beach, open your laptop, and read this column.) Don't forget to rub some sunscreen on. )

Start a journey of learning

The Find command has been introduced several times in previous dialog UNIX columns (see Resources), a useful utility for scanning and processing various files, even the entire UNIX file system. For example, I often use find with grep or Perl to work on a large number of files. Do you need to know where a variable or constant is defined in a large section of code? You can try the following command:

$ find /path/to/src
-type f | xargs grep -H -I -i -n
string

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

You may not have seen Xargs before, and it will run the command you specified using all the options listed, in this case grep, each using one of the parameters provided through standard input. Suppose the/path/to/src directory contains files A, B, and C, using Find and xargs equivalent to:

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 file sets is a common task, so grep has the option to recursively traverse the entire file system hierarchy. You can use the-D recurse or its synonyms-R or-R. For example, you can use:

$ grep -H -I -i -n -R string
        /path/to/src

This command completes the same task as Find/xargs. (You'll find that many file-related UNIX utilities have recursive options.) Ls-r can recursively list the contents of a 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 a directory's execution bit, such as Chmod-r a-x, you may make a directory unusable. To be more selective, you can use Find. -type F | Xargs chmod a-x. )

So, when should you use Find/xargs, and when should you use grep? Find can be used when a certain selectivity is required. 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, you can use the grep-r.

Another utility might be more convenient and faster than find. If you plan to find a file by name, you can try using locate instead of find-name. The Locate command is periodically (approximately once a day, set by the system administrator) to catalog all the files in the system and build a database of paths and file names. When you run locate, it scans its private database for an attempt to match.

For example, running a query locate ' *.1 ' will get all the files and directories with names ending 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* '.

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.