When using the Linux terminal, we often make mistakes. At other times, tasks feel just plain tedious. Fortunately, there are many terminal tricks that help you amend those mistakes and perform the tedious tasks easily. Let’s explore some of those tricks in this guide.
13
Run the Previous Command With sudo
We’ve all been there. You type a command, hit Enter, and get a “Permission denied” message. You realize that you forgot to prefix it with sudo. Instead of retyping the whole thing, you can use a neat little trick:
sudo !!
This tells the shell to rerun your last command, but this time, with sudo. The !! symbol is part of Bash history expansion. It expands to the entire previous command. So, if your last command was apt update, typing !!
would effectively run apt update. And sudo !! becomes:
sudo apt update
It’s a real time-saver, especially when you’re deep into a workflow and forget to elevate privileges.
12
Run Commands Without Leaving a Trace
Ever run a command you’d rather not have logged in your history? Maybe you’re using a secret token, experimenting with a risky command, or just trying something you’re not sure about. In Bash, there’s a surprisingly simple way to keep a command out of your history. Just start it with a space.
Here’s an example of my command history.
Now, after running a command starting with a space, it won’t get added to the history.
As you can see, that last echo command is not to be found. This trick relies on a Bash feature called HISTCONTROL. By default, many systems have HISTCONTROL set to either ignorespace or ignoreboth. These settings tell Bash to ignore commands that start with a space when writing history.
11
Use the Argument From the Previous Command
Ever typed out a long file name, then needed to use it again in the next command? Instead of copying and pasting (or retyping,) just hit Alt+period (.) and it will insert the last argument from the previous command right where your cursor is. It works in most Bash shells, and it’s especially handy for commands that work on the same file or directory over and over.
For example, you might issue this command:
mkdir really_long_directory_name_with_underscores
Then you could type cd and hit Alt+. to change directories to the one you just made.
Another common situation where you may use this is when you extract a archive file and want to remove it. If you ran this command:
tar -xvf archive_name.tar.gz
You could just type rm and press Alt+. to delete the archive. You can press this shortcut multiple times to cycle through previous arguments from earlier commands.
Related
The 8 Types of Linux Terminal Programs: Do You Know Them All?
How to tell your filters from your TUIs.
10
Replace a Word With Another One in the Previous Command
We all type fast and mess up sometimes. Perhaps you are using the wrong filename, server name, or have mistyped a flag. Instead of retyping the entire command, Bash gives you a quick correction tool, the caret (^) symbol:
^wrong^right
This replaces the first occurrence of “wrong” in your previous command with “right,” and runs the updated command immediately. Let’s say you meant to ping google.com, but typed ping goggle.com mistakenly. Just do:
^goggle^google
Bash will run:
ping google.com
This can be useful when you have a quick typo to fix, and it occurred only once.
9
Swap the Characters Before and After the Cursor
If you’re a fast typist, you’ve probably mashed the wrong keys and typed something like:
sl
When you actually wanted to type:
ls
Yes, that’s such a common typo that there’s literally a command-line joke tool called sl that shows a train chugging across your screen.
But instead of hitting backspace, retyping, or reaching for the arrow keys, there’s a quicker fix. Just hit Ctrl+T. It swaps the two characters right around your cursor. Let’s say you typed:
grpe file.txt
You meant grep. Just place the cursor on the p, hit Ctrl+T, and it becomes:
grep file.txt
It’s a tiny trick that feels minor until you catch yourself using it 20 times a day.
8
Using Aliases
Let’s face it. Some Linux commands are just long. And when you’re running them often, typing the same flags or full paths gets irritating. Not to mention error-prone. That’s where aliases come in. They allow you to create your own command shortcuts, using simple names for more complex or frequently used commands.
Creating an alias is as simple as:
alias shortname="actual command here"
Let’s say you’re always running:
ls -lah --color=auto
You can quickly create an alias like this:
alias ll="ls -lah --color=auto"
Now you can just run ll
on the terminal to do the whole thing. Note that this will only be valid for the current terminal session. If you want to create a permanent alias, you’ll have to add it to the ~/.bashrc file and reload it.
7
Format Your Output in Columns
On Linux, we often need to work with data in the command line. Sometimes, the output can be quite messy. Luckily, we have the column command. It can turn that chaos into clean, readable tables of data. The column command formats text into neat columns, aligning things nicely based on whitespace or a custom delimiter. It’s perfect when you’re dealing with data like lists, CSV, colon-separated data, etc.
Let’s see a common example. We can normally list the processes on Linux with:
ps aux
We can format it further using the column command:
ps aux | head -10 | column -t
This tells column to create a table with aligned columns. When using this command, it’s best if you have predictable input. If your data is wildly inconsistent, the output might still look a bit rough. Moreover, if your delimiter appears in values, it might throw off the formatting.
6
Running Multiple Commands Consecutively
Sometimes you want to run several commands back to back. Instead of typing them one at a time and waiting, you can chain them together on a single line. Then, control whether they run unconditionally or only if the previous one succeeds.
The semicolon (;) lets you chain commands so they run one after the other, regardless of success or failure.
echo "Updating..." ; sudo apt update ; echo "Done!"
In this example, even if apt update fails, the final echo “Done!” still runs.
The && operator, on the other hand, ensures that each command only runs if the one before it succeeds. This is a smarter way to chain commands.
sudo apt update && sudo apt upgrade
The command after && will only run if the command before it runs successfully. The opposite of && is the || (two vertical bars) operator. When two commands are combined using this symbol, the command on the right will execute only if the command on the left fails. Using && and || helps you debug issues with your Bash scripts.
Related
5 Ways to Make Linux Commands Work the Way You Want
You don’t really know the full power of Linux until you can tweak your toolset.
5
Using Fingerprint Instead of a Password
Typing your password every time you run sudo or unlock your screen? It’s secure, but after doing it multiple times, it starts feeling tedious. If your laptop has a fingerprint reader, you can use it to authenticate instead of typing your password.
These steps are geared towards Ubuntu and derivatives. The steps may vary for other distros.
First, install the necessary packages:
sudo apt install fprintd libpam-fprintd
After that, run this command to scan your fingerprint:
fprintd-enroll
Then open the PAM configuration file for sudo:
sudo nano /etc/pam.d/sudo
Add this line at the top:
auth sufficient pam_fprintd.so
Run a command with sudo to test the fingerprint. The system will prompt you to scan your fingerprint instead of asking for a password.
4
Go to the Previous Directory You Were In
I often find myself jumping back and forth between two directories. Typing the full path repeatedly is annoying. So this neat little trick was a lifesaver for me:
cd -
This command sends you to the previous working directory, just like a back button in your Linux file system. Let’s say you start in:
/home/user/projects/website
Then you go to “~/Downloads.” Now, if you run the cd – command, you’re instantly back in “/home/user/projects/website.” And if you run cd – again, you go back to “~/Downloads.”
Source link