Linux Shell : Basic Commands and Scripting

This tutorial is intended to folks who are new to world of Linux. Like Command prompt in Windows, Linux have terminal where you can enter commands for your basic to advanced task. Terminal in Linux is very powerful tool. In this article we will be discussing some very basic commands and then forming a shell script with those commands.

You can open up Terminal from menu of your Linux Disrto. You can press Alt+F2 and type gnome-terminal to open up terminal. Now we will discuss some of the commands one by one.

cd <dir name> This command is used for changing the directory.

pwd This command prints working directory.

mkdir <dir name> This command is used for creating a directory

ls To list the file and directories inside a directory.

touch <file name> To create an empty file.

echo Hi This command will print Hi on terminal.

rmdir <dir name> Removes an empty directory.

rm <file name> Removes a file or directory.

These are some of basic commands which you will need to get started. There are a lot of many more commands which we come across while moving ahead with Linux. That we will discuss when we come around. Now we will move toward writing a script. A script is basically a file in which all commands are stored. It can reduce your effort of typing same commands again and again if you have to do same task lot many times. But if we talk about actual power of script it is far more powerful than above mentioned. It can perform looping, decision making and much more.

In this article we are discussing script as a basic bunch of commands. In forthcoming articles we will discuss more aspects of shell programming. Lets start. Open up terminal. Type

vi my_first_script.sh

and enter following commands. Instead of vi you can use your favorite text editor.

 

echo This is my first Shell Script
echo $USER
mkdir ./temp
echo temp directory is created
touch textfile.txt
echo textfile.txt is created.
mkdir ./temp/doc ./temp/doc/music ./temp/doc/pics
echo doc dir is created.
echo Displaying created Directory struct
tree ./temp
rm –rf ./temp/doc
echo Now removing doc dir struct
echo New dir tree
tree ./temp
rm -rf ./temp
echo temp dir is removed

 

After entering all the commands press Esc and :wq to save and quit from vi. After creating shell script we need to change its permissions to make it executable. For doing so type

chmod +x ./my_first_script.sh

on terminal. Now we can execute this script by typing its path on terminal. If you are currently in the same directory where the script is, then type

./my_first_script.sh

to execute the script. If everything goes as mentioned here you should be presented with following output on terminal.

script outputI have already explained much of the commands used in this script. In second line $USER is a system variable. The command echo is printing value of the variable. We can also use variables in our scripts. Variable names are preceded by $. The command tree presents directory structure in tree form. Note that tree command may not be available as default in some distributions of Linux. All other commands used in script are already explained.