How to create a custom user command in Linux quickly ?

Today we will check how to create a custom user command in Linux easily.

Lets see the procedure step by step.

The easiest way is to create a function and then we can directly call it like a command in bash shell.

1. Create the function in /etc/profile file for all users, or in ~/.bashrc file for your login id.
I have created a function called details in my .bashrc file so that it is available only to my login shell.

### Creating a function named details to serve as command
[root@ngelinux bash_completion.d]# tail ~/.bashrc 
details ()
{
      echo "You are working in `pwd` directory with `ls -l| grep -v ^total | wc -l` files"
}
[root@ngelinux bash_completion.d]# 

2. Now you can run the command like below and TAB auto completion will work by default.

### Try to run details command.
[root@ngelinux ~]# details 
You are working in /root directory with 60 files

### Switch directory and then run details command.
[root@ngelinux ~]# cd /var/log/
[root@ngelinux log]# details 
You are working in /var/log directory with 56 files
[root@ngelinux log]# 

If you want for all users, simply add the function to /etc/profile file.

Similar to details function, you can create any command to perform any task as defined in the function.

Leave a Reply

avatar
  Subscribe  
Notify of