Linux Files - Wildcards

Discuss everything about Linux here!

Moderator: Community Moderator

Post Reply
User avatar
MrNiitriiX
Premium Uploader
Premium Uploader
Posts: 2197
Joined: 19 Apr 2010, 14:52
Location: between space and time
Has thanked: 24 times
Been thanked: 164 times

Linux Files - Wildcards

Post by MrNiitriiX »

How Do I Use Wildcards to Select Files?

Using Wildcards

Wildcards come in quite handy when you want to operate on more than one file at a time. You can use wildcards with any of the commands in this section to list, copy, rename, or delete groups of files or directories.

The asterisk (*) character will match any number of characters in a file name, so consider these examples in light of the animals directory we used earlier.

$ ls -l c*
-rw-r--r-- 1 hermie users 45090 Mar 23 23:17 cat_info
-rw-r--r-- 1 hermie users 64183 Feb 14 22:07 cow_info
$ mv *inf* ../docs
$ rm -f *


The first command lists only those files that begin with the letter "c." The second will move files with the term inf anywhere in the name to another directory. The third command will delete all (nonhidden) files from the current directory.

Note: The expansion of the wildcard is done by the shell, not the command you are running. This means that when you enter the command
rm -f *
the shell expands the wildcard by replacing it with all files that match and actually ends up executing this command:
rm -f cat_info cow_info dog_info pig_info slugs zippity

This might seem a bit arcane, but it's actually important. Since the shell handles the wildcards in Linux, you don't have to worry about which commands accept wildcards and which don't. This is quite different from DOS, where only certain commands work with wildcards.

Here's a little pop quiz: What will be left in the animals directory if you execute the rm command in the preceding example? Try it and then use the ls -al command--you may be surprised at the answer.

Leason 1: The Linux File System
Leason 2: Linux File Names
Leason 3: Linux Directories
Leason 4: Directory Terminology
Leason 5: Navigating the File System
Leason 6: Listing Linux Files
Leason 7: Displaying Linux Files
Leason 8: Copying and Renaming Files
Leason 9: Creating Files and Directories
Leason 10: Deleting Files and Directories
Leason 11: Linux Files - Wildcards
Leason 12: The Nine Deadly Keystrokes
Leason 13: Linux File Permissions
Leason 14: Changing File Permissions
Last edited by MrNiitriiX on 30 Sep 2010, 15:01, edited 1 time in total.
"Injustice anywhere is a threat to justice everywhere." - Martin Luther King
Image
Upload List
Post Reply

Return to “Everything Linux”