Linux is a powerful open source operating system that has been around for many years and is widely used for running servers and websites. But most students and makers encounter it for the first time when they are working on projects with their Raspberry Pi or similar single-board computers (SBCs) such as BeagleBone Black or Intel Galileo. By gaining a deeper understanding of Linux, makers can add another useful tool to their kit that will help them build their projects more easily.
If you are like me, your spelling and typing abilities may be lacking. Too many times I have spent 20 or 30 seconds typing a long command with lots of options only to find out after I hit enter that I had something wrong and needed to start from the beginning again. Not only that, but with all the possible choices, it can be hard to remember exactly the command you used to perform a certain task from day to day. Luckily, the Linux shell has some tools built in that can help with both of these problems.
Auto-complete a command: TAB
You can use the auto-complete feature of the shell by simply pressing the Tab key on the keyboard. This will auto-complete a command that has been partially typed and it will also auto-complete a filename based on the context of what you are typing.
TIP: By default, Tab doesn’t always know about the available options for a command, but can auto-complete the name of the command and any associated file names that might be used as part of a command.
For example, if you type “tou” and press the Tab key, the shell will fill in the rest of the missing letters to make “touch”. If there are multiple options that start with the letters you have entered, the first time you press Tab nothing will happen. If you press it again, however, the shell will display a list of all possible commands or file names that start with the letters you entered. So, if you type “mkd” and press Tab twice, you will be presented with two options for commands that start with mkd: mkdir and mkdosfs:
pi@raspberrypi ~ $ mkd mkdir mkdosfs pi@raspberrypi ~ $ mkd
If you continue to add more characters and then press Tab, you will eventually rule out all the other options and the shell will complete the rest of the command or filename when there is only one choice left. This auto-complete feature is a real time saver with bigger commands and long file names. It also eliminates spelling errors when you haven’t used a command very often yet.
Search for a previous command: Up, CTRL-R
Linux keeps a history of all the things you type into the command line. A simple way to review the commands you have typed is simply to use the Up Arrow to scroll back through each command starting with the most recent. If the command you are looking for is further back in your history, you can search for it by pressing “Ctrl-R” on the command line followed by some characters. For example, if you wanted to search for the last time you used nano to edit a file you could press “Ctrl-R” followed by “nano”.
It doesn’t matter if there’s already some information entered at the cursor when you press Ctrl-R. That text won’t be used for the search, only what you type after you press Ctrl-R. Notice that the prompt changes to (reverse-i-search) followed by the letters you entered when doing this type of search through your command history.
(reverse-i-search)‘nano’: nano hello.sh
If you press one of the arrow keys, Home, End, or Tab, you will finish the search and be able to edit the command that you looked up. You can also continue to search through your history by pressing Ctrl-R multiple times before you exit out of the search.
Try it for yourself: Change to your home directory and create a file by typing:
cd tou <TAB> file1
When you press Tab it should complete the name of the touch command. Now change to your Downloads directory by typing:
cd D <TAB> <TAB>
You should see something similar to this:
pi@raspberrypi ~ $ cd D Desktop/ Documents/Downloads/ pi@raspberrypi ~ $ cd D
Add the letters “ow” and press Tab again to auto-complete the path we want and press enter.
Now let’s create our second file by using the command history. Press Ctrl-R followed by “tou”:
pi@raspberrypi ~ $ cd D Desktop/ Documents/Downloads/ pi@raspberrypi ~ $ cd Downloads/ (reverse-i-search)‘tou’: touch file1
Press the End key and change “file1” to “file2”. Press enter to complete the task. Now you have created two files — one in your home directory and one in the Downloads directory. You have also saved a lot of typing in the process!