Inscrivez-vous maintenant pour un meilleur devis personnalisé!

Nouvelles chaudes

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

May, 17, 2024 Hi-network.com
A sample .bash_aliases file.
Screenshot by Jack Wallen/

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 typingsudo apt-get update&& sudo apt-get upgrade -y, I can typeupdateand 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. Thegist 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 theclearcommand quite a bit. Whatcleardoes is simply clear the terminal, so you're back at bash prompt with a clean slate. Because I useclearso often, I prefer to simplify it with this alias:

alias c='clear'

Now, all I have to do is typecand 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 typel., I see all hidden files within a directory listed in color mode.

3. Move up a directory

Moving around the Linux filesystem uses thecdcommand. Let's say you're in the /home/jack/Documents/ directory and want to move up to /home/jack/Documents. You could run eithercd .., orcd /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 thedate 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 typenowall 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 typess -tulpn. Sometimes my brain gets fuzzy and I can't remembertulpn

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 typeportsto 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 isdnf. And, unlike Ubuntu, you don't have to update the package manager and upgrade the software. Instead, the manager runs with the commandsudo 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 thels 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 thehistorycommand. If you want to search the history | grep cdfor a string, you have to pipe the output togrep,like so:

history | grep cd

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

alias gh='history|grep'|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 thefindcommand to thewccommand (and the proper options required), let's simplify that process like this:

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

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

10. Safely remove files

The rmcommand is an easy way to delete files. The problem withrmis 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 '

Typetrashfollowed 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.

tag-icon Tags chauds: technologie

Copyright © 2014-2024 Hi-Network.com | HAILIAN TECHNOLOGY CO., LIMITED | All Rights Reserved.