Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1
directory   ending  in  .txt,   you could   use this:

Click here to view code image
matthew@seymour:~$ ls *.txt


?—Matches   a   single  character.  For example,    to  find    all files   in  the current
directory ending in the extension .d?c (where ? could be 0–9, a–z, or
A–Z), you could use the following:

Click here to view code image
matthew@seymour:~$ ls *.d?c


[xxx]   or [x-x]—Matches    a   range   of  characters. For example,    to  list    all
files in a directory with names containing numbers, you could use this:

Click here to view code image
matthew@seymour:~$ ls [0-9]


\x—Matches  or  escapes a   character   such    as  ?   or  a   tab character.  For
example, to create a file with a name containing a question mark, you
could use the following:

Click here to view code image
matthew~$ touch foo\?


Note that the shell might not interpret some characters or regular expressions
in the same manner as a Linux command, and mixing wildcards and regular
expressions in shell scripts can lead to problems unless you’re careful. For
example, finding patterns in text is best left to regular expressions used with
commands such as grep; simple wildcards should be used for filtering or
matching filenames on the command line. And although both Linux command
expressions and shell scripts can recognize the backslash as an escape
character in patterns, the dollar sign ($) has two wildly different meanings
(single-character pattern matching in expressions and variable assignment in
scripts).


CAUTION
Make sure you read your command carefully when using wildcards; an all-
too-common error is to type something like rm -rf * .txt with a space
between the * and .txt. By the time you wonder why the command is
taking so long, Bash will already have deleted most of your files. The
problem is that it will treat the * and the .txt separately. * will match
everything, so Bash will delete all your files.
Free download pdf