- Make an Alias in Bash or Zsh Shell in macOS with Terminal
- 30 Handy Bash Shell Aliases For Linux / Unix / MacOS
- More about bash shell aliases
- How to list bash aliases
- How to define or create a bash shell alias
- How to disable a bash alias temporarily
- How to delete/remove a bash alias
- How to make bash shell aliases permanent
- A note about privileged access
- A note about os specific aliases
- 30 bash shell aliases examples
- #1: Control ls command output
- #2: Control cd command behavior
- #3: Control grep command output
- #4: Start calculator with math support
- #4: Generate sha1 digest
- #5: Create parent directories on demand
- #6: Colorize diff output
- #7: Make mount command output pretty and human readable format
- #8: Command short cuts to save time
- #9: Create a new set of commands
- #10: Set vim as default
- #11: Control output of networking tool called ping
- #12: Show open ports
- #13: Wakeup sleeping servers
- #14: Control firewall (iptables) output
- #15: Debug web server / cdn problems with curl
- #16: Add safety nets
- #17: Update Debian Linux server
- #18: Update RHEL / CentOS / Fedora Linux server
- #19: Tune sudo and su
- #20: Pass halt/reboot via sudo
- #21: Control web servers
- #22: Alias into our backup stuff
- #23: Desktop specific – play avi/mp3 files on demand
- #24: Set default interfaces for sys admin related commands
- #25: Get system memory, cpu usage, and gpu memory info quickly
- #26: Control Home Router
- #27 Resume wget by default
- #28 Use different browser for testing website
- #29: A note about ssh alias
- #30: It’s your turn to share…
- Conclusion
- Conclusion
- How to set an alias in a macOS or Linux shell
- How to set an alias in a UNIX environment, including Bash, Zsh, Fish on both macOS and Linux
Make an Alias in Bash or Zsh Shell in macOS with Terminal
To make aliases of macOS Unix commands in your bash or zsh shell on macOS and earlier versions, it is done via your .bash_profile or .zshrc file which lives in your home account directory, if the file does not already exist, just create one.
Launch Terminal from the /Application/Utilities folder
Go to your home directory by just entering ‘cd’ followed by the ‘return’ key to enter the command:
List your home directory contents including invisible files to see if the file already exists, use:
Create the .bash_profile or .zshrc file using the command line program called ‘nano’ if it doesn’t exist, if it does exist you add your aliases to the end of the file. The commands below assume you are in your home directory:
If you are not in your home directory you would use a prefix of the tilde ‘
’ and forward slash, which is the home directory path:
When the .bash_profile or .zshrc file is created you are ready to enter your alias commands.
So here I am using the alias ‘l’ to alias the command ‘ls -lah’
In nano ‘control+o’ to write the file out and ‘control+x’ to exit the file.
Refresh the shell environment by entering the command below:
That’s it, now the alias will take effect.
To add other aliases just start a new line, and apply the same formatting.
Источник
30 Handy Bash Shell Aliases For Linux / Unix / MacOS
A n bash shell alias is nothing but the shortcut to commands. The alias command allows the user to launch any command or group of commands (including options and filenames) by entering a single word. Use alias command to display a list of all defined aliases. You can add user-defined aliases to
/.bashrc file. You can cut down typing time with these aliases, work smartly, and increase productivity at the command prompt.
This post shows how to create and use aliases including 30 practical examples of bash shell aliases.
More about bash shell aliases
The general syntax for the alias command for the bash shell is as follows:
How to list bash aliases
Type the following alias command:
alias
Sample outputs:
By default alias command shows a list of aliases that are defined for the current user.
How to define or create a bash shell alias
To create the alias use the following syntax:
In this example, create the alias c for the commonly used clear command, which clears the screen, by typing the following command and then pressing the ENTER key:
Then, to clear the screen, instead of typing clear, you would only have to type the letter ‘c’ and press the [ENTER] key:
How to disable a bash alias temporarily
How to delete/remove a bash alias
You need to use the command called unalias to remove aliases. Its syntax is as follows:
In this example, remove the alias c which was created in an earlier example:
You also need to delete the alias from the
/.bashrc file using a text editor (see next section).
How to make bash shell aliases permanent
The alias c remains in effect only during the current login session. Once you logs out or reboot the system the alias c will be gone. To avoid this problem, add alias to your
The alias c for the current user can be made permanent by entering the following line:
Save and close the file. System-wide aliases (i.e. aliases for all users) can be put in the /etc/bashrc file. Please note that the alias command is built into a various shells including ksh, tcsh/csh, ash, bash and others.
A note about privileged access
You can add code as follows in
A note about os specific aliases
You can add code as follows in
30 bash shell aliases examples
You can define various types aliases as follows to save time and increase productivity.
#1: Control ls command output
The ls command lists directory contents and you can colorize the output:
#2: Control cd command behavior
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
#3: Control grep command output
grep command is a command-line utility for searching plain-text files for lines matching a regular expression:
#4: Start calculator with math support
#4: Generate sha1 digest
#5: Create parent directories on demand
mkdir command is used to create a directory:
#6: Colorize diff output
You can compare files line by line using diff and use a tool called colordiff to colorize diff output:
#7: Make mount command output pretty and human readable format
#8: Command short cuts to save time
#9: Create a new set of commands
#10: Set vim as default
#11: Control output of networking tool called ping
#12: Show open ports
Use netstat command to quickly list all TCP/UDP port on the server:
#13: Wakeup sleeping servers
Wake-on-LAN (WOL) is an Ethernet networking standard that allows a server to be turned on by a network message. You can quickly wakeup nas devices and server using the following aliases:
#14: Control firewall (iptables) output
Netfilter is a host-based firewall for Linux operating systems. It is included as part of the Linux distribution and it is activated by default. This post list most common iptables solutions required by a new Linux user to secure his or her Linux operating system from intruders.
#15: Debug web server / cdn problems with curl
#16: Add safety nets
#17: Update Debian Linux server
apt-get command is used for installing packages over the internet (ftp or http). You can also upgrade all packages in a single operations:
#18: Update RHEL / CentOS / Fedora Linux server
yum command is a package management tool for RHEL / CentOS / Fedora Linux and friends:
#19: Tune sudo and su
#20: Pass halt/reboot via sudo
shutdown command bring the Linux / Unix system down:
#21: Control web servers
#22: Alias into our backup stuff
#23: Desktop specific – play avi/mp3 files on demand
#24: Set default interfaces for sys admin related commands
vnstat is console-based network traffic monitor. dnstop is console tool to analyze DNS traffic. tcptrack and iftop commands displays information about TCP/UDP connections it sees on a network interface and display bandwidth usage on an interface by host respectively.
#25: Get system memory, cpu usage, and gpu memory info quickly
#26: Control Home Router
The curl command can be used to reboot Linksys routers.
#27 Resume wget by default
The GNU Wget is a free utility for non-interactive download of files from the Web. It supports HTTP, HTTPS, and FTP protocols, and it can resume downloads too:
#28 Use different browser for testing website
#29: A note about ssh alias
Do not create ssh alias, instead use
/.ssh/config OpenSSH SSH client configuration files. It offers more option. An example:
You can now connect to peer1 using the following syntax:
$ ssh server10
#30: It’s your turn to share…
Conclusion
This post summarizes several types of uses for *nix bash aliases:
- Setting default options for a command (e.g. set eth0 as default option for ethtool command via alias ethtool=’ethtool eth0′ ).
- Correcting typos (cd.. will act as cd .. via alias cd..=’cd ..’ ).
- Reducing the amount of typing.
- Setting the default path of a command that exists in several versions on a system (e.g. GNU/grep is located at /usr/local/bin/grep and Unix grep is located at /bin/grep. To use GNU grep use alias grep=’/usr/local/bin/grep’ ).
- Adding the safety nets to Unix by making commands interactive by setting default options. (e.g. rm, mv, and other commands).
- Compatibility by creating commands for older operating systems such as MS-DOS or other Unix like operating systems (e.g. alias del=rm ).
I’ve shared my aliases that I used over the years to reduce the need for repetitive command line typing. If you know and use any other bash/ksh/csh aliases that can reduce typing, share below in the comments.
Conclusion
I hope you enjoyed my collection of bash shell aliases. See
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Nice list; found a couple new things I never thought of. To return the favor; my addon..
A nice shell is key in bash imo; I color code my next line based on previous commands return code..
And I liked your . <1,2,3,4>mapping; how I integrated it…
And two random quick short ones..
The following is my version of the “up function” I came up with this morning:
Show text file without comment (#) lines (Nice alias for /etc files which have tons of comments like /etc/squid.conf)
@linuxnetzer, nocommand is nice to dump squid, httpd and many others config files.
@mchris, I liked cp alias that can show progress.
Appreciate your comments.
Ctrl+L is also a nice quick way to clear the terminal.
Hi!
This isn’t an alias, but for clear screen is very handy the CTRL+L xDD
Источник
How to set an alias in a macOS or Linux shell
How to set an alias in a UNIX environment, including Bash, Zsh, Fish on both macOS and Linux
Published Oct 24 2019
Sorry, no Windows instructions as I don’t use Windows. Use Google if that’s the OS you use.
I found a funny post on Reddit that highlights how we, as developers, can lose productivity over the small things, like typos:
I don’t have this set up, as I mostly use a GUI for Git (GitHub Desktop). But I use other aliases. Most notably ll instead of ls -al .
How do I set up an alias in the terminal?
I said macOS in the title, but this works also on Linux of course.
Here’s the syntax:
Here’s the example I mentioned above:
This works in Bash, Zsh, Fish shell and others too.
If you write this in your shell, from now on ll will be a new available command in the console.
Note: this alias is going to be valid for the entire session, which means until you close the shell, or you restart the computer, whatever comes first.
To persist the aliase, so you can use them any time in the future, you need to add it to the configuration file for your shell.
If you use Bash, that’s the .bash_profile in your home folder. It’s an invisible file, so you might need to open it with the terminal rather than the Finder:
always points to your home folder path
(assuming you have Visual Studio Code installed, which provides the code command).
/.bash_profile does not exist, you can create it, and the shell will pick it up. You can also use the
/.bashrc file, if present.
For licensing purposes, macOS Catalina comes with a new shell by default, called Zsh. In this case, the configuration file is in the
Источник