Useful aliases in Bash

The Linux operating system includes many tools that can make life much easier for system administrators, especially beginners, for whom working with console commands using many different parameters often seems too complicated. In this article, we will talk about using aliases and look at examples of creating the most useful ones.

temporary alias

At its core, an alias is just a shortcut for commands. That is, we can, by entering just one word, run any command, or even a group of commands, including parameters and file names. Consider a simple but useful example. Suppose we need to sort all the files in the output of the ls command by size. For example, we need to understand which files take up too much space in the Downloads directory and whether they should be deleted.

To accomplish this task with ls, we would need the following construct:

ls --human-readable --size -1 -S –classify

Typing a command with so many parameters every time is not very convenient. You can use the alias command to create a shortcut for this command.

alias lsrt="ls --human-readable --size -1 -S --classify"

Now running lsrt will produce the same result as using ls with options.

In general, the alias command and its values ​​might look like this:

alias name=value

alias name="command"

alias name="command arg1 arg2"

alias name="/path/to/script"

alias name="/path/to/script.sh arg1"

If we no longer need the created alias, we can use the command:

unalias имя_алиаса

Although, in fact, the alias created in this way will live until the end of the session of this user. When you log in again, you will have to create it again.

I note that as an alternative to aliases, you can use ready-made scripts, however, in order for them to be available from any directory, the directory with the scripts will need to be registered in $ PATH and correctly distributed execution rights. When using aliases in Bash, things are much simpler.

Adding to Bash

In order to make our aliases permanent, we need to add them to the .bashrc file. The syntax will be the same as before. As an example, let’s look at ls with options that also display files that start with a dot:

alias ls_hid=’ls -d .* --color=auto’

Add this line to the .bashrc file and execute in your home directory:

After running this alias, we can see files and directories that start with a dot, including .bashrc itself.

Let’s continue setting up our admin utilities. Let’s create an alias to update the lists of packages and the packages themselves:

alias update="sudo apt-get update && sudo apt-get upgrade"

Using one such alias will allow you to get rid of a whole line of commands.

Another useful short command that will allow us to quickly find the desired operating system command in the history of commands already used:

alias gh="history|grep"

This can be useful if you don’t remember which options were used with a particular command before, or which directories and files were previously used with a given command.

Here is what the use of this alias looks like:

Let’s see how we can replace the more complex one-liner. In the example below, we will format the output of the mount command, leaving only a list of mounted drives in it. If the simple output of the mount command contains quite a lot of unnecessary information that only makes it difficult to understand, then as a result of formatting, we get information in the first column about what is mounted, and in the second column information about which resource we mounted to.

alias mnt="mount | awk -F' ' '{ printf \"%s\t%s\n\",\$1,\$3; }' | column -t | egrep ^/dev/ | sort"

Result:

A couple more small but no less useful aliases. Sort by modification time and count files:

alias left="ls -t -1"

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

Virtual environments and networks

The next set of useful aliases will focus on working with Python virtual environments and network utilities. A Python virtual environment is an isolated environment for running Python projects. That is, each project in the course of work can have its own dependencies, regardless of what dependencies the other project has.

The following are two aliases for setting up virtual environments in Python:

alias ve="python3 -m venv ./venv"

alias va="source ./venv/bin/activate"

When working with a network, it is also convenient to have short aliases at hand that allow you to quickly execute the desired set of commands. For example, if we need to find out which ports are currently listening on the machine, we can use the following alias:

alias ports="netstat -tulanp"

Also, if we need to look at the contents of the iptables chains, then there is no need to remember the entire set of necessary keys each time, but instead we can use the following alias:

alias iptl="sudo /sbin/iptables -L -n -v --line-numbers"

And one more small utility, which would also be nice to put in an alias, is the ability to check if the remote server supports compression:

alias hdrc="curl -I --compress"

Goodies for Docker

When working with Docker containers, you have to start and stop containers frequently. It is most convenient to prepare aliases in advance to perform the corresponding tasks.

So, if we want to run a container in privileged mode, then we can create an alias like this:

alias dockrun=’sudo docker run -i -t --privileged $@’

To stop the container, create the following alias:

alias dockstop=’sudo docker stop $(docker ps -a -q) ‘

And to remove, use the following:

alias dockrm=’sudo docker rm $(docker ps -a -q)‘

Conclusion

On this, I think, the story about aliases can be completed. Naturally, the above aliases are not an exhaustive list of all commands that can be “reduced” with their help. If necessary, the reader can create their own set of shorthand commands to work with.

The article was prepared in anticipation of the start of the course DevOps. Practices and tools. Click the link below to find out more about the course program and register for a free lesson.

Similar Posts

Leave a Reply

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