Linux Classes
Share This With a Friend  
LINUX CLASSES - FILES

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.

Previous Lesson: Deleting Files and Directories
Next Lesson: The Nine Deadly Keystrokes

[ RETURN TO INDEX ]


   

Comments - most recent first
(Please feel free to answer questions posted by others!)

EA     (24 Sep 2011, 20:36)
Assume that you are NOT currently in your home directory.

Enter a command to copy all files in your home directory beginning with the
letter 'a' to the current directory.
jvbuiten     (20 Jan 2011, 05:53)
what command to use if i want to delete all files in a filessystem ending with ~?
hari     (14 Jan 2011, 01:14)
It is good site.......for linux learner.......thanks Dr.Bob
CCee     (06 Dec 2010, 16:25)
when editing a file and saving it does order matter? what I mean is this:

:qw or :wq

lastly, how about this
:wq!
Dave     (12 Nov 2010, 13:48)
rm -f * means:
remove (rm)
force (-f)
all files - hidden or not (*)

If you want to do it recursively, it's rm -rf * with the r being recursively. Use this carefully as it will delete anything, system files and folders included. Force means the system will not prompt you.
Bob Rankin     (29 Oct 2010, 11:06)
@Shdz - You're both right and wrong. The -f flag means "force" (never prompt), and has no bearing on hidden or non-hidden files. However, you are correct that the "rm -f *" command does not remove hidden files.
Shdz     (29 Oct 2010, 10:47)
Haven't tried the exercise yet, but I think the result would be that all files but the hidden ones will be deleted. So, after executing the "rm -f *" command, all files in that directory are hidden files.

"rm -f *"
means remove ("rm")
all ("*" wildcard)
non-hidden files ("-f")

^^
tejas     (17 Aug 2010, 03:09)
yaaaaaaaaa linux is best!!!!!!!!!
Otto     (27 Apr 2010, 14:00)
Haven't done the exercise, but the hidden file shouldn't have been deleted. To delete it one would need to write rm -f .*, methink.
hahahaha     (08 Apr 2010, 00:47)
lol
Jason     (26 Mar 2010, 08:44)
They were all deleted.
tom     (15 Feb 2010, 13:43)
I don't want to take the time to do it.
what is the answer?

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
 


Ask Bob Rankin - Free Tech Support


Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.