📜 ⬆️ ⬇️

Linux basics from the founder of Gentoo. Part 1 (4/4): Glob-substitutions

The final part of the translation of the first part of the wonderful series of textbooks. The previous passages by reference: beginning , second and third .

In this fourth passage, the use of wild cards * , [] and ? for substituting paths by pattern. And also, summarizes the first part. Enjoy! ;)

Using jokers


Meet the Jokers


In everyday use, Linux often happens when you need to perform a single operation (for example, rm) on a set of file system objects at a time. In such situations, it is often quite burdensome to enter multiple files on the command line:



$ rm file1 file2 file3 file4 file5 file6 file7 file8 

To solve this problem, you can benefit from native support for jokers in Linux. This support is also called “globbing” (for historical reasons; also known as “universal file names” in Russian), it allows you to specify many files at once using the wild concatenation template. Bash and other Linux commands interpret this pattern by scanning the disk for files that satisfy it. So, if you have files file1 to file8 in the current working directory, you can delete them by typing:


')
$ rm file[1-8]

Or if you just want to delete all files with names beginning with file , including the file itself named file , you can type:



$ rm file*

The wild concatenation "*" matches any character or set of characters, or even their absence. Of course, glob jokers can be used for much more than just deleting files, as we will see later.



Unsuitable templates


If you need to list all the filesystem objects in / etc that start with “g”, as well as g itself, you can enter:



 $ ls -d /etc/g* 
/etc/gconf /etc/ggi /etc/gimp /etc/gnome /etc/gnome-vfs-mime-magic /etc/gpm /etc/group /etc/group-

And now what happens if you specify a template that does not fit into any file system object. In the following example, we tried to loop through all the files in / usr / bin that start with “asdf” and end with “jkl”, potentially including asdfjkl :



$ ls -d /usr/bin/asdf*jkl
ls: /usr/bin/asdf*jkl: No such file or directory


This is what will happen. Usually, when we set a template, and if it contains one or more files in an implied file system, bash replaces our template with a space-separated list of all matching objects. However, when there are no matches with the pattern, bash leaves the passed argument with the jokers as is. So, then ls cannot find the file / usr / bin / asdf * jkl and gives us an error. The basic rule here is this: glob-templates are deployed only if they coincide with the objects of the file system. Otherwise, they remain untouched and are literally transferred to the program call.



Jokers syntax: * and?


So, we have already looked at how globbing works, but now it is worth considering the syntax of jokers. Special symbols are used as jokers:



* - matches zero or more characters. This means: "there can be anything, including nothing." Examples:



? - is equal to any one character. Examples:



Joker syntax: []


Does this joker look like ? but more accurate. To use it, put any characters you need inside [] . The resulting expression will satisfy any one of these characters. You can also use "-" to specify a range, or even a combination of ranges. Examples:



The [!] Construct is equivalent to the [] construct, except that instead of matching the characters inside the parentheses, it matches any character that is NOT listed between [! and]. Example:



Jokers warnings


Now a few cautions to be careful while using jokers. Because bash handles jokers (?, [,], And *) characters in a special way, you need to take special care when you write an argument for a command containing these characters. For example, if you want to create a file containing the string "[fo] *", then the following command may not give the desired result:



$ echo [fo]* > /tmp/mynewfile.txt

If the [fo] * template matches any files in the current working directory, you will find their names inside /tmp/mynewfile.txt , instead of the expected [fo] *. Decision? Well, one of them will be to enclose your characters in single quotes, which will tell bash not to make any jokers in them:



$ echo '[fo]*' > /tmp/mynewfile.txt

Using this approach, your new file will contain [fo] * literally, as expected. You can also use a backslash to tell bash that [,] and * should be interpreted literally, and not as jokers:



$ echo \[fo\]\* > /tmp/mynewfile.txt

Both approaches (single quotes and a backslash) work with the same effect. Since we started talking about processing a backslash, it would be nice to clarify now that in case you want to specify \ literally, you can either either take it in single quotes or type \\ (this will be converted to \).



Note:


Double quotes work just like single quotes, but still allow bash to do some limited processing. Therefore, single quotes are your best bet if you are really interested in literally passing text to a command. You can get more information about the discovery of jokers by typing man 7 glob. For more information about bash quotes, type man 1 bash and read the section called QUOTING. If your plans include passing the LPI exams, then consider this as your homework. =)



Summing up and links to other resources


Total


Congratulations; You got to the end of our review on the basics of Linux! I hope this helped you strengthen your fundamental knowledge of Linux. Topics that you learned here, including the basics of working in bash, basic Linux commands, links, and jokers - laid the foundation for our next tutorial on the basics of administration, in which we cover topics such as regular expressions, membership and access rights, user account management , and much more.



By continuing to dive into these tutorials, you will soon achieve LPIC Level 1 certification from the Linux Professional Institute. Speaking of this certification, if this is really what you are interested in, we recommend that you study the resources in the paragraph below, which have been carefully selected for the material covered in this guide.



Links


In the “Bash in Examples” series of articles, Daniel shows how to use bash software constructs to write your own bash scripts. This series (mainly 1 and 2 parts) will be an excellent preparation for LPIC Level 1:


Continued ...



About the authors


Daniel Robbins


Daniel Robbins is the founder of the Gentoo community and the creator of the Gentoo Linux operating system. Daniel lives in New Mexico with his wife, Mary, and two energetic daughters. He is also the founder and head of Funtoo , has written many technical articles for IBM developerWorks , Intel Developer Services and the C / C ++ Users Journal.



Chris Houser


Chris Hauser was a UNIX supporter since 1994 when he joined the team of administrators at Taylor University (Indiana, USA), where he received a bachelor's degree in computer science and mathematics. After that, he worked in many areas, including web applications, video editing, drivers for UNIX, and cryptographic protection. Currently working in Sentry Data Systems. Chris also contributed to many free projects, such as Gentoo Linux and Clojure, co-authored The Joy of Clojure .



Aron griffis


Airon Griffis lives in Boston, where he spent the last decade working with Hewlett-Packard on projects such as UNIX network drivers for Tru64, Linux security certification, Xen and KVM virtualization, and most recently, the HP ePrint platform. In his spare time, Airon prefers to ponder over the problems of programming while riding his bike, juggling bits, or cheering on the Boston Red Baseball team.

Source: https://habr.com/ru/post/99827/


All Articles