Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
How to Make Linux Terminal look Awesome
Next article icon

How to Make Custom Commands for Linux Terminal?

Last Updated : 14 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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-command
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 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
Screenshot-2024-10-10-103752

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
Screenshot-2024-10-10-103849

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-Terminal
Custom Commands for Linux Terminal

Conclusion

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.


Next Article
How to Make Linux Terminal look Awesome

N

nikhiljain17
Improve
Article Tags :
  • TechTips
  • Linux-Unix
  • linux
  • How To

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 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 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 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
  • How to execute commands remotely using SSH in Linux?
    Many times users need to work in remote systems. For which they have to log in to the remote server, execute certain commands and come out of that session. Is it possible to perform all these actions locally? Yes, it's possible using ssh client. In this article, we will see different ways of running
    2 min read
  • How to Open Terminal in Linux?
    The Shell or Command Prompt are common name for the Linux Terminal. The terminal was designed as a Linux File Browser, and this function is still carried out using it. You may traverse your files and undo changes made by using the Terminal as a File Browser. However, the very first step will be to O
    4 min read
  • How to Install Jmespath-terminal on Linux?
    JMESPath is known as JSON document manipulation expression language. It is a programming language that allows you to create a JMESPath expression that, when applied to an input JSON document, generates an output JSON document depending on the expression you gave. Experimenting with JMESPath expressi
    2 min read
  • How to Display Random ASCII Art on Linux Terminal?
    Here we are going to see how to print the drawing on the terminal in the form of ASCII characters. ASCII-Art-Splash-Screen is a tool written in python that pulls a random drawing from a repository and shows it every time you open the terminal. This works on UNIX-based systems like Linux and macOS.
    2 min read
  • How to Install and Use Alacritty Terminal Emulator in Linux?
    Alacritty is a cross-platform terminal emulator with GPU acceleration. It's a terminal emulator for OpenGL written in the Rust programming language. Alacritty is the fastest and lightest Terminal emulator in nature, thanks to its OpenGL renderer and high throughput parser. Alacritty Terminal emulato
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences