Article Title: using wildcards in Unix improves operation efficiency. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
A Unix operating system has a special set of characters, which are called wildcards. Using these wildcards can improve the efficiency of some commands. For example, a folder in the system contains files such as test, test1, test2, test3, log, and buildlog. But now the system engineer only wants to operate the files starting with test, for example, to copy them to another folder. If no wildcard is available, the system engineer has to include all the file names after the cp command. When there are many files, this operation is obviously a waste of time. Now with the help of wildcards, this work will become very simple.
In Unix, The metacharacters used to construct the file name matching mode are called wildcards. In Unix operating systems, the wildcard feature is relatively powerful. So what functions can our System Engineers use wildcards to implement? What should I pay attention to when using wildcards? I will elaborate on this in the next article.
Wildcard * and wildcard? Is the two most frequently used wildcards by System Engineers. This wildcard is required for remote backup of databases. For example, I recently deployed an Oracle database system on the Unix operating system, and then used the expdb tool to export the database for backup. However, when using this tool to back up the database, it uses more than one dump file. For example, if you set the size of the dump file to 1000 MB during database deployment. When the database size is MB, there may be five final dumped files. For example, if I set the name of the dump file to backup _ % U. dmp, the generated dump file will be named backup_01.dmp and backup_02.dmp. As the database capacity increases, the number of dumped files also increases. Therefore, when you need to write a script to copy the local dumping file of the backup to another place, you cannot use a fixed file name. However, you need to rely on this wildcard.
For example, we can represent these dump files as *, backup *, backup _??. Bmp and so on. Although they are able to complete the above tasks at the end, they are still very different. For example, * indicates all file names under a directory. For example, in the above requirement, in addition to these dumping files, there may also be log files backed up each time. If you use a single * wildcard, not only will the dump file be copied to other places, but other files in the directory, such as log files, be copied at the same time. Obviously, if the directory contains many files that are not related to the dump file, and the capacity is relatively large, it will take a lot of time during the copy process. This is not worth it. Therefore, you need to use more precise wildcards.
So what does backup * mean? If the * wildcard character is added to the string, the first few characters of the file name must be backup, and any character after the file name is used. The above requirement is used as an example. As long as the first few characters in the file name are backup, all related files will be copied. However, this also has a problem. If there is a backup log file named backup.txt in the current directory, it will be copied at the same time. Now the system engineer only needs to copy the dump file, which is obviously contrary to our desire. Because we only need to copy the dump file.
To precisely define a type of file, we need to use? Wildcard. For example, to define a qualified dump file, you only need to use backup _??. Dmp. It indicates that the name of the file must start with backup _, followed by two characters, and finally ended with a. dmp extension. The file must comply with this rule. Otherwise, the file will not be copied. Obviously, based on the above requirement, only the files that meet this condition are dumped.
In the above case, we can see the wildcard * and the wildcard? There are essential differences. Wildcard * represents any character, including space characters. If a single * number is used, it indicates all the files in the current directory. If it is followed by a string, it indicates the files starting with this string. If you use wildcards? It indicates a single character.
2. Uncommon wildcard characters.
In addition to the above two wildcards, there are some other wildcards in the Unix operating system. Although these Wildcards are not very useful, they can still play a very important role in some special occasions. For example, the wildcard [abc] indicates any of the three characters. For example, the wildcard [a-z] indicates all lowercase English characters. You can also use it when necessary! Operators, such [! Abc], which indicates any character except a, B, and c. These wildcards are mainly used for more precise restrictions. Sometimes strict control in script programs can improve the performance.
3. Precautions for using wildcards.
Although using wildcards can improve our work efficiency. However, wildcards are different from other characters. If they are not used properly, they will be difficult to use. Especially when used with some destructive commands such as Delete and rename, pay more attention to some taboos during the use of wildcards. Specifically, the author believes that the taboos for using wildcards in Unix operating systems mainly include the following aspects.
First, pay attention to the influence of space characters. For example, what are the differences between rm *. txt and rm *. txt? In terms of form, there is no big difference at once. But take a closer look, we can see that the second command has a space in the middle of the wildcard * and the extended name .txt. Although this is only a small difference, Unix operating systems are very different in processing. System engineers or users wanted to use rm *. txt name to delete all txt files in the current directory. However, rm * is used *. txt command (it may be that an extra space character is followed by the wildcard * during the input process ). In this case, the operating system deletes all files in the current folder. That is to say, if there is a space behind the wildcard *, the text after the space will be ignored, indicating all files in the current directory. Therefore, when using the * wildcard (the same for other wildcards), be sure to note the effect of this special character. Especially when using these rm commands, it is best to execute them in interactive mode. This ensures that the operated file is exactly the target file you want to operate on. This prevents irreparable losses such as data deletion and information modification due to wrong commands.
[1] [2] Next page