Ubuntu Command Line Tips & Tricks For Beginners 2019

Before beginning today’s article, I’d like to thank the r/Ruby community for the immense support for my article featuring Ruby Tips & Tricks. Since you all seem to like tips & tricks articles a lot, I’ve decided to write one for the Linux and Ubuntu newbies out there.

In Linux, the command line/shell is the most important tool. The command line is what makes Linux a really powerful operating system for developers. A developer’s productivity can be improved greatly by utilizing the command line interface effectively. In this article, I will take you through some of the best Ubuntu command line tips & tricks I have learned over the years that have helped me immensely.

So, let’s jump right into it.

1. Creating Shortcuts For Long Commands

Linux commands are mostly designed to remain short and sweet however, sometimes you might have to use a bunch of flags and parameters which will result in you having to enter a really long line. Things become even more painful if you need to do this on a regular basis.

To make things easier, you can set up shortcuts for any command in the ~/.bashrc file which you can then use anytime, saving you truckloads of your precious time. Creating command line shortcuts is the easiest thing I learned during my noob days. For example, the command for viewing the git tree structure inside a git repository is

git log --pretty=oneline --graph --decorate --all

Now imagine having to remember the entire line and typing it out every time you need to view the git tree. painful right? So to create a shortcut for the above command, open your ~/.bashrc file and add the following line towards the end of the file:

alias gitline='git log --pretty=oneline --graph --decorate --all'

Voila! there you go, now after your reopen the terminal or source the bashrc file, you now just need to type “gitline” to view the git tree structure. Over the years, I have added over 20 shortcuts to my ~/.bashrc file for the commands I use on a regular basis. Here is the list of shortcuts I use:

alias gst='git status'
alias gco='git checkout'
alias gitline='git log --pretty=oneline --graph --decorate --all'
alias raisl='rails'
alias lla='ls -la'
alias vm='vi'
alias gd='git diff'
alias gds='git diff --staged'
alias railsenv='echo $RAILS_ENV'
alias prodenv='export RAILS_ENV=production'
alias testenv='export RAILS_ENV=test'
alias devenv='export RAILS_ENV=development'
alias railss='rails s'
alias raisls='rails s'
alias railsext='rails s -b 0.0.0.0'
alias glog='git log'
alias cls='clear'
alias ivm='nvim'
alias nv='nvim'
alias vi='nvim'
alias rc='rails c'

Some of the shortcuts above are used to execute the command even when there is a typo.

2. Using The Ack Tool For Searching Your Codebase

Ack is a faster and more powerful replacement for the grep tool. Searching through the source code to find a function definition or a constant declaration is something that developers do on a regular basis and grep has always remained a popular choice to achieve the same.

Ack is a relatively new tool that lets you search through the files much faster and has features like search term highlighting. It will return a result which highlights the line(s) containing the occurrence of the searched term along with its’ line number. You can learn more about the ack tool on their website aptly named “beyond grep”.

Ack is not included in Ubuntu by default, so you’ll have to install using apt-get. To install ack, use:

sudo apt-get install ack

3. Using Shortcuts To Get Things Done Quicker

Most new Ubuntu or Linux users do not know that there are shortcuts that can be used in the terminal to navigate through the commands, cut, copy and paste text. I have created a list of such useful shortcuts. Using these shortcuts instead of using your trackpad or the arrow keys will greatly improve your speed in using the command line interface.

ShortcutDescription
Ctrl + aMove to the beginning of a command
Ctrl + eMove to the end of a command
Ctrl + wCut the word on the left side of the cursor
Ctrl + kCut all text on the right side of the cursor
Ctrl + uCut all text on the left side of the curor
Ctrl + r Search the history of commands used
Ctrl + lClear Terminal
Ctrl + dLogout of Terminal or ssh session
Alt + fMove cursor to the next word
Alt + bMove cursor to the previous word
TabAutocomplete commands

4. Using Special Characters in Terminal to Achieve Results Faster

The Bash (Bourne-again Shell) has meanings associated with some special characters. Learning some of them can be advantageous to programmers as it helps to improve productivity. The following is a list of special characters with their use cases

CharacterUse CaseDescription
cd –Last Working Directory
!!sudo !!Last executed command
!$ls !$Arguments of the last executed command

5. Exiting a Broken SSH Session Without Force Closing the Terminal

When working on a remote server, if you are stuck with blazing internet speeds of 128Kb/s, the is a good chance that the SSH session will break and when that happens, any of the shortcuts you normally use to break out of a program, like “ctrl+c” will be of no use. Then the only option normally would be to force close the terminal. Once when I faced this issue, I spent a reasonable amount of time trying to figure out other ways to exit the ssh session without having close the terminal. After long hours of research, I finally found the solution.

To break out of a broken SSH session, you simply need to hit “ENTER”, “~” and “.” in the same sequence (↵~.) and there you go, the broken session has been successfully terminated.

6. Monitoring Log Files

Monitoring log files to listen for exceptions at runtime during development is a very frequent thing you’ll do as a developer. If you don’t then you need to start doing it because that is the most effective way of understanding how your code is being executed. In Linux, for monitoring log files, you can use the tail command with the -f argument to achieve this. Here is an example.

tail -f log/production.log

7. Truncating a file

Log files tend to grow really large really quickly especially while developing web applications. A large log file can slow down your web application’s boot up time and hence it always a good practice to either clear the log files or move the old logs to a separate directory. In Linux, there are two main ways by which you can truncate a file without deleting it. The first one is to use the truncate command and the second one is to use the “>” operation. Here are examples for both methods:

# using the truncate command
truncate -s 0 log/production.log

# using the ">" operation
> log/production.log

8. Using Man Pages for Learning About Commands & Tools

Linux installs documentation for every tool when the tool is installed. The best way to learn about any tool and all of its options is to read the documentation for the tool using the man command. This will give you a clear idea of the different ways in which the tool can be used. For example, if you are interested in reading more about the ls command, you can use the following command to print out its documentation.

man ls

9. Learn to Use The Vim Text Editor

I know I am going to receive a lot of criticism for this one but for me personally, Vim has helped me improve my programming speed more than any other tool or software. Initially, I started out using the “notepad++” text editor for windows and then with my introduction to Linux, I started using, “Sublime Text” and then moved to “Atom” before Vim.

Vim has a very steep learning curve but once you get acquainted to the way things work in Vim, you’ll never go back to any other editor, I promise.

For those of you who do not know what vim is, It is a very intricate command line text editor that works differently from the other “regular” editors. Getting used to Vim will take weeks or even months sometimes but is worth it.

A very famous article by Steve Losh “Coming Home To Vim” is a great place to start your journey with Vim.

So that’s my list of some of the most useful Ubuntu command line tips & tricks for beginners. I will release a part 2 of this article in the future with even more interesting tips & tricks for Linux Newbies. So stay tuned. Did you find the tips & tricks useful? Leave your thoughts and suggestions in the comments below.

Sreedev Kodichath

Sreedev Kodichath, a passionate software engineer, an avid blogger & an eloquent orator is the author of DevTechnica.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *