How to Make Custom Commands for Linux Terminal?
Last Updated :
14 Oct, 2024
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 Aliases
The 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:
- Get your terminal open.
- To create an alias, use the syntax as follows:
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 CommandAdd 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 Shell
Shell 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.sh
3. 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_directory
Customizing 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:
git_status_log() {
git status
git log --oneline
}
2. Combining Multiple Commands
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:
go_and_list() {
cd "$1" && ls -la
}
3. Using Custom Commands in Scripts
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 TerminalConclusion
Developing 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.
Similar Reads
How to Make Linux Terminal look Awesome
In this article, we will install and configure some themes and plugins to tweak our Linux terminal for better productivity and a fancier look. These features include autocompletion, autosuggestion, command-line search, syntax highlighting, and better support for git and environment managers. Zsh The
3 min read
How to copy a file's content from Linux terminal?
This article shows the alternative method to copy the content of the file onto the clipboard, via the Linux terminal. In OSX, the commands pbcopy and pbpaste are available by default. Thus, to copy a file onto the clipboard via OSX terminal, type: pbcopy < 'path of the file' Since, in U
2 min read
How to Clear the Terminal History in Linux
The Linux Terminal stores every command you execute in a history file like .bash_history for Bash or .zsh_history for Zsh, making it convenient to access previous commands. However, this history can become a privacy or security issue, especially when dealing with sensitive tasks or working on shared
6 min read
How to Create Directory in Linux | mkdir Command
In Linux, the 'mkdir' command is like a magic wand for creating folders super easily. 'mkdir' stands for "make directory," and it helps you organize your computer stuff by creating folders with just one command. Whether you're making one folder or a bunch of them in a row, 'mkdir' is there to help y
7 min read
How to Make Script Executable in Linux | chmod Command
In Unix operating systems, the chmod command is used to change the access mode of a file. The name is an abbreviation of change mode. Which states that every file and directory has a set of permissions that control the permissions like who can read, write or execute the file. In this the permissions
7 min read
eDEX-UI Terminal for Windows, Mac and Linux
eDEX-UI is a fullscreen window, cross-platform terminal emulator, and system monitor which looks and feels like a sci-fi computer interface. Features of eDEX-UIIt has fully featured terminal emulator Tabs, colors, mouse events, and other features.It supports curses and curses-like applications.It al
4 min read
How to Customize Bash Colors and Content in Linux Terminal Prompt
If you are using the Linux operating system, that means you use the CLI most of the time. And do more work on the terminal. By default, most Linux Operating systems provide you the bash shell. Shell provides the interface between the user and kernel and executes commands. In this article, we are goi
9 min read
How to Customize Linux Terminal Using powerlevel10k?
Sometimes we get bored with the normal Linux Terminal and feel like if we can customize the same, so we can use powerlevel10k for that. It is a theme for ZSH. It changes normal shell commands to colorful commands. To install powerlevel10k we need to install Oh My Zsh, both are open-source in GitHub.
2 min read
How To Create Custom Theme for Windows Terminal?
Every operating system should have some sort of place where users can execute some commands. Commands are a very important element while using any operating system. Every technique of the operating system can't be known by all users. There might be some processes that need to be executed by the comm
4 min read
How to Compile and Run C program in Terminal?
To efficiently compile and run C programs, users typically utilize a compiler or the built-in terminal. The command prompt (CMD) on Windows can be employed to process C programs and generate the desired outputs. To create an executable C program, users must compile their C code using the system term
6 min read