Linux How to add custom commands

eye-catch Other techs

It’s necessary to resize pictures while writing an article for my blog.

I installed imagemagick and used the following command to resize all the pictures in the directory.

mkdir resize
cp *.jpg resize/
mogrify -resize "25%" -quality "80%" *.jpg

I don’t want to run these 3 commands every time I need to do it.

Sponsored links

Create a new directory for the commands

Let’s create a specific directory for the commands.

$ sudo mkdir /usr/local/my_commands
Sponsored links

Add the path to $PATH

Then, edit the profile to add the path.

$ nano /etc/profile

Use your preferred editor.

Add the following line to the end of the file.

export PATH=$PATH:/usr/local/my_commands

If you want to add the commands only for a specific user, add the same content to ~/.bashrc file.

Add a new command

The next step is to create our own command. I want to resize pictures with one command, I create resize command.

$ sudo nano resize

Then, add your own commands there. I added the following.

mkdir -p resize
cp *.jpg resize/
mogrify -resize "25%" -quality "80%" *.jpg

Add the execution right

No execution right is added to the file by default.

$ ls -l
total 4
-rw-r--r-- 1 root root 76 Dec 27 09:16 resize

No one can execute the command at the moment. So let’s add it to the file.

$ sudo chmod +x resize 
$ ls -l
total 4
-rwxr-xr-x 1 root root 76 Dec 27 09:16 resize

Reloading the profile

If you want to execute the command without logout/login, the profile needs to be reloaded.

$ source /etc/profile

After this, the command can be executed by any user.

Comments

Copied title and URL