Linux Wildcards - Standard
"Unix is simple. It just takes a genius to understand its simplicity." – Dennis Ritchie
One of the advantage of Unix or *nix operating system is its simplicity. Beginners might find it hard to agree with me. But once you have familiarized with the OS and learned how to make use of it, you will discover the simplicity offered by Unix / *nix operating systems. Wildcards are one such concept which make Unix / *nix simple
Wildcards are symbol used to replace or represent one or more characters. i.e. wildcard expressions can be used by commands to perform action on multiple files at a time.Wildcards can be used in many ways and helps you to keep things simple and easy.
There are mainly 2 ways by which a wild cards can be used. One is the Standard (globbing) wildcard, usually used by shell and the second is regular expression (regex) wildcard used mainly with text searching and manpulation commands like grep.
In this post we are going to see Standard wildcards. The most used standard wildcards are as follows
Before going into the details, displayed below is the ls -l output of a my home directory. Rest of the post will be explained from within this directory
For example to long list all .txt files, enter
One of the advantage of Unix or *nix operating system is its simplicity. Beginners might find it hard to agree with me. But once you have familiarized with the OS and learned how to make use of it, you will discover the simplicity offered by Unix / *nix operating systems. Wildcards are one such concept which make Unix / *nix simple
Wildcards are symbol used to replace or represent one or more characters. i.e. wildcard expressions can be used by commands to perform action on multiple files at a time.Wildcards can be used in many ways and helps you to keep things simple and easy.
There are mainly 2 ways by which a wild cards can be used. One is the Standard (globbing) wildcard, usually used by shell and the second is regular expression (regex) wildcard used mainly with text searching and manpulation commands like grep.
In this post we are going to see Standard wildcards. The most used standard wildcards are as follows
- * (Asterisk)
- ? (Question Mark)
- { } (Curly Bracket)
- [ ] (Square Bracket)
- [!] (Exclamation within Square Bracket)
- \ (Back slash)
Before going into the details, displayed below is the ls -l output of a my home directory. Rest of the post will be explained from within this directory
* (Asterisk)
One of the most commonly used wildcard. This can represent any number of characters, numerals or special characters. * can be used in cases like listing all files with .txt files in a directory
For example to long list all .txt files, enter
ls -l *.txt
Sample Output:
-rw-rw-r-- 1 calypso calypso 2 Feb 14 15:21 demo.txt
-rw-rw-r-- 1 calypso calypso 10 Feb 17 17:06 file1.txt
-rw-rw-r-- 1 calypso calypso 10 Feb 17 17:06 file2.txt
-rw-rw-r-- 1 calypso calypso 10 Feb 17 17:06 file3.txt
-rw-rw-r-- 1 calypso calypso 10 Feb 17 17:06 file4.txt
-rw-rw-r-- 1 calypso calypso 10 Feb 17 17:06 file5.txt
-rw-r--r--. 1 calypso calypso 4244 Sep 26 2011 README.txt
-rw-rw-r-- 1 calypso calypso 10 Feb 17 17:06 file1.txt
-rw-rw-r-- 1 calypso calypso 10 Feb 17 17:06 file2.txt
-rw-rw-r-- 1 calypso calypso 10 Feb 17 17:06 file3.txt
-rw-rw-r-- 1 calypso calypso 10 Feb 17 17:06 file4.txt
-rw-rw-r-- 1 calypso calypso 10 Feb 17 17:06 file5.txt
-rw-r--r--. 1 calypso calypso 4244 Sep 26 2011 README.txt
Or to list files starting with httpd, enter
ls httpd*
Sample Output:
httpd-2.2.3-53.el5_7.3.i386.rpm httpd-devel-2.2.3-53.el5_7.3.i386.rpm httpd-manual-2.2.3-53.el5_7.3.i386.rpm
? (Question Mark)
For example to copy 5 files named file1.txt, file2.txt, file3.txt, file4.txt and file5.txt from current directory to Downloads directory also located in my home directory enter
cp file?.txt Downloads/
Similarly, two question marks in succession represents two character in succession, three question marks in succession would represent three characters in succession and so on. i.e
ls -l ????
will list all files with exactly four character length
{ } (Curly Bracket)
Curly Brackets are used to represent multiple terms separated by commas and each term can be a character, group of character, or a wild cardFor example to list only file1.txt and file2.txt, enter
ls file{1,2}.txt
Similarly to remove file1.txt, file4.txt and file5.txt, enter:
rm file{1,4,5}.txt
[ ] (Square Bracket)
For example to list file1.txt, file2.txt, file3.txt, file4.txt and file5.txt, enter
ls file[1-5].txt
[!] (Exclamation within Square Bracket)
For example
ls file[!2].txt
will display all file?.txt files except file2.txtSample Output
file1.txt file3.txt file4.txt file5.txt
\ (Back slash)
Back slash or the escape character is used to protect special character. Consider you have copied a file named 'My File.txt' created from a windows machine to your linux box and you want to view the contents of the file using cat command
cat My File.txt
Sample Output
cat: My: No such file or directory
cat: File.txt: No such file or directory
cat: File.txt: No such file or directory
In the above example, cat command thinks My File.txt are two different files, My and File.txt, since there is a space in between the file name. The correct method is to use '\' before the space
These are the different Simple Wildcards used in Unix/*nix systems. Hope you got an idea on wild cards. Like I said before wildcards are very handy and their usage can be improvised based on your requirement. The regex wildcards will be explained later.
Comments
Post a Comment