Combine commands using the ALIAS command in Linux

Home > Search > Linux commands
  by

The alias command allows us to combine frequently used commands. For example, let's say you often use the command date and then pwd.

[user1@server1 ~]# date
Mon Jan 01 2016 00:00:01 CDT 2016

[user1@server1 ~]# pwd
/home/user1

 

These commands can be combined by created an alias to date and pwd. In this example, the alias name is xyz. We can then type xyz to execute both the date and pwd commands.

[user1@server1 ~]# alias xyz='date; pwd'

[user1@server1 ~]# xyz
Mon Jan 01 2016 00:00:01 CDT 2016
/home/user1

 

The unalias command can be used to remove the xyz alias. After using unalias, we can verify the alias was removed by typing xyz and seeing that there is STDERR.

[user1@server1 ~]# unalias xyz

[user1@server1 ~]# xyz
No command 'xyz' found, did you mean:

 

Another (more practical) example is creating an alias that shows the current files in the pwd and the amount of disk space used by the files. 

[user1@server1 ~]# alias lsdu='ls; du -sh'

[user1@server1 ~]# lsdu
file1 file2 file3
4M

 

It does not matter if the alias is surrounded by single quotes or double quotes.

[user1@server1 ~]# alias singleq='ls $HOME'
[user1@server1 ~]# singleq
file1 file2 file3

[user1@server1 ~]# alias doubleq="ls $HOME"
[user1@server1 ~]# doubleq
file1 file2 file3

 

Another neat way to use alias is to shorten the CD (change directory command) to move down one, two, three, or four directories. For example, let's say we are starting at /home/user1. 

[user1@server1 user1]# alias .='cd ..'
[user1@server1 user1]# .
[user1@server1 user1]# pwd
[user1@server1 /home]# 

 

The move (mv), copy (cp), and remove (rm) commands are actually aliases to:

  • mv -i
  • cp -i
  • rm -i

The -i option means interactive, which will prompt you to confirm that you want to copy, move or remove a file. This prevents the file from accidentally being copied, moved or removed.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments