The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing

 Linux Wildcards

1.0 Wildcards
       "?" : Matches any single character
       "*" : Matches zero or more characters of any length
       "{PATTERN 1,PATTEN 2,..,PAATER n}" : Expands to multiple patterns
       "[ ]" : Specifies a character set to match against
       "[ - ]" : Specifies a range of characters to match
       "[^ ]" or "[!]" : Performs negation or inverse matching
       POSIX Character
1.1 rename : Renaming Multiple Files
ENG⇒中ENG⇒中
  1.0 Wildcards

Wildcard characters and pipes and redirections are the three magical powers of Linux commands, which can sublimate the power of text interface from shells to nuclear bombs, which is hard to compare with the graphical operation interface.
The main purpose of wildcards is to match file names. "Character" in wildcards is an English letter or number or symbol defined by ASCII code. For example, the English letter "A" "(ASCII=65) is a character. More than one character can form a string (String), for example, "XYZ" is a string.

Traditional file names are all encoded in ASCII code, so making good use of wildcards can simplify file operations, because the Linux shell itself can interpret wildcards, so almost any command related to files can be used with wildcards Characters, file names use wild characters to match called "globbing patterns" or "glob" for short, the common usage is as follows:
The wildcard character is versatile and useful, but it can also confuse users at times, particularly when dealing with hidden files (files that start with a dot). For example, there may be many hidden files in the "/tmp" directory, but if I want to clear out all the files and directories in this directory, using rm -fr * will not delete the hidden files. The main reason is that "*" represents anything, so rm -fr * will delete not only the hidden files but also the current directory represented by "." and the parent directory represented by "..". However, this is not what we expect, so we have to leave the files starting with a dot untouched.

Of course, other commands like ls * or cp * ~/ will also encounter similar issues. One solution is to use ".[^.]" to match hidden files starting with a dot while excluding "." and "..". For example, rm -fr .[^.]* indicates deleting all hidden files in the working directory.

In addition, it is not recommended to use very strange file names, such as the beginning of the file "-" or "#, @, %, \,?, |, {, }" and other strange characters in the file name, because it may cause let the wildcard fail (Many Linux distributions have this issue) or it is not easy to filter and match the desired files.Let's experiment!

Experiment
$ echo > abc ← Create a file with a length of three characters
$ ls ??? ← Verify
abc
$ echo > -12 ← Create a file "-12" (also three characters in length)
$ ls ??? ← Verify
ls: invalid option -- 2 ←??? Invalid
Try `ls --help' for more information.
$ ls *←List all files
ls: invalid option -- 2 Bizarre incident? wildcards completely fail.
$ rm -f ./-12 ←Kill this ghost file
$ ls ??? ←Verify
abc ←Normal


 1.1 rename - Renaming Multiple Files
In Linux, the rename command is a powerful tool for renaming multiple files simultaneously. By using the appropriate wildcard characters and specifying the renaming rules, you can efficiently rename a large number of files in just a single command.

Its syntax is rename OLD_NAME NEW_NANE PATTERN , where PATTERN is a pattern of wildcards, as long as it matches the pattern of wildcards, replace OLD_NAME with NEW_NANE.

It is easy to understand the usage with examples. For example, I took 50 photos in Keelung, but the default file name of the camera is IMAGE0001.jpg ~ IMAGE0050.jpg, but after a long time, I forgot that the photos were taken there, so I want to save all The file name was changed to keelung0001.jpg ~ keelung0050.jpg of the shooting location. However, using the mv command or using the Windows GUI to rename the files one by one is guaranteed to cause cramps, and it is much more convenient to use rename .

Example: (Assume that there are 50 files in the working directory, including IMAGE0001.jpg~IMAGE0050.jpg)
$ rename IMAGE keelung IMAGE* ←Rename the file IMAGE0001.jpg ~ IMAGE0050.jpg to
keelung0001.jpg ~ keelung0050.jpg


Example: (Continued from the previous example, there are 50 files from IMAGE0001~IMAGE0050 in the working directory)
$ rename IMAGE00 keelung00 IMAGE00[0-2]?.* ←Rename all files IMAGE0001.jpg ~IMAGE0029.jpg to keelung0001.jpg~keelung0029.jpg, and keep the rest unchanged

Example:
$ rename .config .cfg *.config ←Change the extension ".config" to ".cfg" in all working directories
$ rename - _ *-* ←Change the minus sign "-" in the file name of all files Change to underscore"_"

Not just files, rename can also be used to rename a large number of directories, which is actually very useful!
Example: (assuming that there are already directories catlog00~catlog99 in the home directory, a total of 100 directories)
$ rename ~/catlog ~/list ~/catlog?? ←in the home directory,Rename all directories from "catlog00"~"catlog99" rename to "list00"~"list99"

Of course, as long as you're happy, there's nothing wrong with using a sledgehammer to crack a nut. rename can also be used to rename individual files.

例:
$ rename abc wxyz * ← Rename "abc" to "wxyz"


^ back on top ^






[Note] LANG= can be used to set the locales, as in the following example:
$ LANG= ←To clear all configured locales (equivalent to "LANG=C" or "LANG=POSIX")
$ LANG=en_US.UTF-8←Set locales to "en_US.UTF-8"