5 Tips which will make you fall in love with Linux terminal
If you use any Linux based distribution, chances are you’re already familiar with terminal and have used it at some point or other. The Linux terminal is a text interface to your computer. Often referred to as the shell, command line, console, prompt or various other names, it can give the appearance of being complex and might be intimidating to newcomers.
If you’re just starting out with Linux chances are you’re looking for solutions on web and frequently you would encounter some terminal commands which can be difficult to remember all at once or you simply typed
sl instead of
ls and are wondering why is there a steam locomotive on my screen.
1. Use Aliases
Alias in English language means ‘a false or assumed identity’, and in terminal it is a way to write commands in shorthand. Let’s take an example. Imagine you have a friend named
Vishvanathan and you talk to him frequently so use a pet name (or alias)
Vis to address him. Similarly you can tell your computer that when you say following expression, you need it to do a specific task. You can define aliases like this
alias please_read_file="cat file.txt"
Now, if you enter
please_read_file in terminal, it will show contents of
file.txt present in current directory. You can change
please_read_file to anything you like, even an existing command like
alias date=”cat file.txt” now when you type
date in terminal, it will show you contents of
file.txt instead of current date and time on your system. You can get creative and use them in different ways like
alias please_read_file="cat /some/really/long/path/file.txt" alias open_folder="cd /some/really/long/path"
In above box,
please_read_file will show contents of
file.txt placed in some folder whose path is difficult to remember.
open_folder changes current working directory to that folder whose path is defined in alias and you can access
file.txt without need to give it’s complete path, This way you can access any file which you like from a different location (perhaps a network computer whose IP address is difficult to remember) without changing your directory to that folder and then navigating back when you’re done.
Note: Aliases are temporary and your customization is lost when you close the terminal. To make these changes persist you need to setup a
bash_profilecheck this awesome article on how to do that
2. Use ⇅ arrow keys
When you press ↑ key on keyboard you get previous command. You can use ↑ and ↓ keys multiple times to browse your history in command line even after you restart terminal. And that’s it! It’s really is that simple.
P.s. This trick also works in Minecraft.
This is a really useful feature and can come in handy when you need to search for something really specific. Although traversing through your search history using arrow keys works just fine, But it defeats the purpose to press ↑ 10 times (or even more) to type a small command which will take only 6 keystrokes.
To overcome this problem, you can press
Ctrl+R in terminal and you will see
(reverse-i-search): appears where
[current user] and
[hostname] used to be. Now you can start typing your command and commands matching your search term will appear in reverse chronological order (latest to earliest).
(reverse-i-search)`date': sudo apt-get update
In the above example search term was
date and command which found was
sudo apt-get update . If that’s not the command you were looking for keep typing and as the search term changes, resulting command will also change accordingly. Once you find what you’re looking for just press enter(carriage return) and command will be executed in your terminal. If search term doesn’t match any command
failed will appear along with last command your search term matched with
(failed reverse-i-search)`new': npx express-generator
This is useful but it only shows last command which matched and if you try to browse up or down using ⇅ keys, search utility closes.
To solve this problem and further enhance searching capabilities you can use comments in your commands and then later search for those comments. Example date #some easy_to_remember phrase here,
cat command will be executed but anything after # (including the symbol) will be ignored while executing but will be stored in history.
cat -n read_file.txt # pizza
Now you can search for pizza and get exact command with custom arguments without much effort.
4. Use bash scripts for recurring combination of commands
If you need to run some commands in a specific order often, you can make a simple shell script and it will make your life so much easier. Just open up any text editor and type all commands in order you wish for them to execute. Save the file as
something.sh . Now change the permission of file to allow it to be executed. You can do that in 2 ways:
- use GUI : Open file properties ( press
Ctrl + i), Go to permissions tab and check the option saying ‘Allow executing file as a program’.
- use CLI : Open terminal and type following
chmod +x something.sh. Replace
something.shwith name of file you wish to execute.
starttime=$(date) # give path of a text file to log starting time times echo "update Started at $starttime" >>./log.txt sudo apt-get update sudo apt-get upgrade -y endtime=$(date) # give path of a text file to log finish times echo "update completed successfully on $endtime" >>./log.txt shutdown now #shutdown pc
I made a script which updates your computer and then shuts it down afterwards. Whenever I am about to shutdown my PC, I run this script and leave the desk. It updates and shuts down automatically. You can use it too if you like. Additionally you can un-comment line 3 and 8 to log starting and ending times in a text file.
You can now run your script by typing complete path followed by name of file in terminal . Example
./ means that file is in current working directory.
5. Use Tab to autocomplete paths and filenames
While typing it is easy to make typing mistakes or maybe one of your keys on your keyboard is not working and you need to find a work around. You can do this by pressing
tab key on keyboard to autocomplete names of file and folders. If there are more than 1 present it will autocomplete all characters which are similar and you can type next few characters and press tab again. If you don’t know what next characters are, you can press
tab twice and list of all matching search results will be displayed, you can read from list and type next few characters.