Linux Configuration and Installation:Basic Linux Tools
-->
Previous
Table of Contents
Next
Wildcards
Like UNIX (and DOS, for that matter), Linux supports wildcards in command lines and shells scripts. A wildcard is merely shorthand for a character or a string of characters. Wildcards can come in handy if youre looking for a file and youve forgotten the specific filename (geez, I know the file ends in 1996), or if you want to see a list of files that fall within specific parameters (such as ending with .c, useful if you plan on using Linux for software development).
There are three types of Linux wildcards: *, ?, and [...]. Each will be explained.
NOTE: Technically, wildcards are the province of the shell, and in theory a discussion of wildcards should take place with a discussion of shells. For our purposes, however, were going to discuss wildcards at this point in the Linux discussion, because what were saying here applies to all shells.
In the previous section covering the ls command, we covered the commands use when its applied to single files. However, there may be times when you want to list a set of files that share a common characteristic, such as ending with .c. In this instance, you can tell ls to look for every file that ends with .c, using the following command line:
gilbert:~$ ls *.c
aardvark.c stuff.c titles.c xylophone.c
In this instance, ls is told to substitute * for any portion of a filename preceding an ending of .c. And, as you can see from the list of files, the command was successful. The ls is used to match any number of characters in a string, including zero characters:
gilbert:~$ ls titles*
titles titles.c
In the case of titles, the wildcard matched zero characters.
The asterisk (*) can be used at the beginning or end of a wildcard expression. You can also use more than one asterisk in an expression:
gilbert:~$ ls t*.*
titles.c
If you wanted to list the files with the string titles anywhere in the filename, you could use the following command line:
gilbert:~$ ls *titles*
subtitles titles titles.c
The asterisk wildcard is the most expansive wildcard available. On the other end of the spectrum is the question-mark wildcard, which is used to match a single character:
gilbert:~$ ls title?
titles
In this instance, ls did not match titles.c, which contains two characters after the search string of title. Titles, meanwhile, contained only one character after the search string of title, which matched the parameters of the ls command.
The final wildcard is used to return specific characters, as defined by brackets ([]). For example, youre looking through a directory filled with memos from the last 12 months. Since youve been a good Linux user, youve been placing a number at the end of every file, signifying the month it was written. (Yes, we know youre not likely to have too many files if youve just installed Linux. Think of this advice as something youll need in the future.) You want to track down a memo you wrote sometime in the summer, but you cant remember the name of the file, and a reading through the directory listings dont spark a memory. In this instance, youll want to narrow down the directory listings to files ending in 6, 7, or 8 (corresponding to June, July, and August). To do this with the ls command, youd enter 68 in brackets:
gilbert:~/memos$ ls *[6-8]
golf.8 golfanne.8 golfpat.6 golfjim.6
golftod.6 golftom.7
This narrows down the list of files returned by ls. It also means you probably play too much golf.
In the preceding example, we asked ls to return files that ended with a range of characters, i.e., in 6, 7, or 8. You can also use this wildcard to return a single character:
gilbert:~/memos$ ls *[6]
golfpat.6 golfjim.6 golftod.6
If youre searching for a character (remembering, of course, that Linux distinguishes between uppercase and lowercase letters at all times) or range of characters, you can list them in the brackets:
gilbert:~/memos$ ls report.[Ee]rc
report.Erc report.erc
Wildcards can be used with any Linux command.
Creating Directories with Mkdir
The mkdir command is used to create directories. If you plan on using Linux for most of your day-to-day stuff, we advise creating directories to help organize the many files Linux (and any other version of UNIX, for that matter) creates. Using mkdir is simple:
gilbert:~$ mkdir directory
where directory is the name of the directory you want to create. To create a directory named letters in your home directory, youd use the following command:
gilbert:~$ mkdir letters
To see if the directory was really created, you can use the ls command:
gilbert:~$ ls
letters/ text
You can also use it to create a new directory elsewhere in the directory hierarchy:
gilbert:~$ mkdir /users/kevin/letters
gilbert:~$ ls /users/kevin
letters/
Mkdir can create more than one directory on a command line:
gilbert:~$ mkdir letters data
gilbert:~$ ls
data/ letters/ text
Mkdir can also create a directory and a subdirectory in a single command line:
gilbert:~$ mkdir -p /letters/eric
Other options to mkdir are listed in Table 4.6.
Table 4.6 Other Options to the Mkdir Command
Option
Result
m mode
Sets the mode for the new directory.
Previous
Table of Contents
Next
Wyszukiwarka
Podobne podstrony:
218 22105 (221)218 (2)218 219v 04 21810 (221)221 224więcej podobnych podstron