Step-by-step guide for specifying file names in Linux with patterns


How it works

The shell provides a way to construct patterns, called file name expansions that specify a group of files. You can use them when specifying file and directory names as arguments to any tool or application.

The following lists the various file expansion characters and their meaning.

  • *
    • The asterisk matches a series of zero or more characters, and is some- times called the “wildcard” character. For example, * alone matches all file names, a* matches all file names that consist of an ‘a’ character followed by zero or more characters, and a*b matches all file names that begin with an ‘a’ character and end with a ‘b’ character, with any (or no) characters in between.
  • ?
    • The question mark matches exactly one character. Therefore, ? alone matches all file names with exactly one character, ?? matches all file names with exactly two characters, and a? matches any file name that begins with an ‘a’ character and has exactly one character following it.
  • [list]
    • Square brackets match one character in list. For example, [ab] matches exactly two file names: ‘a’ and ‘b’. The pattern c[io] matches ‘ci’ and ‘co’, but no other file names.
  • ˜
    • The tilde character expands to  your  home  directory.  For  example, if your username is joe and therefore your home directory is ‘/home/joe’, then ‘˜’ expands to ‘/home/joe’. You can follow the tilde with a path to specify a file in your home directory—for example, ‘˜/work’ expands to ‘/home/joe/work’.

Brackets also have special meaning when used in conjunction with other characters, as described by the following.

  • –  A
    • hyphen as part of a bracketed  list  denotes  a range of characters  to match—so [a-m] matches any of the lowercase letters from ‘a’ through ‘m’.  To  match a literal hyphen character,  use it as the first or last character in the list. For example, a[-b]c matches the files ‘a-c’ and ‘abc’.
  • !  
    • Put an exclamation point at the beginning of a bracketed list to match all characters except those listed. For example, a[!b]c matches all files that begin with an ‘a’ character, end with a ‘c’ character, and have any one character, except a ‘b’ character, in between; it matches ‘aac’, ‘a-c’, ‘adc’, and so on.

 

You can combine these special expansion characters in any combination, and you can specify more than one pattern as multiple arguments. The following examples show file expansion in action using some Linux commands.

  • To list all files in the ‘/usr/bin’ directory that have the text ‘tex’ anywhere in their name, type:
$ ls /usr/bin/*tex*
  • To copy all files whose names end with ‘.txt’ to the ‘doc’ subdirectory, type:
$ cp *.txt doc
  • To output a verbose listing of all files whose names end with either a ‘.txt’ or ‘.text’ extension, sorting the list so that newer files are listed first, type:
$ ls -lt *.txt *.text
  • To move all files in the ‘/usr/tmp’ directory whose names consist of the text ‘song’ followed by an integer from 0 to 9 and a ‘.cdda’ extension, placing them in a directory ‘music’ in your home directory, type:
$ mv  /usr/tmp/song[0-9].cdda ˜/music
  • To remove all files in the current working directory that begin with a hyphen and have the text ‘out’ somewhere else in their file name, type:
$ rm -- -*out*
  • To concatenate all files whose names consist of an ‘a’ character followed by two or more characters, type:
$ cat a??*

Resources- Self-Paced Linux Courses

If you like to learn more about Linux, taking the following courses is highly recommended:

 

Resources- Free Courses

Here is the list of our 9 free self-paced courses that are highly recommended:

Resources- Live Linux Courses

If you like to learn more about Linux, take the following live Linux classes is highly recommended:

 

Resources- Tutorials

If you like to learn more about Linux, reading the following articles and tutorials is highly recommended: