There are a number of shortcuts and wildcards available for use in Linux and learning these can make navigating through your system substantially easier,more intuitive and quicker than using a GUI.
Table of Contents
Shortcuts
tab
– Auto-completion. Begin typing a command, filename or directory, hit tab
and Bash will automatically complete it for you. A second tab
will display a list of options if there are multiples. For example, if you input b
tab
tab
, you will be given a list of all possibilities beginning with the letter b.
ctrl+c
– end task. This will cancel a running command.
ctrl+u
– erase current line
ctrl+a
– go to beginning of line.
ctrl+e
– go to end of line.
ctrl+l
– clear terminal. (This does not delete anything, simply clears the terminal window of text.)
ctrl+left-arrow
– go to beginning of previous word.*
ctrl+right-arrow
– go to beginning of next word.*
ctrl+d
– logout or exit.
.
– working directory. (To be used in conjunction with other commands, for example mv /path/to/file .
)
..
– relative parent directory. (To be used in conjunction with other commands, for example cd ..
)
~
– user home directory. (To be used in conjunction with other commands, for example ls -lah ~
.)
*May not work on some operating systems.
Wildcards
There are three wildcards to be aware of;
*
– none or more characters.
?
– one character.
[]
– a range of characters.
It’s important to understand that these wildcards are parsed by bash (the program which provides the command-line itself) and not the command (or program) you’re actually using. For example, if we input ls *.txt
, the command-line translates the *
for us before the ls
program receives the command. This allows us to use wildcards for any command we use without limitation which is kind of a big deal.
*
The *
(referred to as asterisk or splat) character is the wildcard we use when we don’t care how many characters are being represented. For example, let’s say we wanted to list only the files or directories in our directory starting with the letter a, we would use ls a*
. There are endless practical uses for this but let’s imagine, you had just converted all your jpg files to a more compressed format and wanted to make sure you were not storing any in your uploads
directory, you could use rm /path/to/wp/wp-content/uploads/*/*.jpg
to tell the rm
command to delete ant file ending with .jpg
and to look for them in any sub-directory of uploads
.
?
The ?
character represents a single character. Using the same examples as before, ls a?
would list any files or directories in the working directory but only if they had only one character following the a. As you can use multiple wildcards in the same command, let’s extrapolate our second example and imagine you had just converted all your jpg, gif, and png images to webp. Since we know that webp has a 4-character extension and the source formats are all 3-character extensions, we can run the following command: rm /path/to/wp/wp-content/uploads/*/*.???
. This would tell the rm
command to delete any file ending with a 3-character extension and look for them in any sub-directory of uploads
.
[ ]
As I understand it, these are named differently depending where you come from, but I’ve always known them as “square brackets” so I’ll stick with that. In bash, they are used to represent the range of characters between them. Going back to the same examples as above, ls [abc]*
would list any file or directory starting with a or b or c in my working directory. You can also use a hyphen to indicate an inclusive range so ls [a-c]*
would yield the same result. You can also specify multiple ranges, for example, ls [a-cx-z]*
would list all files or directories in the working directory beginning with a or b or c or x or y or z. This wildcard also works with numerals so ls *[0-9]*
would list any file or directory with a numeral in it’s name.
As you are not limited to the amount of wildcards you use, it is possible to get quite creative with these. A practical example I use quite regularly is when looking for plugin generated backup files in WordPress installations. Most backup plugins will include the date somewhere in the name but they all use different formats. Some will breakup the date with _, while others use -. Some include the date as a prefix, others as a suffix. If I use the following wildcard string in my search, I can normally catch them all;
*[0-9][0-9][0-9][0-9]?[0-9][0-9]?[0-9][0-9]*
This represents any string that includes four numerals (a year), followed by a separating character, followed by two numerals (a month), followed by a separating character, followed by two final numerals (a day). Including a *
at the beginning and end, allows this string to occur with either a suffix or prefix or neither.