Objective
In part 5, we covered how to connect commands using pipes, allowing us to build powerful command chains. Now, we're going to take your command line efficiency to the next level by introducing one of the most practical time-saving features in Linux: aliases.
What Are Linux Aliases?
Think of aliases as custom shortcuts or nicknames for commands. Just as you might have a nickname for a friend with a long name, Linux allows you to create short, memorable names for long or complex commands that you use frequently.
An alias is essentially a way to tell your shell, "When I type this short command, I actually want you to run this longer command instead." It's like creating your own personal command vocabulary tailored to how you work.
Creating Your First Alias: Step by Step
Let's jump straight into creating a permanent alias that will be available every time you log in. We'll do this by creating a special file that Bash automatically reads when it starts.
Step 1: Creating the .bash_aliases File
First, we need to create a file called .bash_aliases
in your home directory:
touch ~/.bash_aliases
Let's break down what this command does:
touch
is a command that creates a new file if it doesn't exist~
is a shortcut that represents your home directory (like /home/username).bash_aliases
is the name of our file
What's with the Dot (.) in Front of the Filename?
You might be wondering about that dot (.) at the beginning of .bash_aliases
. In Linux, any file or directory name that begins with a dot is considered "hidden". This means:
These files won't show up in normal directory listings with
ls
You need to use
ls -a
to see them (the-a
stands for "all", including hidden files)They're typically used for configuration files that you don't need to see every day
For example, let's see the difference:
# Create a regular file and a hidden file
$ touch regular_file .hidden_file
# List files normally
$ ls
regular_file
# List all files including hidden ones
$ ls -a
. .. .hidden_file regular_file
Notice how .hidden_file
only appears when we use the -a
option.
It's very important to use exactly the name .bash_aliases
(not bash_aliases
or .bash_alias
) because Bash specifically looks for this filename when it starts.
Step 2: Writing Your First Alias
Now let’s imagine that you have built very complicated long command where you used many pipes similar to the example below, and you don’t want to type that every time you need to use it!
#Your Original Command could be something like this
date | tee /path/fulldate.txt | cut --delimiter=" " --fields=1 | tee /path/shortdate.txt | xargs echo hello
This is a pipeline of five distinct commands connected with pipes (|
), each passing its output to the next. Let's break down what happens at each stage:
date
- This starts our pipeline by generating the current date and time. For example, if run today, it might output something like:Tue Apr 9 08:15:23 UTC 2025
tee /path/fulldate.txt
- Thetee
command takes the output fromdate
and does two things simultaneously:It saves a copy of the complete date and time to the file
/path/fulldate.txt
It also passes that same text forward to the next command in the pipeline
So now our file contains the full date string, and the same text continues down the pipeline.
cut --delimiter=" " --fields=1
- This command takes the date string and:Splits it wherever there's a space (that's what
--delimiter=" "
does)Keeps only the first field after splitting (
--fields=1
)So if our input was
Tue Apr 9 08:15:23 UTC 2025
, after splitting at spaces we have:Field 1:
Tue
Field 2:
Apr
Field 3:
9
so on ..
Since we're asking for just field 1, the output of this command is just
Tue
(the day of the week).
tee /path/shortdate.txt
- This secondtee
commandSaves the day of the week (Tue) to another file called /path/shortdate.txt
Continues passing this shortened text down the pipeline
So now we have two files: one with the full date and time, and another with just the day of the week.
xargs echo hello
- Finally,xargs
takes the day of the week and:Converts it into a command-line argument for the
echo
commandEffectively runs
echo hello Tue
So the final output that appears on your screen would be
hello Tue
(assuming today is Tuesday).
This is a perfect example of the power of Linux pipes that you covered in Part 5. By connecting these commands together, you're able to perform multiple operations in a single line, processing and saving the data at various stages along the way.
Creating an alias for this command would save you from having to type this entire pipeline each time you want to perform this specific set of operations.
Now let's open the .bash_aliases
file in a text editor. You can use nano, vim, or any other text editor you're comfortable with:
nano ~/.bash_aliases
To create an alias for this long command, add the following line to your .bash_aliases
file:
alias getdates='date | tee /path/fulldate.txt | cut --delimiter=" " --fields=1 | tee /path/shortdate.txt | xargs echo hello'
Here's what this line does:
alias
tells Bash you're defining a new aliasgetdates
is the short name you'll type instead of the full command=
connects the alias name to the actual commandThe entire command is enclosed in single quotes (this is important!), you can use double quotes instead.
Save the file and exit the text editor (in nano, press Ctrl+O to save, then Ctrl+X to exit).
Step 3: Activating Your New Alias
For the changes to take effect, you have two options:
Log out and log back in to your terminal, or
Reload your Bash configuration with:
source ~/.bashrc
The source
command reads and executes the content of a file in the current shell. Since .bashrc
automatically loads your .bash_aliases
file, this will make your new alias immediately available.
Step 4: Using Your New Alias
Now, instead of typing that long command, you can simply type:
getdates
Bash will automatically run the full command for you! You should see:
The full date saved in
/path/fulldate.txt
Just the day of the week saved in
/path/shortdate.txt
# View the full date file
$ cat /path/fulldate.txt
Wed Apr 9 10:15:23 UTC 2025
# View the short date file
$ cat /path/shortdate.txt
Wed
"hello [day of week]" displayed in your terminal
hello Wed
Congratulations! You've just created your first permanent alias that will save you time whenever you need to run this command.
A Quick Note About File Paths
In our example, we used /path/fulldate.txt
and /path/shortdate.txt
. These are placeholder paths and might not work on your system. You should replace them with actual paths where you want to save these files, such as:
~/fulldate.txt
(in your home directory)/tmp/shortdate.txt
(in the temporary directory)
For example, a more realistic alias might be:
alias getdates='date | tee ~/fulldate.txt | cut --delimiter=" " --fields=1 | tee ~/shortdate.txt | xargs echo hello'
Advanced Alias: Using xargs with Commands That Don't Accept Standard Input
In our previous example, we created an alias for a command chain that used pipes to pass data between commands that were designed to work with standard input. But what about commands that aren't designed to accept input through a pipe?
Let's look at the “cal”
command as an example. This useful command displays a calendar in your terminal. Normally, you could use it like this:
cal -A 1 -B 1 12 2020
When you run this command, the output looks like this:
November 2020 December 2020 January 2021
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 1 2 3 4 5 1 2
8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9
15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16
22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23
29 30 27 28 29 30 31 24 25 26 27 28 29 30
31
As you can see, it displays a three-month calendar showing:
November 2020 (one month before December) due to -B 1 option
December 2020 (your specified month)
January 2021 (one month after December) due to -A 1 option
When we use a command like cal
, it expects arguments directly on the command line (cal 12 2020
), not through a pipe. This is a fundamental distinction in how Linux commands work, some read from standard input (stdin), while others only look at their command-line arguments.
Think of it like speaking to someone who doesn't read notes. If you write "12 2020" on paper and hand it to them, they won't know what to do with it, you need to speak "12 2020" directly to them.
Creating an alias for a command that doesn't accept standard input (like cal
) requires a special approach using xargs
especially if your intention is to pass the arguments manually to the alias, Here's how we'd build this alias:
alias CalMagic='xargs cal -A 1 -B 1'
Let's break down what's happening here:
alias CalMagic=
- We're creating an alias named "CalMagic"xargs
- This crucial command takes whatever text is piped to it and converts it to command-line argumentscal -A 1 -B 1
- This tells cal to show the specified month, plus one month after (-A 1
) and one month before (-B 1
)
Let’s run our new alias
Now let's see our new alias in action. We can pipe month and year information to it:
echo "12 2020" | xargs CalMagic
What happened behind the scenes:
echo "12 2020"
output the text "12 2020"This text was piped to our
CalMagic
aliasThe alias expanded to
xargs cal -A 1 -B 1
xargs
took the input "12 2020" and converted it to arguments forcal
The command effectively became
cal -A 1 -B 1 12 2020
cal
displayed the three-month calendar as requested
Summary
An alias is a custom nickname for a command or pipeline
Aliases go in a .bash_aliases file in your home folder
A period (.) at the start of a filename makes that file “hidden”
alias aliasName=”command1 -options args | command2 -options args ..”
Always Reload your Bash configuration or restart your terminal after new alias
Make sure the first command can be piped to! or use xargs