Command Line
Terminal_commands

Hi there, Welcome to Cli commands by Chronicles. This blog contains a few commonly used CLI commands and is meant for beginners new to CLI.
This guide is a starting point for your journey in CLI.

All of the following commands use the following syntax: File/Directory Name -mode

In case of Multiple -modes stated last one will be executed.

General GNU Linux Commands

  1. mkdir- It is used to create a directory. The first directory will be treated as root directory.

  2. cd- It is used to change the control from one directory to another directory.

  To Move out of a directory use the following commands.
  $ cd ../-Use to move out of sub directory.
  $ cd \-Use to move out of multiple sub directories.
  1. touch-It is used to create a file.
For eg. $ touch Test.txt- It will create an empty text file with name Test.
  1. cat- It is used in 3 ways.

(i). To create a file. The file will be opened in append mode.

$cat >Test.txt

To save press Ctrl+D

(ii). To view content of a file.

$cat Test.txt

(iii). To merge the file and Copy the content of one file to another.

$cat A.txt B.txt>Test.txt
  1. ls-To view number of directories and files in a directory. $ ls

  2. ls*- To view files of a specific format.

$ls* .txt - will search the files of format .txt .
  1. pwd- It is used to view the current working directory. $pwd

  2. cp - It is used to copy one file or directory with all its contents from one directory to another.

   $cp file.txt ../A- To copy file in same directory.
  1. mv - It is used to move one file or directory from one directory to another.
    $ mv file.txt ../A - To move file from one in the same directory.
  1. head - It is used to print first or more lines in a file. $ head file.txt

  2. tail - It is used to print last or more lines in a file. $ tail file.txt

  3. tac - It is used to print in reverse order. $ tac file.txt

  4. more- It allows us to see large amounts of data at once. $ more file.txt

  5. id - It is used to display the id of group and user.

    $id
  6. clear - It is used to clear the screen.

    $clear
  7. vi - Opens Vi text editor to write the programs of text.

    $vi
  8. grep - Filter to search the given pattern in the file content

Syntax: $grep 'string to be searched' File name
For eg. $grep 'I' C.txt

  1. diff - It is used to compare the result of two different files. For eg.

    $diff A.txt C.txt 
  2. ping - It is used to check the connectivity status of the server. It returns the number of packets sent, recieverd and round trip times.

    $ping google.com
  3. history - It is used to view all the previous exectured commands in the terminal.

    $history
  4. hostname - It displays the host name.

    $hostname
  5. hostname -i - It displays host ip.

    $hostname -i
  6. chmod - It is used to change the user/group permissions to access file.

    Syntax: $chmod u=r/w/x file name

    For eg.

    $chmod u=r A.txt- This will change the files permission to read only.
  7. nl - It is used to display the line numbers of a file.

    $nl A.txt
  8. wc - It gives the Lines, Words and Characters respectively in the available file content.

    $wc A.txt
  9. uniq - It is used to remove duplicate content in a file. It can only remove continuous duplicate content.

    $uniq A.txt
  10. rmdir - It is used to remove specified directory. The directory must be empty.

    $rmdir Test
  11. rm - It is used to remove a file.

    $ rm C.txt
  12. cat /etc/shells - It allows user to view all the files present in /bin directory. $cat /etc/shells It returns the following directories. image

  13. which - It allows the user to view the the path to a specific file. For eg. $which bash returns the path of the file bash.

    image

  14. ./ - It is used to execute a file.

Vi Text Editor

The Vi Text editor allows us to view and modify a file using special Vim commands.

The following are basic commands used in a Vim Text Editor.

  1. :q- To exit Vim.

  2. :! - To override previous commands.

  3. :wq - To write and quit.

Following are the commands that were used to create a bash script and edited using VI editor.

  1. To execute a text file as a bash script include #!/bin/bash . It allows the script to be executed as a bash script.

  2. To make a variable in bash script use the following syntax. $variable name= any value Sometimes we use $declare to create a variable. For eg. $declare Test=100.

  3. echo - It is used to print on the screen. $echo Hello. This will print hello on the screen.

  4. To change the color of the output we use, echo -e "\033[0;31m TEXT". Depending upon the system used the color will be different in Fedora37, it will print TEXT in RED color.

Enviorment Variables are in Uppercase characters. Following is the list of some variables and their meanings, echo is used to print these variables.

  1. $SHELL - It will print the path of Shell.

  2. $HOME - It will print the path of root directory.

  3. $OSTYPE - It will print the type of Operating system such as linux-gnu.

  4. $PATH - It will print the local path.

  5. $$ - It will print the PID(Process ID).

  6. $PPID - It will print the parent process id.

  7. $HOSTNAME - It will print the hostname such as fedora.

  8. $UID - It will print the User ID.

Here is a sample BASH script which was created using Vi editor and prints Enviorment Variables.

image

Execution using terminal:

  1. Change it's permission to make it an executable file.

    image

    image

  2. Execute using ./ command.

    image