How to Use Alias in Linux and Ubuntu
An alias in Linux or Ubuntu is a shortcut for a command. It lets you type a short command and have the shell run a longer command for you.
For example, if you often run ls -l to see a detailed file listing, you can make ls run ls -l --color=auto instead. This saves typing and makes common terminal work faster.
Aliases are especially useful for commands you run every day, such as directory listings, server checks, log viewing, Git commands, and script folders.
Check the Current Folder
Start by moving into a folder and listing its files.
This example uses a sample server script folder:
cd /srv/admin/scripts/site-tools ls
Example output:
Check-Backups.sh Server-Inventory.sh
The normal ls command shows the file names, but it does not show details such as permissions, owner, file size, or modified date.
Run `ls -l` Manually
To see the long listing format, run:
ls -l
Example output:
total 20 -rwxrwxr-x 1 admin admin 4569 Jul 2 00:41 Check-Backups.sh -rwxrwxr-x 1 admin admin 11641 Jul 2 00:41 Server-Inventory.sh
This output shows more useful information:
- File permissions, such as
-rwxrwxr-x. - Number of links.
- File owner and group.
- File size.
- Last modified date and time.
- File name.
If you always want this detailed view, you can create an alias.
Create a Temporary Alias
Run this command:
alias ls='ls -l'
Now run:
ls
Example output:
total 20 -rwxrwxr-x 1 admin admin 4569 Jul 2 00:41 Check-Backups.sh -rwxrwxr-x 1 admin admin 11641 Jul 2 00:41 Server-Inventory.sh
Now the short command ls runs ls -l.
This alias is temporary. It only works in the current terminal session. If you close the terminal or log out, the alias is gone.
Make the Alias Permanent
To keep the alias after logging out, add it to your ~/.bashrc file.
Use this command:
echo "alias ls='ls -l --color=auto'" >> ~/.bashrc
Then reload the file:
source ~/.bashrc
Now run:
ls
Example output:
total 20 -rwxrwxr-x 1 admin admin 4569 Jul 2 00:41 Check-Backups.sh -rwxrwxr-x 1 admin admin 11641 Jul 2 00:41 Server-Inventory.sh
The --color=auto option keeps normal color output when your terminal supports it. This makes executable scripts, directories, and other file types easier to spot.
View Existing Aliases
To see all aliases currently loaded in your shell, run:
alias
To check one alias, run:
alias ls
Example output:
alias ls='ls -l --color=auto'
Remove an Alias
To remove an alias from the current terminal session, use unalias.
Example:
unalias ls
After that, ls goes back to its normal behavior for the current session.
If the alias is saved in ~/.bashrc, open that file and remove the alias line:
nano ~/.bashrc
Remove or comment out this line:
alias ls='ls -l --color=auto'
Then reload the file:
source ~/.bashrc
Useful Alias Examples
Here are a few common aliases for Linux and Ubuntu.
Show all files in long format:
alias ll='ls -la --color=auto'
Show disk space in human-readable format:
alias dfh='df -h'
Show folder sizes in the current directory:
alias duh='du -h --max-depth=1'
Clear the terminal:
alias c='clear'
Go up one directory:
alias ..='cd ..'
View recent system logs on systems that use journalctl:
alias logs='journalctl -xe'
Edit `.bashrc` Directly
If you plan to add several aliases, it is cleaner to edit ~/.bashrc directly:
nano ~/.bashrc
Add your aliases near the bottom:
alias ls='ls -l --color=auto' alias ll='ls -la --color=auto' alias dfh='df -h' alias c='clear'
Save the file, then reload it:
source ~/.bashrc
You do not need to reboot. Reloading ~/.bashrc applies the aliases to the current terminal.
Important Notes
- Aliases only affect the shell where they are loaded.
- A temporary alias disappears when you close the terminal.
- A permanent alias should be saved in
~/.bashrc. - Use quotes around the command when creating an alias.
- Be careful when aliasing common commands such as
rm,cp,mv, orls. - If an alias does not work, run
source ~/.bashrcor open a new terminal.
Quick Reference
Create a temporary alias:
alias ls='ls -l'
Make it permanent:
echo "alias ls='ls -l --color=auto'" >> ~/.bashrc source ~/.bashrc
Check the alias:
alias ls
Remove it from the current session:
unalias ls
Aliases are a simple way to make Linux and Ubuntu commands faster, shorter, and easier to remember. Once you start using them for repeated commands, the terminal feels much more comfortable.