Hello, everyone.
In this Blog, we are going to talk about a very powerful concept and tool in our Linux command line, which is the wildcards.
wildcards are symbols that represent one or more characters in Linux. They allow you to perform powerful operations on groups of files without specifying each file individually. The two most common wildcards are the asterisk (*) and the question mark (?).
Asterisk (*): It matches any characters, any number of characters, or any characters. This wildcard is your go-to for working with multiple files at once.
Question Mark (?): This symbol represents or matches a single character. It's like having a wildcard detective that can stand in for any character.
Let's See Them in Action! π
I am on my desktop as you can see, I have many files, and I also have a directory one, dir1 here. This directory is initially empty.
So far, we learned how to copy files into the directories, right?
But if I told you now, to copy all these files here to directory one, which is on the desktop?
I'll begin with File1, File2, and File3β¦..even until you reach File10, And then you specify your destination directory.
And you are right, you can do it this way.
-
But have you noticed one thing?
It will take you forever to include all these file names without any error and then copy them to directory one.
Instead, we can use the asterisk (*) So if I said cp * dir1. this will simply copy everything that I have here to dir1, So let's execute this command here.
$cp * dir1
As you can see, every file on my desktop was copied to my dir1.
So, if I go to my dir1 now, and I want to remove all my files, I just have to say
$rm *
So rm * will remove all the files that I have here.
if I want to copy test1.txt, test2.txt, test3.txt to my dir1.
$ cp test* dir1
here I'm saying, that I want to copy all the files that begin with the name test or with the characters file, and I do not care about what follows the file.
So if I execute this command, I can go to my dir1, As you can see, I am done.
-
Do you have an idea of how the asterisk wildcard works? For more examples, let's see here.
If you want to copy all the files that end with the .txt. , So here I have .txt, .txt, and .txt.
$ cp *.txt dir1
-
I don't care what is the name of the file, but all I care about is that it ends with .txt and copy it to my dir1.
let's go to my directory once again and delete everything that I have, clear my screen. Okay. Right now, I don't have anything on my dir1.
You want to copy all the files that begin with the letter, the lowercase letter t.
$cp t* dir1
-
one more example of the asterisk here. So, let's say that you want to copy all the files that begin with the Uppercase letter A and you don't care how long the file name is, but you want to make sure that it ends with a g.
$cp A*g dir1
-
Now we move our attention to the second wildcard, which is the question mark (?).
So, the question mark is different and it's very useful when you have a certain number of characters in mind.
What do I mean by this? Let's say, for example.
"I want to copy all the files that begin with the upper...β, Okay, let's see.
I want to copy all the files that begin with the name test, and I don't care what is the next character here, but I want it to be .txt directory one
Unlike the star or the asterisk, which represents any character, the question mark represents a single character. So here what I'm saying is that
I want to copy all the files that have file, that begins with the name file, but I do not care what the other character that follows here, and then .txt.
So, as you know, this will include test1.txt,test2.txt, and test3.txt.
-
I want to include another file here, which is called test45.txt, I will tell you why I am including this file now.
So if I said cp file.txt...Okay, can you expect what this command will execute?
Okay, I'll tell you right now We will show. So, if you go to dir1, we can only see three files,
which is test1, test2, and test3.
So you might be wondering why it did not include test45, for example, .txt Because it's different. The question mark is different compared to the asterisk.
Because I only included one character here, which is a question mark. So this question mark can represent only one character, which is here, two, three, or one.
But here I have two characters, 45. So to represent these two characters,
I have to say $cp test**.txt dir1, And this will copy test45.txt, as you can see here.
-
The question mark it's different compared to the asterisk. So let's do this one more time to make sure we understand the difference. So I'll delete everything that I have in my dir1.
And so, if he says test*.txt, this will include all the guys,
test1, test2, test3, and test45.
Because star here, there is no restriction. On the number of characters here. So here it will take all four files as you can see.
-
However, if I delete all the guides here and then I put a question mark instead of an asterisk, it will only take the guy's test1, test2, test3
because a question mark only matches or represents a single character.
-
I hope you guys understand now the difference between a question mark and an asterisk.
Why Wildcards Are Essential π
Efficiency: Using wildcards simplifies your commands and makes file manipulation more efficient. You can perform complex operations with just a few characters.
Flexibility: Wildcards provide flexibility when dealing with various files or patterns. You don't need to specify each file individually.
Productivity: They save time and reduce the risk of errors. Wildcards are a productivity boost for Linux users.
Stay tuned as we explore how these wildcards work their magic! πππ
Conclusion π
Now that you've seen wildcards in action, you're equipped with a valuable tool for navigating and manipulating files in Linux. Whether you're a beginner or an experienced Linux user, understanding wildcards will make your command-line tasks more manageable and efficient. So, go ahead and put your newfound knowledge to useβit's time to master the art of wildcards! ππ‘
If this post was helpful, please do follow and click the like button below to show your support π
Thank you for readingπ
Sprasad π»β¨