Useful Ubuntu tips and tricks
Some handy Ubuntu tips in this post, check it out
By: Ajdin Imsirovic 01 March 2019 | Last updated: 18 January 2021
This article lists some experiences from working with Ubuntu on different projects.
Image by @purlzbaum on Unsplash
1. Quickly open the terminal in Ubuntu
The shortcut key to do this is CTRL ALT T. Easy stuff!
2. Programmatically provide your version of Ubuntu
When downloading some packages, you need to provide your version of Ubuntu; you can do it programmatically, like this:
$(lsb_release -cs)
For example, if you’re running Ubuntu 20.04, the above command will return focal
(as in Focal Fossa).
3. Programmatically get your logged-in username
Sometimes in some bash commands you need to print the username of the user that’s logged in when a command is ran.
For that, you can use $USER
.
4. Programmatically provide more information about your Linux distribution
This one’s similar to the previous tip, but it gives us more info:
lsb_release -a
An example of a returned output:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.2 LTS
Release: 20.04
Codename: focal
5. Quickly open another tab in bash
If you’ve got the bash window already open, to quickly open another tab in that same window, use this keyboard shortcut: CTRL SHIFT T
6. Install xclip to Ubuntu 20.04
First update apt-get
:
sudo apt-get update -y
Next, install xclip
:
sudo apt-get install -y xclip
That’s it, now you can type which xclip
in the bash to verify that this program is now available:
which xclip
We’ll know that xclip
is installed if something like this comes back:
/usr/bin/xclip
7. In Linux copy file contents and paste it in programatically
To copy and paste text programatically in Linux, we can use the xclip
command we installed in the previous step.
Let’s say we want to copy a value from a variable called $FORMAT
.
Now it’s a matter of doing the following:
echo -n $FORMAT | xclip
We now have the contents of the $FORMAT
variable in our clipboard.
Now we can paste onto the screen what’s currently in the clipboard by running:
xclip clipboard -o
TO reiterate, we run these two to copy and paste:
echo -n $FORMAT | xclip
xclip clipboard -o
8. Use alias commands in Linux
Sometimes you have very long commands that would be a bit hard to type, plus it’s easy to misspell some long commands. There’s a simple solution to this issue, and that is: using aliases.
Here’s an example of how it works.
First, open the terminal, and type the following command:
nano ~/.bashrc
Now you can add another two lines to the .bashrc
file, as follows:
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
The above two lines give us two “new” commands that we can use: pbcopy
and pbpaste
.
These two commands are used in Mac OSs, not in Linux. But if we’re used to the pbcopy
and pbpaste
commands, we can kind of emulate them like shown above.
We can even make aliases that use other aliases, like this:
alias cpspec='pbcopy < ~/Desktop/spec.txt'
For example, let’s say that we have a special file that we named spec.txt
, and we want to copy the contents of this file using the pbcopy
alias. Again, instead of typing the full command with the full path, we can simply use a short version, as alias - which we’ve called cpspec
. So now all we have to do is just type:
cpspec
As an introductory exercise to using alias commands in Linux, a useful and easy-to-do exercise includes *making aliases to cd-ing into some nested folders. For example:
alias cddeep='cd ~/Desktop/this/folder/is/nested/very/deep/`
Now, instead of having to type the entire cd
command with the full path, we can simply type:
cddeep
Aliases are a nice addition in your Linux toolbox, and a great productivity-boosting tool.
9. Run two commands in Linux but wait for the first to finish before the next one
This is a really newbie question, but still an important one.
The answer is very simple: We don’t have to wait.
We just specify one command, and then add &&
and we follow it up with the second command.
For example:
echo "first" && echo "second"
10. Display custom user in the prompt
For example, let’s say you’re recording a tutorial named Intro to Python
and you’d like to show that in your bash prompt instead of the actual username.
Here’s the command to do just that:
export PS1="introtopython $ "
A tiny little tip, but quite useful in certain cases.
11. Find all the filenames that contain a specific word in all the subfolders
Let’s say that you have a folder with a complex structure of subfolders and files, going several levels deep.
You are trying to quickly find only those files that contain a specific word in their filename. For example, let’s say you’re trying to find all the filenames that contain the word INVOICE
somewhere in the filename.
Here’s how you can easily find all the files that match the given criteria:
find -maxdepth 5 -name "*SCREENCAST*"
This command will result in the output of each of the files with the full path in their nested structure. Pretty useful!