Jan 10, 2018 Way too often, diets feel so restrictive and limiting, you just end up hungry. Not the macro diet—instead of counting calories, you’re focused on eating a healthy balance of proteins, fats and carbs.
Related
How to Add and Delete Users on Ubuntu 16.04 Tutorial
How To Upgrade to Ubuntu 20.04 Focal Fossa Tutorial
Tutorial
Introduction
The more you operate on the command line, the more you will find that the majority of the commands you use are a very small subset of the available commands. Most tasks are habitual and you may run these the same way every day.
While the makers of many of the most common command utilities have attempted to eliminate extraneous typing by using shortened names (think of how many keystrokes you save daily by typing “ls” instead of “list” and “cd” instead of “change-directory”), these are not ubiquitous. Additionally, many people always run commands with the same few options enabled every time.
Luckily, bash allows us to create our own shortcuts and time-savers through the use of aliases and shell functions. In this guide, we’ll discuss how to make use of these and give you some useful examples to get you started in the right direction.
How To Declare a Bash Alias
Declaring aliases in bash is very straight forward. It’s so easy that you should try it now.
You can declare aliases that will last as long as your shell session by simply typing these into the command line. The syntax looks like this:
<pre>
alias <span class=“highlight”>aliasname</span>=“<spanalias-examples'>Alias Examples
alias <span class=“highlight”>aliasname</span>=“<spanalias-examples'>Alias Examples
Now that you know how to create your own aliases, let’s talk about some popular ones that may be useful to you. These can be found throughout the web, and some may also be included in your distribution’s default bash configuration as well.
Navigation and Listing
Many of the most simple Linux commands are more helpful when you apply some formatting and options.
We discussed one
ls
example above, but there are many others you may find.Make
ls
display in columns and with a file type indicator (end directories with ”/“, etc) by default:We can also anticipate some typos to make it call the correct command:
Let’s also make an alias to pipe our output to
less
for viewing large directory listings with the long format:How about we stray from
ls
and try some helpful commands for cd
.This one will change to your parent directory, even when you forget the space:
You can also cut out the
cd
part entirely by making an alias for .
:We can find files in our current directory easily by setting this alias:
System Aliases
How about some of our monitoring and system stats commands? I call these with the same options every time, so I might as well make some aliases.
This one will list our disk usage in human-readable units including filesystem type, and print a total at the bottom:
We might as well add an alias for our preferred
du
output as well:Let’s keep going in the same direction by making our
free
output more human friendly:We can do a lot with our listing process table. Let’s start out by setting a default output: Mac catalina printers.
How about we make our process table searchable. We can create an alias that searches our process for an argument we’ll pass:
Now, when we call it with the process name we’re looking for as an argument, we’ll get a nice, compact output:
Miscellaneous Aliases
One common option to the
mkdir
command that we use often is the -p
flag to make any necessary parent directories. We can make this the default:We might want to add a
-v
flag on top of that so we are told of every directory creation, which can help us recognize quickly if we had a typo which caused an accidental directory branch:When downloading files from the internet with
wget
, in almost all circumstances, you’ll want to pass the -c
flag in order to continue the download in case of problems. We can set that with this:We can search our history easily like with a grep of the
history
command’s output. This is sometimes more useful than using CTRL-R to reverse search because it gives you the command number to do more complex recalls afterwards:I have a few system tools that I prefer to upgrade from the standard version to more complex tools. These will only work if you’ve downloaded the required utilities, but they can be very helpful. Keep in mind that these may affect your other aliases.
This one replaces the conventional
top
command with an enhanced version that is much easier on the eyes and can be sorted, searched, and scrolled without complications:In a similar way, the
ncdu
command can be downloaded which presents file and directory sizes in an interactive ncurses display that you can browse and use to perform simple file actions:There’s an upgraded utility for
df
as well that’s called pydf
. It provides colorized output and text-based usage bars. We can default to using this utility if we have it:Have you ever needed your public IP address from the command line when you’re behind a router using NAT? Something like this could be useful:
For my own purposes, I like to optimize the images I upload for articles to be 690px or less, so I use the ImageMagick package (
sudo apt-get install imagemagick
if not already available) which contains a command called mogrify
that does just this. I have this command in my ~/.bashrc
file:This will resize all of the PNG images in the current directory, only if they are wider than 690px.
If I then have to upload them to a server, I can use
sftp
to connect and automatically change to a specific directory:<pre>
alias upload='sftp <spangetting-started-with-bash-functions'>Getting Started with Bash Functions
alias upload='sftp <spangetting-started-with-bash-functions'>Getting Started with Bash Functions
Although aliases are quick and easy to implement, they are quite limited in their scope. You’ll find as you’re trying to chain commands together that you can’t access arguments given at runtime very well, among other things. Aliases can also be quite slow at times because they are read after all functions.
There is an alternative to aliases that is more robust and can help you bridge the gap between bash aliases and full shell scripts. These are called shell functions. They work in almost the same way as aliases but are more programmatic and accept input in a standard way.
We won’t go into extensive detail here, because these can be used in so many complex situations and bash is an entire scripting language, but we’ll go over some basic examples.
For starters, there are two basic ways to declare a bash syntax. The first uses the
function
command and looks something like this:<pre>
function <span class=“highlight”>function_name</span> {
<span class=“highlight”>command1</span>
<span class=“highlight”>command2</span>
}
</pre>
function <span class=“highlight”>function_name</span> {
<span class=“highlight”>command1</span>
<span class=“highlight”>command2</span>
}
</pre>
The other syntax uses a set of parentheses which is more “C-like”:
<pre>
<span class=“highlight”>function_name</span> () {
<span class=“highlight”>command1</span>
<span class=“highlight”>command2</span>
}
</pre>
<span class=“highlight”>function_name</span> () {
<span class=“highlight”>command1</span>
<span class=“highlight”>command2</span>
}
</pre>
We can compress this second form into one line and separate the commands with semicolons. A semicolon must come after the last command too:
<pre>
<span class=“highlight”>function_name</span> () { <span class=“highlight”>command1</span>; <span class=“highlight”>command2</span>; }
</pre>
<span class=“highlight”>function_name</span> () { <span class=“highlight”>command1</span>; <span class=“highlight”>command2</span>; }
</pre>
Let’s start off by demonstrating an extremely useful bash function. This one will create a directory and then immediately move into that directory. This is usually exactly the sequence we take when making new directories:
Now, when we use use this function instead of the regular
mkdir
command to auto change into the directory after creation:Good Allies For Macos Free
One cool function that you’ll see around is the
extract
function. This combines a lot of utilities to allow you to decompress just about any compressed file format. There are a number of variations, but this one comes from here:This function takes the first argument and calls the appropriate utility program based on the file extension used.
Conclusion
Hopefully this guide has given you some inspiration for creating your own aliases and bash functions. Extensive use of these can help make your time in the shell more enjoyable and less complex.
Remember to be wary of redefining existing commands with behavior that is potentially destructive. Even doing the opposite and aliasing a command to a safer variant (always asking for confirmation before deleting recursively, for instance) can come back to bite you the first time you’re on a system without it once you’ve come to rely on it.
To find candidates that might be good to create aliases for, it might be a good idea to search your history for your most commonly used commands. A one-liner from here allows us to see our most used commands:
We can easily use this list as a starting point for commands that we frequently utilize. In the comments section, feel free to share your favorite bash aliases and functions:
<div class=“author”>By Justin Ellingwood</div>