X
Tech

Try these Linux bash aliases for more efficient use of the command line

A bash alias is a shortcut to a complicated command. Here are 10 aliases I've used to make using the command line a bit easier.
Written by Jack Wallen, Contributing Writer
A sample .bash_aliases file.
Screenshot by Jack Wallen/ZDNET

Sometimes I need a shortcut to make things a bit faster. There are also situations where my memory needs a helping hand with the litany of commands I use throughout the week. That's why I often turn to bash aliases.

For those who don't know, bash aliases allow you to create unique command shortcuts. For example, as I explained in an earlier article, instead of typing sudo apt-get update && sudo apt-get upgrade -y, I can type update and both commands will run. I use that command every day on my Pop!_OS desktop.

Also: Pop!_OS has a complicated name but it makes using Linux so easy

But aliases can do much more than make updating or upgrading your system easier. I've cobbled together 10 handy aliases that you could use to make your Linux experience a bit more efficient.

In the article above, I explain how to create aliases in Linux. The gist is you open the necessary file with:

nano ~/.bash_aliases

Once you've added your aliases, source the file with:

source ~/.bash_aliases

You can then use your new alias. 

With that said, let's get to the aliases.

1. Clear the terminal

I use the clear command quite a bit. What clear does is simply clear the terminal, so you're back at bash prompt with a clean slate. Because I use clear so often, I prefer to simplify it with this alias:

alias c='clear'

Now, all I have to do is type c and hit Enter to clear my terminal window and I'm ready to start anew.

2. Hidden files and enable color

I often have to locate hidden files (files that start with a dot) and also want to ensure the output is set to color (which makes it easier to discern between files and folders). 

Also: 8 things you can do with Linux that you can't do with MacOS or Windows

For this task, I create the alias:

alias l.='ls -d .* --color=auto'

Now, when I type l., I see all hidden files within a directory listed in color mode.

3. Move up a directory

Moving around the Linux filesystem uses the cd command. Let's say you're in the /home/jack/Documents/ZDNET directory and want to move up to /home/jack/Documents. You could run either cd .., or cd /home/jack/Documents. Or, you could use this alias:

alias ..='cd ..'

Now, if you type .., you will move up one directory.

4. Display the time

If you run the date command, you'll see the full output, which looks something like this:

Mon May 13 10:55:03 AM EDT 2024

But what if you just want to view the time? For that, you can configure an alias that makes use of the %T format control like this:

alias now='date +%T'

When you type now all you will see is the time in the format H:M:S.

5. List open ports

If you want to view all open ports on your system, you could type ss -tulpn. Sometimes my brain gets fuzzy and I can't remember tulpn

Also: How to choose the right Linux desktop distribution for you

I use an alias, so I don't worry about that issue. The alias is:

alias ports='ss -tulpn'

You can do the same. Set this alias us and all you have to do is type ports to see a listing of the open ports on your system.

6. Update a Fedora-based system

If you use a Fedora-based system, the package manager is dnf. And, unlike Ubuntu, you don't have to update the package manager and upgrade the software. Instead, the manager runs with the command sudo yum update -y. Instead of typing all of that, use this alias:

alias update='sudo yum update -y'

7. Sort by file size

Let's go back to the ls command, only this time let's sort our files and folders by file size and list them in a human-readable format. 

Also: The best Linux laptops for everyone

To simplify this task, we'll use the -F option, which places a / at the end of each directory (so there's no mistaking a file from a directory). That alias is:

alias lt='ls --human-readable --size -1 -S -F'

8. Easier grepping of your history

If you want to find a specific command you've previously run, you use the history command. If you want to search the history for a string, you have to pipe the output to grep, like so:

history | grep cd

The above command would list all commands in your history that included cd. Let's make that simple with an alias like this:

alias gh='history|grep'

Now, just type something like this:

gh cd

And you'll see the same output.

9. Count the number of files

The command gets complicated if you want to know how many files are in a folder. Instead of having to remember to pipe the find command to the wc command (and the proper options required), let's simplify that process like this:

alias count='find . -type f | wc -l'

All you have to do now is type count and you'll see how many files are within the current directory (and the included child directories).

10. Safely remove files

The rm command is an easy way to delete files. The problem with rm is that it permanently deletes a file. Instead, it's better to move those files to Trash and then later empty Trash. This alias gives you the hope of recovering accidentally deleted files. 

For this task, we'll create an alias that moves files to the Trash folder like this:

alias trash='mv --force -t ~/.local/share/Trash '

Type trash followed by the file you want to remove and it'll be swept away into Trash. 

And there you have it, 10 aliases that will make your Linux command line experience a bit easier, while not straining your memory to remember various options and switches.

Editorial standards