Showing posts with label Shell. Show all posts
Showing posts with label Shell. Show all posts

10 Commonly use command in Shell/Bash Scripting

Below listed 10 commands will help you with shell scripting.

1. cat :

This command will show all the file content.
This can be used to read content of zip files too only by adding required prefix to "cat" command.

Example: You want to read content of file1.txt and  file2.zip.

>> cat file1.txt

>> zcat file2.zip

2. cd :

This command will change directory.
This can also be used to return back to home directory.

Example: If you want to go to  directory1. Once you reach directory one you can just type cd and it will take back to home directory.

>> cd /directory1

3. mkdir :

This command creates new directory.

Example: You want to create new directory name D1.

>> mkdir D1

4. cp/mv : 

Command cp/mv will create copy of file to another directory. However, when we use mv it will delete the original file.

Example: You want to copy file1 to location /new while only move file2 to location /new.

>> cp file1 /new

>> mv file2 /new

5. rm :

This will remove/delete file and directory.

Example: You want to remove file1.

>> rm file1

6. grep:

This command will help to search specific word in file.
Please click on below link to find more about grep command.
Example: You want to search echo in file1

>> cat file1 | grep "echo"

7. ls:

This command will get the list of available file & directory in a folder.

8. tail :

This command is used to print the last/recent lines from the file. N is the user input which states number of lines and need to be provided along with command.

Example - You want to see recent/last 5 lines of new created file1.

>> tail -5 file1.

9. less :

This command allows user to only view the file. "shift+G" to read last line and "G" to read first line of the opened file.

10. chmod :

This command is used to modify the access/permission of a user

Learn to write your first shell script...

Shell scripting is writing a series of command for the shell to execute. 

You can use ssh application name "putty".

If you have windows PC then use power shell application or cmd to launch .sh files.

You can use simple notepad or notepad++ to write these scripts

 

Steps to create your first Shell Script !!

  1. Create a file using a vi editor. Name script file with extension .sh (if using putty)
  2. Start the script with #! /bin/sh (if using notepad start from #2)
  3. Write below mentioned code.
  4. Save the script file as filename.sh
  5. For executing the script type bash filename.sh

Once script is saved. Run it as ./filename.sh and you can see below output.


Cheers to you on learning your first shell script !!!