Last Updated : 11 Jul, 2025
With commands, users can communicate with the operating system via the Linux terminal, a handy tool. Although the terminal has several pre-defined commands, you can improve productivity and optimize processes by writing your custom commands. This article explains how to write and use custom commands in the Linux terminal to personalize and enhance your experience.
What Are Custom Commands?User-defined scripts or shortcuts that carry out particular actions in the terminal are called custom commands. They can automate tedious chores, simplify complicated command sequences, or add functionality to existing commands. Users can increase their overall efficiency and save time when using the terminal by writing custom commands.
Creating Custom Command for Linux Terminal 1. Making Use of AliasesThe easiest method for creating custom commands in Linux is to use aliases. With them, you can create a shortcut for an already-existing combash: cd: Videos: No such file or directory
ubuntu $mand or set of commands. This is how an alias is made:
Command: alias shortcut_name='command_to_run'
For example, if you frequently use ls -la
To list files in detail, you can create an alias like this:
Command: alias ll='ls -la'Custom Command
Add this alias to your shell configuration file (e.g., ~/.bash_aliases or ~/.bashrc) to make it permanent. Run source ~/.bashrc after making modifications to the file.
2. Creating Programs in ShellShell scripts can be used to construct more intricate custom commands. A shell script is a file that the terminal can run that contains a list of commands. This is how to make one:
1. Create a New Script File: To make a new script file, use a text editor. As an illustration:
Command: nano myscript.sh
2. Add Shebang Line: At the top of the file, add the shebang line to specify the script interpreter:
Command: #!/bin/bash
3. Add Commands: Write the commands you want to execute in the script. For example:
Command:
#!/bin/bash
echo "Hello, World!"
date
4. Make the Script Executable: Save the file and exit the editor. Then, make the script executable with the following command:
Command: chmod +x myscript.sh
5. Run the Script: Execute the script by typing:
Command: ./myscript.sh3. Making Use of Functions
You can also write custom commands inside your shell using functions. They are very handy when generating more complex instructions with parameters. How to build a function is as follows:
1. Open Your Shell Configuration File: Edit your ~/.bashrc
or ~/.bash_profile
.
2. Define the Function: The syntax for creating a function is as follows:
my_function() {
command1
command2
}
For example, to create a function that backs up a directory, you could write:
backup() {
tar -czf "$1_backup.tar.gz" "$1"
}
3. Save and Source the File: Save the changes and run source
~/.bashrc
to apply them.
4. Use the Function: You can now use your custom function like this:
backup my_directoryCustomizing Your Terminal Experience 1. Developing Unique Git Commands
If you use Git regularly, you can write custom commands to make repetitive activities easier. For instance, you could write a function that logs in with a single command and checks the status:
Command:2. Combining Multiple Commands
git_status_log() {
git status
git log --oneline
}
You can combine multiple commands into a single custom command. For instance, if you often navigate to a directory and list its contents, you can create a function:
Command:3. Using Custom Commands in Scripts
go_and_list() {
cd "$1" && ls -la
}
You can also call your custom commands or scripts within other scripts. This allows you to build complex workflows by combining various commands and scripts.
Custom Commands for Linux Terminal ConclusionDeveloping personalized commands for the Linux terminal can significantly increase your efficiency and improve your enjoyment of using the command line. Shell scripts, functions, and aliases can help you automate repetitive activities and improve processes. As you gain more experience with custom commands, there are a plethora of methods to customize the terminal to meet your unique requirements, which will ultimately increase your task management efficiency.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4