Basic Linux Commands

                                    Linux

 

Introduction

  • Linux was developed by LINUS TORVALDS
  • Birth of Linux is on 1969 during the development of 'C' programming language

Various Linux Distributions:

  • Red hat Enterprise Linux
  • Fedora
  • Debian
  • Ubuntu
  • Linux mint
  • Cent OS
  • Open SUSE/ SUSE Linux enterprise 

Linux Features:

  • Simplified updates for all installed software
    Eg: sudo yum update java
  • Free software licensing
  • Access to source code
  • Multiple Distributers
  • Better Malware protection

Working with Directories:

  • Linux provides a CLI (Command Line Interface) to communicate with OS
  • CLI is better for tasks which cannot be performed with the GUI

 

Si.No

Command

Explanation

1.

pwd

Present working directory- displays the current working directory of the terminal

2.

/

Root directory (Documents to Desktop)
cd Documents/
cd ../Desktop/

3.

echo

Command that writes its arguments to standard output
Eg 1: echo “Hi”
Hi

Eg 2: x=5

echo $x

5

4.

clear

used to clear the terminal screen, contents will not be deleted but scrolled down
short cut key: ctrl+L

5.

su

Used to switch to the root users (so that super user permissions can be used to execute commands)

6.

su username

Used to switch to a different user

7.

sudo

Executes only that command with root/super user privileges 

 

ls command:

Ø ls [options] [file directory]

Si.No

Commands

Explanations

1.

ls

Lists all the contents in the current working directory

2.

ls path

By specifying the path after ls, the content in that path will be displayed

3.

ls -l

Displays all the details in long format.
owner setting and time stand and permission
eg format: permission username hostname memory size date year 

4.

ls -a

Lists all the hidden contents in the specified directory

5.

ls -author

Lists the contents in the specified directory along with its owner
eg format: permission author size timestamp filename

6.

ls -S

Sorts and lists all the contents in the specified directory by size
ls –laS (combination) – to store the results in the file or text
ls –laS /home/Desktop > file1.txt

7.

ls -la

Provides all the details with the hidden contents of the files with the extended long format

8.

ls *.html

Lists only the contents in the directory of a particular format

 

cd command:

Ø cd [directory] -> change directory

Si.No

Commands

Explanation

1.

cd

Changes the directory to the home page

2.

cd ~

This also changes the directory to the home directory (~ -> root)

3.

cd /

Changes the directory to the root directory

4.

cd ..

Changes the directory to its parent directory

5.

cd ‘xx yy’

If the file name is separated by comma and that needs to open/change, we can use (‘ ’) single quotes or (“ ”) double quotes
eg: cd ‘file test’

 

Cat command:

Ø Cat [options] file1 [file2] -> concatenate

Si.No

Commands

Explanation

1.

cat

Used to display the content of text files and concatenate several files into one.
eg: cat file1.txt

2.

cat > filename

Used to create a new file and can able to write and save it in that particular file name
cat > file2.txt
hi, hello
welcome

3.

cat file1 file2

Used to combine and display the two or multiple number of files
eg: cat file1.txt file2.txt

4.

cat file1 >> file2

Used to concatenate the contents of two different files into a one single file
eg: cat file1.txt >> file2.txt

5.

cat –b filename

Used to add line numbers to non-blank lines
eg: cat –b file2.txt
1. Hi
2.Hhello
3. Welcome 

6.

cat –n filename

Used to add line number to all the lines (including blank lines)

7.

cat –s filename

Used to squeeze blank lines into one line.
eg: cat –s file2.txt

8.

cat –E filename

Show $at the end of the line
eg: cat –E file2.txt

 

grep command:

  • We use ‘grep’ command to search for a particular string/word in a text file
  • This is similar to ‘ctrl+F’, but executed via CLI
  • grep “word_to_search” filename

Si.No

Command

Explanation

1.

grep “options” file.txt

Returns results for matching string options

2.

grep –i “options” file.txt

Returns results for case sensitive strings

3.

grep –n “options” file.txt

Returns the matching strings along with their line number
eg: grep –ni “options” file.txt

4.

grep –v “options” file.txt

Returns the result of lines not matching the search string
eg: grep –vi “options” file.txt

5.

grep –c “options” file.txt

Returns the number of lines in which the results matched search string
eg: grep –ci “options” file.txt

 

Sort command:

  • We use the ‘sort’ command to sort the results of a search either automatically or numerically
  • Files, file contents and directories can be sorted.
  • Sort: under the many lines it will sort all the lines

Si.No

Command

Explanation

1.

Sort file.txt

Sort the contents of file.txt and returns them in alphabetical order

2.

Sort file1.txt file2.txt

Sorts the contents of both file1.txt and file2.txt

3.

Sort –r file1.txt

‘r’ flag returns in reverse order

4.

Sort –f file1.txt

‘f’ flag does case insensitive sorting

5.

Sort –n file1.txt

‘n’ flag returns as per the numerical order
alphabets at first and numbers at the last
eg: sort –nr file1.txt (for reverse sorting with)

 

‘|’ pipe command:

Ø The ‘|’ command as known as ‘pipe’ command is used to output the result of one command as input to another command

Ø ‘|’ are used to perform two operations in the same command

Si.No

Command

Explanation

1.

grep “am” file1.txt file2.txt | sort

Searches for string “am” from both files and sort the results

2.

grep “am” file1.txt file2.txt | sort -r

Sorts the results in reverse order

3.

grep –i “am” file.txt | sort

With case insensitive sort with the char “am”

4.

grep –ni “am” file.txt | sort –r

With the line number it will displays the result, if we provide ‘-r’ flag it will displays it in reverse order

 

cp command:

Ø cp [options] source destination

Ø Copy Documents to Desktop:

Ø Enter this in the destination location(Desktop)

Ø cp /home/Documents/abc.txt .

Si.No

Command

Explanation

1.

cp

Used to copy files and directories

2.

cp -i

Enters interactive mode, CLI asks before overwriting files

3.

cp -n

Does not overwrite the files

4.

cp -u

Updates the destination file only when source file is different from destination file

5.

cp -R

Recursive copy for copying directories

6.

cp -V

Verbose; prints informative messages

 

mv command:

Ø mv [options] source destination

Si.No

Command

Explanation

1.

mv

Used to move files and directories

2.

Mv -i

Enter interactive mode; CLI asks before overwriting files

3.

Mv -u

Updates the destination files only when source file is different from destination file

4.

Mv -v

Verbose, prints source and destination files

Examples:

·       Music$ mv /home/sukanya/pictures/*.txt .

·       Documents$ cp *.txt /home/sukanya/Music/

·       Music$ mv *.txt /home/sukanya/pictures/

mkdir command:

Ø mkdir directory (path)

Si.No

Command

Explanation

1.

mkdir folder_name

Used to create a new directory
Eg: mkdir folder/

2.

mkdir –p or mkdir --parents

Creates both a new directory and a sub directory
Eg: mkdir –p folder2/folder3/folder4

3.

mkdir –p folder1/{f2,f3,f4}

Used to create multiple subdirectories inside the new parent directory
Eg: mkdir –p f2/{fold2,fold3,fold4}

 

rm and rmdir command:

Ø rmdir folder_name

Ø rm filename/folder_name

Si.No

Command

Explanation

1.

rmdir

Used to remove the specified directory

2.

rmdir -p

Removes both the parent and child directory

3.

rmdir -pv

Removes all the parent and sub directories along with the verbose(explanation)

Eg: rmdir –p folder2/folder3/folder4
mkdir –pv folder2/folder3/

rmdir –pv folder2/folder3/

4.

rm –r

Removes even non-empty directory

5.

rm -rp

Removes non-empty directory including parent and sub directories

6.

rm -rv

Verbose (with explanation of each step)

 

 

awk command:

1.   awk operations:

o   Scans a file line by line

o   Splits each input line into fields

o   Compares input line/fields to pattern

o   Performs action on matched lines

2.   Useful for:

o   Transform data files

o   Produce formatted reports

3.   Programming constructs:

o   Formal output lines

o   Arithmetic and string operations

o   Conditionals and loops

Syntax:

Ø awk [options] ‘selection_criteria {action}’ input_file > output_file

Si.No

Command

Explanation

1.

awk ‘{print}’ employee.txt

Prints all the data from the file

2.

Awk ‘/keyword/{print}’ filename

Prints only the lines which has the keyword in the file

3.

Awk ‘{print $1,$4}’ filename

Splitting a line into fields
Eg: awk ‘{print $1,$4}’ man.txt

 

Examples:

Cat > ex.txt

A                B            C

Tarun        A12          1

Man           B6            2

Praveen     M42          3

 

1.    To print the first item along with the row number, separated with “-” from each line in ex.txt

a.     $awk ‘{print NR “-” $1}’ ex.txt

b.    o/p:

                                                             i.      1-Tarun

                                                          ii.      2-Man

                                                       iii.      3-Praveen

2.    To return the second row/item from the ex.txt

a.     $awk ‘{print $2}’ ex.txt

b.    o/p

                                                             i.      A12

                                                          ii.      B6

                                                       iii.      M42

3.    To print any non-empty line if present

a.     $awk ‘NF<0’ ex.txt

b.    o/p

                                                             i.      0

4.    To find the length of the longest line present in the file

a.     $awk ‘{if (length($0)>max)max=length($0)} END {print max}’ ex.txt

b.    o/p

                                                             i.      13

5.    To count the lines in a file

a.     $awk ‘END{print NR}’ ex.txt

b.    o/p

                                                             i.      3

6.    Printing lines with more than 10 charecters

a.     $awk ‘length($0)>10’ ex.txt

b.    o/p

                                                             i.      Tarun     A12    1

                                                          ii.      Praveen  M42   3

7.    To find/check for any string in any specific column

a.     $awk ‘{if($3==”B6”)print $0;}’ ex.txt

8.    To print the squares of first number from 1 to 6

a.     $awk ‘BEGIN{for (i=1;i<=0;i++) print “square of”, i ,”is”, i*i}’

b.    o/p

                                                             i.      Square of 1 is 1

                                                          ii.      Square of 2 is 4

                                                       iii.      Square of 3 is 9

                                                       iv.      Square of 4 is 16

                                                          v.      Square of 5 is 25

                                                       vi.      Square of 6 is 36

Sed command:

  • Ø Perform functions like searching, find and replace, insertion and deletion
  • Ø Supports regular expression which allows it perform complex pattern matching
  • Ø Work with chars
  • Ø Sed [options] filename

Si.No

Command

Explanation

1.

$sed ‘s/unix/linux’ filename

Replace or substituting string

2.

$sed ‘s/unix/linux/2’ filename

Replacing the nth occurrence of a pattern in a line

3.

$sed ‘s/unix/linux/g’ filename

Replacing all the occurrence of the pattern in a line (-temporary displays in the console window)

4.

$sed ‘s/unix//linux/3g’ filename

Replace form nth occurrence to all occurrence in a line

5.

$sed ‘s/\b open \b/ close/g’ filename

Replaces globally with the excel word

6.

$sed ‘s/unix//linux/g’ filename

Replaces only the line which starts with “unix”

7.

$sed ‘s/os.$/system./g’ filename

Replaces the word only the lines ends with “os.” To system

8.

$sed ‘/unix/d’ filename

Deletes the line which contains the word has unix

9.

$sed –i ‘s/unix/linux/g’ filename

Replaces the word permanently to the file

10.

$echo “Welcome To The Geek Stuff” | sed ‘s/\(\b[A-B]\)/\(\|\)/g’

Parenthesis first character of each word
o/p:
(W)elcome (T)o (T)he (G)eek (S)tuff

11.

$sed ‘3 s/unix/linux/’ filename

Replacing string on a specific line number

12.

$sed ‘s/unix/linux/p’ filename

Duplicating the replaced line with /p flag

13.

$sed –n ‘s/unix/linux/p’ filename

Printing only the replaced lines

14.

$sed ‘1,3 s/unix/linux/’ filename

Replacing string on a range of lines

15.

$sed ‘nd’ filename

 To delete a particular line say n in this eg
Eg: $sed ‘5d’ filename

16.

$sed ‘$d’ filename

To delete a last line

17.

$sed ‘x,y d’ filename

To delete line from range x to y
Eg: $sed ‘3,6d’ filename

18.

$sed ‘nth, $d’ filename

To delete nth to last line
Eg: $sed ’12,$d’ filename

19.

$sed ‘/pattern/d’ filename

To delete the pattern matching line
Eg: $sed ‘/abc/d’ filename

 

Cut command:

  • Ø Used to cutting out the sections
  • Ø cut [options] file

Example:

$cat state.txt

Andhra Pradesh

Arunachal Pradesh

Assam

Bihar

Chhattisgarh

1.    $cut –b 1,2,3 state.txt : To extract specific bytes (-b indicates byte)

a.     o/p

                                                             i.      And

                                                          ii.      Aru

                                                       iii.      Ass

                                                       iv.      Bih

                                                          v.      Chh

2.    $cut –b 1-3,5-7 state.txt : List with ranges

a.     o/p

                                                             i.      Andra

                                                          ii.      Aruach

                                                       iii.      Assm

                                                       iv.      Bihr

                                                          v.      Chhtti

3.    $cut –b 1- state.txt : 1- indicates from first bite to end byte of a line

a.     Andhra Pradesh

b.    Arunachal Pradesh

c.     Assam

d.    Bihar

e.     Chhattisgarh

4.    $cut –b -3 state.txt : -3 indicate 1st byte to 3rd byte of a line

a.     o/p

                                                             i.      And

                                                          ii.      Aru

                                                       iii.      Ass

                                                       iv.      Bih

                                                          v.      Chh

5.    $cut –c 2,5,7 state.txt

a.     o/p

                                                             i.      nr

                                                          ii.      rah

                                                       iii.      sm

                                                       iv.      ir

                                                          v.      hti

6.    $cut –c 1-7 state.txt

a.     o/p

                                                             i.      Andhra

                                                          ii.      Arunach

                                                       iii.      Assam

                                                       iv.      Bihar

                                                          v.      Chhatti

7.    $cut –c 1- state.txt

a.     o/p

                                                             i.      Andhra Pradesh

                                                          ii.      Arunachal Pradesh

                                                       iii.      Assam

                                                       iv.      Bihar

                                                          v.      Chhattisgarh

8.    $cut –f 1 state.txt : prints whole line

a.     o/p

                                                             i.      Andhra Pradesh

                                                          ii.      Arunachal Pradesh

                                                       iii.      Assam

                                                       iv.      Bihar

                                                          v.      Chhattisgarh

9.    $cut –d “ ” –f 1 state.txt

a.     o/p

                                                             i.      Andhra

                                                          ii.      Arunachal

                                                       iii.      Assam

                                                       iv.      Bihar

                                                          v.      Chhattisgarh

10.                                                                                                                                $cut -- complement –d “ ” –f 1 state.txt : deleted 1st word

a.     o/p

                                                             i.      Pradesh

                                                          ii.      Pradesh

                                                       iii.      Assam

                                                       iv.      Bihar

                                                          v.      Chhattisgarh

11.                                                                                                                                                                                                        $cut –complement –c 5 state.txt : cuts the 5th char of the given lines

a.     o/p

                                                             i.      Andha Pradesh

                                                          ii.      Arunchal Pradesh

                                                       iii.      Assa

                                                       iv.      Biha

                                                          v.      Chhatisgarh

Top command

  • Ø Lists of all the processes that running in the system
  • Ø $top

PID – Process ID

USER – username

PR – Priority

NI – Niceness value

VRT – Virtual memory

RES – Physical memory

SHR – Shared memory

S – status

%CPU – CPU time

%MEM – Physical memory time

TIME+ - Total CPU time

Command – command

User permissions:

File Permissions:

‘r’ -> read -> 4

 ‘w’-> write -> 2

 ‘x’ -> execute -> 1

File types:

‘-’ -> normal file

‘d’ -> directory

‘c’ -> characters special file

‘b’ -> binary special file

$ls –l

drwxrwxr-x

d -> directory

1st rwx -> user permission

2nd rwx -> group permission

3rd rwx -> other’s permission

To give permission for all the access its 777

4(r)+2(w)+1(x) = 7

chmod 777 folder or filename

eg: chmod 777 pictures/

chmod: to change the access permission of files and directories

chown: to change the owner of files and directories

chgrp: to change the group ownership of file and directories

 

Denotations:

‘u’ -> users

‘g’ -> groups

‘o’ -> others

‘a’ -> all

Chmod g=rw, o-x pictures/

  • ü Chmod g+wx filename: This gives the write and execute permission to group members
  • ü Chmod u=rwx,o-wx filename: This gives the read, write and execute and removes the write and execute ownership from other members
  • ü Chown username filename: Changes the owner of the specified file
  • ü Chown username: group_name filename: Changes both the owner and group ownership of the specified file
  • ü Chgrp group_name filename: Changes the group ownership of the specified file

 

 

Comments