Wildcards linux что это
How to Use Wildcards
A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasing the flexibility and efficiency of searches.
Wildcards are commonly used in shell commands in Linux and other Unix-like operating systems. A shell is a program that provides a text-only user interface and whose main function is to execute commands typed in by users and display their results.
Wildcards are also used in regular expressions and programming languages. Regular expressions are a pattern matching system that uses strings (i.e., sequences of characters) constructed according to pre-defined syntax rules to find desired strings in text.
The term wildcard or wild card was originally used in card games to describe a card that can be assigned any value that its holder desires. However, its usage has spread so that it is now used to describe an unknown or unpredictable factor in a variety of fields.
Star Wildcard
Three types of wildcards are used with Linux commands. The most frequently employed and usually the most useful is the star wildcard, which is the same as an asterisk (*). The star wildcard has the broadest meaning of any of the wildcards, as it can represent zero characters, all single characters or any string.
As an example, the file command provides information about any filesystem object (i.e., file, directory or link) that is provided to it as an argument (i.e., input). Because the star wildcard represents every string, it can be used as the argument for file to return information about every object in the specified directory. Thus, the following would display information about every object in the current directory (i.e., the directory in which the user is currently working):
If there are no matches, an error message is returned, such as *: can’t stat `*’ (No such file or directory).. In the case of this example, the only way that there would be no matches is if the directory were empty.
Wildcards can be combined with other characters to represent parts of strings. For example, to represent any filesystem object that has a .jpg filename extension, *.jpg would be used. Likewise, a* would represent all objects that begin with a lower case (i.e., small) letter a.
As another example, the following would tell the ls command (which is used to list files) to provide the names of all files in the current directory that have an .html or a .txt extension:
Likewise, the following would tell the rm command (which is used to remove files and directories) to delete all files in the current directory that have the string xxx in their name:
Question Mark Wildcard
The question mark (?) is used as a wildcard character in shell commands to represent exactly one character, which can be any single character. Thus, two question marks in succession would represent any two characters in succession, and three question marks in succession would represent any string consisting of three characters.
Thus, for example, the following would return data on all objects in the current directory whose names, inclusive of any extensions, are exactly three characters in length:
And the following would provide data on all objects whose names are one, two or three characters in length:
As is the case with the star wildcard, the question mark wildcard can be used in combination with other characters. For example, the following would provide information about all objects in the current directory that begin with the letter a and are five characters in length:
The question mark wildcard can also be used in combination with other wildcards when separated by some other character. For example, the following would return a list of all files in the current directory that have a three-character filename extension:
Square Brackets Wildcard
The third type of wildcard in shell commands is a pair of square brackets, which can represent any of the characters enclosed in the brackets. Thus, for example, the following would provide information about all objects in the current directory that have an x, y and/or z in them:
And the following would list all files that had an extension that begins with x, y or z:
The same results can be achieved by merely using the star and question mark wildcards. However, it is clearly more efficient to use the bracket wildcard.
When a hyphen is used between two characters in the square brackets wildcard, it indicates a range inclusive of those two characters. For example, the following would provide information about all of the objects in the current directory that begin with any letter from a through f:
And the following would provide information about every object in the current directory whose name includes at least one numeral:
The use of the square brackets to indicate a range can be combined with its use to indicate a list. Thus, for example, the following would provide information about all filesystem objects whose names begin with any letter from a through c or begin with s or t:
Likewise, multiple sets of ranges can be specified. Thus, for instance, the following would return information about all objects whose names begin with the first three or the final three lower case letters of the alphabet:
Sometimes it can be useful to have a succession of square bracket wildcards. For example, the following would display all filenames in the current directory that consist of jones followed by a three-digit number:
Created July 17, 2006.
Copyright © 2006 The Linux Information Project. All Rights Reserved.
Команда cp: правильное копирование папок с файлами в *nix
В этой статье будут раскрыты некоторые неочевидные вещи связанные с использованием wildcards при копировании, неоднозначное поведение команды cp при копировании, а также способы позволяющие корректно копировать огромное количество файлов без пропусков и вылетов.
Допустим нам нужно скопировать всё из папки /source в папку /target.
Первое, что приходит на ум это:
Сразу исправим эту команду на:
После копирования мы обнаружим, что скопировались не все файлы — были проигнорированы файлы начинающиеся с точки типа:
.profile
.local
.mc
и тому подобные.
Почему же так произошло?
Потому что wildcards обрабатывает shell ( bash в типовом случае). По умолчанию bash проигнорирует все файлы начинающиеся с точек, так как трактует их как скрытые. Чтобы избежать такого поведения нам придётся изменить поведение bash с помощью команды:
Чтобы это изменение поведения сохранилось после перезагрузки, можно сделать файл wildcard.sh c этой командой в папке /etc/profile.d (возможно в вашем дистрибутиве иная папка).
Однако, если в папке тысячи файлов и больше, то от подхода с использованием wildcards стоит отказаться вовсе. Дело в том, что bash разворачивает wildcards в очень длинную командную строку наподобие:
На длину командной строки есть ограничение, которое мы можем узнать используя команду:
Получим максимальную длину командной строки в байтах:
Получим что-то типа:
Итак, давайте будем обходиться вовсе без wildcards.
Давайте просто напишем
Однако, если папка target существует, то файлы будут скопированы в папку /target/source.
Не всегда мы можем удалить заранее папку /target, так как в ней могут быть нужные нам файлы и наша цель, допустим, дополнить файлы в /target файлами из /source.
Если бы папки источника и приёмника назывались одинаково, например, мы копировали бы из /source в /home/source, то можно было бы использовать команду:
И после копирования файлы в /home/source оказались бы дополненными файлами из /source.
Такая вот логическая задачка: мы можем дополнить файлы в директории-приёмнике, если папки называются одинаково, но если они отличаются, то папка-исходник будет помещена внутрь приёмника. Как скопировать файлы из /source в /target с помощью cp без wildcards?
Чтобы обойти это вредное ограничение мы используем неочевидное решение:
Те кто хорошо знаком с DOS и Linux уже всё поняли: внутри каждой папки есть 2 невидимые папки «.» и «..», являющиеся псевдопапками-ссылками на текущую и вышестоящие директории.
Поведение этой команды однозначно. Всё отработает без ошибок вне зависимости от того миллион у вас файлов или их нет вовсе.
Выводы
Если нужно скопировать все файлы из одной папки в другую, не используем wildcards, вместо них лучше использовать cp в сочетании с точкой в конце папки-источника. Это скопирует все файлы, включая скрытые и не завалится при миллионах файлов или полном отсутствии файлов.
Послесловие
vmspike предложил аналогичный по результату вариант команды:
ВНИМАНИЕ: регистр буквы T имеет значение. Если перепутать, то получите полную белиберду: направление копирования поменяется.
Благодарности:
Wildcards!
Taming the file system.
Introduction
In the section on File Manipulation we learnt about a few commands to do interesting things. The problem was that they all operated on a single file at a time, not very efficient. Now I’m going to introduce a means to play about with a set of files at once.
So what are they?
Wildcards are a set of building blocks that allow you to create a pattern defining a set of files or directories. As you would remember, whenever we refer to a file or directory on the command line we are actually referring to a path. Whenever we refer to a path we may also use wildcards in that path to turn it into a set of files or directories.
Here is the basic set of wildcards:
As a basic first example we will introduce the *. In the example below we will list every entry beginning with a b.
Under the Hood
The mechanism here is actually kinda interesting. On first glance you may assume that the command above ( ls ) receives the argument b* then proceeds to translate that into the required matches. It is actually bash (The program that provides the command line interface) that does the translation for us. When we offer it this command it sees that we have used wildcards and so, before running the command ( in this case ls ) it replaces the pattern with every file or directory (ie path) that matches that pattern. We issue the command:
Then the system translates this into:
and then executes the program. The program never sees the wildcards and has no idea that we used them. This is funky as it means we can use them on the command line whenever we want. We are not limited to only certain programs or situations.
Some more examples
Some more examples to illustrate their behaviour. For all the examples below, assume we are in the directory linuxtutorialwork and that it contains the files as listed above. Also note that I’m using ls in these examples simply because it is a convenient way to illustrate their usage. Wildcards may be used with any command.
Every file with an extension of txt at the end. In this example we have used an absolute path. Wildcards work just the same if the path is absolute or relative.
Now let’s introduce the ? operator. In this example we are looking for each file whose second letter is i. As you can see, the pattern can be built up using several wildcards.
Or how about every file with a three letter extension. Note that video.mpeg is not matched as the path name must match the given pattern exactly.
And finally the range operator ( [ ] ). Unlike the previous 2 wildcards which specified any character, the range operator allows you to limit to a subset of characters. In this example we are looking for every file whose name either begins with a s or v.
With ranges we may also include a set by using a hyphen. So for example if we wanted to find every file whose name includes a digit in it we could do the following:
We may also reverse a range using the caret ( ^ ) which means look for any character which is not one of the following.
Some Real World Examples
The examples above illustrate how the wildcards work but you may be wondering what use they actually are. People use them everywhere and as you progress I’m sure you’ll find many ways in which you can use them to make your life easier. Here are a few examples to give you a taste of what is possible. Remember, these are just a small sample of what is possible, and they can be used whenever you specify a path on the command line. With a little creative thinking you’ll find they can be used in all manner of situations.
Find the file type of every file in a directory.
Move all files of type either jpg or png (image files) into another directory.
Summary
No new commands introduced in this section.
Activities
Let’s play with some patterns.
Bash Wildcard
Wildcard Characters
The three main wildcard characters are,
Asterisk (*) is used to search for a particular character(s) for zero or more times. Question mark (?) is used to search for a fixed number of characters where each question mark (?) indicates each character. Square brackets are used to match with the characters of a defined range or a group of characters. The uses of these characters are shown in the next part of this tutorial.
Use of Asterisk (*)
Asterisk (*) can be used in various ways with shell commands for searching files. Different use of the asterisk (*) are shown in the following examples.
Example – 1: Searching Specific File with Filename and ‘*’
‘ls’ command is used to find out the list of files and folders of the current directory. ‘ls a*’ command will search and print all filenames of the current directory that starts with the character, ‘a’.
Output:
According to the following output, 12 files exist in the current directory that starts with the character, ‘a’.
Example – 2: Searching File with Particular Extension and ‘*’
You can search any file by using the asterisk (*) and the file extension. If you want to search all files with the ‘.txt’ extension from the current directory then run the following command from the terminal. Here, the filename can be any character(s) and any number of characters.
You can also search files of different extensions by using the asterisk (*). The following command will search any files with extension ‘.sh’ or ‘.txt’
Output:
The output shows the list of all existing files of the current directory with the extension ‘.sh’ and ‘.txt.
Example – 3: Removing File by Partial Match and ‘*’
You can use the asterisk (*) for matching any filename partially. The following command will remove the file which contains the ‘test’ word in any part of the filename.
Output:
The following output shows that two files exist in the current directory that contains the word ‘test’ and these files have been removed after executing the ‘rm’ command.
Use of Question Mark (?)
When you know the exact numbers of characters that you want to search then the question mark (?) wildcard can be used. The following examples show the different use of question mark (?) wildcard.
Example – 1: Searching File with Filename and ‘?’
Suppose, the file extension, the total number of characters of a file, and some characters of the file are known, then you can use this wildcard to search the file. The first command will search those files which have the extension ‘.txt’. The second command will search those files which are four characters long, the last character is ‘t’ and the extension of the file is ‘.txt’.
Output:
The output shows that 7 files exist in the current directory with the ‘.txt’ extension and one text file exists that is 4 characters long and the last character is ‘t’.
Suppose, you know the filename and the total number of characters of the file extension then you can use the question mark (?) wildcard to search the file. The following command will search the file with filename ‘hello’ and the extension is three characters long.
Output:
According to the output, two files exist in the current directory that has the name, ‘hello’ and the extension is three characters long.
Use of Square Brackets ([])
Different ranges of characters or group of characters can be used within square brackets ([]) for searching files based on the range.
The following command will search any file whose name contains any character within ‘a-p’ and any digit within ‘0-5’ and the file extension can be any character.
Output:
The following output shows the list of all files that match the pattern used in the command.
Example-2: Search File Starts with a Particular Character and is Followed by Another Character
In this example, the second command will search the filenames starting with any of these characters [afgh] followed by the character ‘o’ with any extension.
Output:
The following output shows the list of all files that match the pattern used in the command.
Example-3: Search Filenames with the Prefix Value
The following command will match those files whose name starts with ‘fn’ and is followed by a number within 0 to 5.
The following command will match those files that contain the name, ‘hello’ and the extension contains any character from t to z.
Output:
The following output shows the list of all files that match with the pattern used in the commands.
The character class can be used in the third brackets to match the filename or the particular string in the script. Different classes that can be used in the pattern has described below.
Class name | Purpose |
---|---|
[:alpha:] | It is used to match with any uppercase and lowercase letter and equivalent to [a-zA-Z]. |
[:digit:] | It is used to match with any digits and is equivalent to 8. |
[:alnum:] | It is used to match any alphabet and digit. It is equivalent to [a-zA-Z]. |
[:upper:] | It is used to match with uppercase latter only and equivalent to [A-Z] |
[:lower:] | It is used to match with uppercase latter only and equivalent to [a-z] |
[:blank:] | It is used to match with space and tab. |
[:print:] | It is used to match with printable characters. |
[:cntrl:] | It is used to match with non-printable characters. |
[:space:] | It is used to match with while-space. |
[:xdigit:] | It is used to match with hexadecimal digits. |
[:ascii:] | It is used to match with ASCII characters. |
[:word:] | It is used to alphanumeric characters including underscore(_). |
The uses of four mostly used classes have explained in the following four examples.
Example-1: Use of [:alpha:] in Bash Script
The following script will check that the input taken from the user contains alphabetic characters only.
Output:
The following output shows the message for invalid and valid input.
Example-2: Use of [:digit:] in Bash Script
The following script will check that the input taken from the user contains numeric characters only.
Output:
The following output shows the message for invalid and valid input.
Example-3: Use of [:alnum:] in Bash Script
The following script will check that the input taken from the user contains alphanumeric characters only.
^ [ [ :alnum: ] ] ] ] ; then
echo «Address is valid.»
else
echo «Address is invalid.»
fi
Output:
The following output shows the message for invalid and valid input.
Example-4: Use of [:upper:] in Bash Script
The following script will check that the input taken from the user contains capital letters only.
^ [ [ :upper: ] ] ] ] ; then
echo «Code is correct.»
else
echo «Code can contain uppercase only.»
fi
Output:
The following output shows the message for invalid and valid input.
Conclusion
The basic uses of wildcard characters for searching files in the terminal and validating data using bash script have been shown in this tutorial. I hope the examples shown here will help the Linux users to write regular expression patterns properly to match any content for different purposes.
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.
Wildcards in Unix & macOS: Why Most People Only Think They Understand Wildcards
Published date August 12, 2018
Last modified date May 9, 2019
A very useful concept on the macOS (and Linux) command line is that of wildcards. Many casual command line users will have a passing familiarity with them but most people don’t really understand how they actually work.
Free Video Course!
The content of this post is now covered in some depth on my Udemy course Learn to be a macOS Command Line Ninja. It’s a three-hour course covering everything you need to know to make you a command line pro – and it’s available free for a limited time only!
What’s a Wildcard?
Many, many years ago I remember a tutor in an introductory CS lecture telling the whole class that wildcards were nothing to do with cards. That’s patently incorrect, but probably forgivable in those pre-Google days (he didn’t look like a hardcore card player). Because just like the Joker or “wild card” in a pack of cards, a wildcard is a character that can act as a stand-in for any other character (or characters) in a file path.
Suppose you have a directory with a lot of similarly-named files in it:-
Multi-Character Wildcard: *
Single-character wildcards are quite limiting, and in reality they’re not used terribly often. To see why, imagine what would happen when the number of report files in the example directory reached 10: the filter would no longer match because it only matches a single character, and now the number is two digits long.
Thankfully there’s another way: the * (asterisk) wildcard. This is the workhorse of wildcards, and is the one most people know about. A version of it exists in Windows, too, but it’s nowhere near as powerful. Let’s see why…
Filtering the report files is easy with the asterisk wildcard:-
In fact, it’s easier than that:-
The magic thing about the asterisk is it tries to match any number of characters (including none). So in the final example above, anything that starts with the letter r is enough to match – the asterisk matches all the remaining characters. A file called raptor999 would also match. The key thing to remember when using it is to ask: what is the minimum pattern that would uniquely identify what I’m looking for?
Say you wanted to make a copy of all the report files to a new subdirectory. First create a new empty directory called copies:-
Then use the wildcard pattern r* as an argument to cp as follows:-
Confirm it has copied the files to the subdirectory:-
Why People Only Think They Understand Wildcards
This can be demonstrated using the lowly echo command. echo simply takes the arguments you pass to it and sends them to the Terminal output:-
But if you pass it a wildcard, something else happens:-
So, what’s going on here? To understand, it’s crucial to remember that the wildcard is interpreted by the shell itself, before it is sent to the command.
The shell (not the command) expands the wildcard pattern to match as many files as it can, from the current working directory. So it would expand the above command to be:-
Now the output makes sense. echo is just echoing the filenames it received from the shell. Because this crucial step is never seen by the user, the exact behaviour of wildcard expansion is often misunderstood.
Pro Tip: Using echo to Preview
You can also use * on its own to match all files in a directory:-
Again, this might not seem any different from using ls on its own, but the crucial thing is that the asterisk wildcard is expanded by the shell into a set of paths, so for example, if you wanted to move all the files in the directory to another directory, or delete them, or whatever, you can: * will expand to the list of files in the current working directory, and you can pass than list to any command.
A veteran programmer, evolved from the primordial soup of 1980s 8-bit game development. I started coding in hex because I couldn’t afford an assembler. Later, my software helped drill the Channel Tunnel, and I worked on some of the earliest digital mobile phones. I was making mobile apps before they were a thing, and I still am.