This chapter introduces the concept of wildcards (globbing), along with the
A wildcard in Unix-like systems is a special character that allows for the specification of a certain filename. It is easier to show than explain.
Let’s say we had a directory full of
.txt files and we wanted to move them all to the parent directory using the shell. With the
mv command, we can enter each file in the command and execute it to move all files. Fortunately there is a better way via the use of wildcards (the use of a wildcard is called globbing). The
* wildcard matches any characters, so we can run a command like
mv *.txt ...
Below is a list of wildcards:
*matches any characters
?matches any single character
[chars]matches any character that is a member of the set chars
[!chars]matches any character that is not a member of the set chars
[[:class:]]matches any character that is a member of the specified class
Below is a list of commonly used character classes:
[:alnum:]matches any alphanumeric character
[:alpha:]matches any alphabetic character
[:digit:]matches any numeral
[:lower:]matches any lowercase letter
[:upper:]matches any uppercase letter
Below is a list of examples:
*matches all files
a*matches all files beginning with
a*.txtmatches all files beginning with
aand ending with
sys???matches all files beginning with
sys, followed by exactly three characters
[tuv]matches all files beginning with either
log[0-9][0-9]matches all files beginning with
log, followed by exactly three numerals
[[:digit:]]*matches all files beginning with a numeral
[![:upper:]]*matches all files that do not begin with an uppercase letter
*[[:lower:]42]matches all files ending with a lowercase letter or the numeral
mkdir command is used to create one or more directories (if the do not exist). The command
mkdir bob alice elliot creates the directories
elliot. A useful option of the
mkdir command is the
-p option, which allows for
mkdir -pbob/alice/elliot`. This will create all non-existent parent directories.
cp command is used to copy one or more files/directories. It is important to remember, that unlike the
mkdir command, copying will overwrite the destination if it exists. Similar to the
mkdir command, we can pass multiple items which will all be copied. A useful option provided by the
cp command, is the
--update option. This option only copies files that don’t exist or are newer than the existing file in the destination directory.
mv command is used for both moving and renaming files. Like the
cp command, it is important to remember that the
mv command will overwrite the destination if it exists. Also like the
cp command, a useful option for the
mv command is the
--update option, which only moves files that either don’t exist or are newer. Another useful option is
--interactive, which prompts for confirmation before overwriting an existing file.
rm command is used to remove/delete files. This is the most dangerous command, and should be used with caution. It is dangerous, because the
rm command does not send the removed file(s) to the trash, but removes it entirely from the system. Like the
mv command, the
rm command has the
--interactive option, which prompts the user for confirmation before deleting file(s). Another useful option is the
--force, which ignores nonexistent files/arguments and overrides the prompt from the
ln command is used to create hard/symbolic links. Every file has a single hard link, which is what gives the file it’s name.
Hard links are two limitations that are important to remember.
Symbolic links came about to overcome the limitations of hard links, by creating a special type of file that contains a text pointer to the referenced file(s).