Soumendra Saha

Software Developer

Qualification: Master in Computer Application (MCA)
Experience: 10 years
Call: 9679326307

Linux Tutorial with some important commands for Linux

Posted on Aug 20, 2023 by Soumendra Saha
Computer Fundamentals
DOS

Important Shortcuts:

  • Open Terminal or Press: Ctrl + Alt + T
  • Lock the Terminal: Ctrl + S
  • Unlock the terminal: Ctrl + Q

Linux Commands:

  1. pwd : Present working Directory
  2. exit : exit from terminal
  3. cd <Directory_name> : Changes the directory
    1. cd ..  : bring the user above one directory
    2. cd / : Navigates to the root directory
    3. cd /home/user/Desktop : Navigates directly to Desktop
  4. ls : List files
    1. ls <directory_name> : List content of a particular directory
    2. ls / : content of root directory
    3. ls -l : : It will show the list in a long list format
    4. ls -a : : Enlist the whole list of the current directory including the hidden files
  5. date : Displays current date
  6. cal : Display Calender of current month
    1. cal <month> : (eg cal aug) Display calender of the given month
    2. cal <year> :(eg cal 2024)- Display the full calendar of the given year
    3. cal <month> <year> : (eg. cal aug 2024) - Display calender of the given month and year
  7. pwd : Present Working Directory
  8. bc : The calculator [crtl +d to exit]
  9. man command : Manual for the given command
  10. Shutdown
    1. shutdown : schedules a   shutdown
    2. shutdown -c : Cancels a scheduled shutdown
    3. shutdown now : Shuts down immediately
    4. shutdown -r : Reboots the system

File & Folder Operations:

  1. mkdir <directory name>: creates a new directory
  2. rmdir <directory name>: removes a directory
  3. rm <filename>: Removes the given file
  4. rm -r: removes a directory recursively
  5. rm * : deletes all files in the present working directory
  6. rm -r *: deletes all files and folders recursively from the present working directory
  7. cp filename location: copy file to another location
  8. mv filename location: Move a file to another location
  9. mv filename newfilename: rename a file
  10. echo Text > filename : Creates a new file and writes the text in the file
  11. echo text : writes the line of text in the terminal
  12. cat filename: Reads the content from a file and displays on the terminal
  13. wc filename: shows the line / word / character count of the given file

More File system commands:

  1. lp <filename>: Prints a file and returns the job ID
  2. cancel <job id>: Cancels an ongoing print job
  3. df : Displays available disk space
  4. gzip <filename> : Compress a file with .gz extension
  5. gunzip <gz file> : Uncompress a file with .gz extension
  6. tar -zcvf <new file .tar.gz> <foldername>: Zip a folder
  7. tar -xvzf filename.tar.gz Unzip a tar file

Advanced File System Commands:

  1. Grep <searchword> <filename>: Search a text in the given file
  2. /pat: Search a pattern
  3. touch <filename>: change timestamp of a file
  4. chmod <permission> <filename>: Changes file permission [777]
    File Permissions:

    Read permission- 4
    Write permission- 2
    Execute permission- 1

  5. chmod o+x <filename> :assigns execute permission to others

    Abbreviations for chmod

    Category Operation Permission
    u- User + Assign permission r read permission
    g- group - Remove Permission w Write permission
    o- Others = Absolute permission x Execute
    a- All
  6. chmod o+x,g-w <filename>:update multiple permissions
  7. chown <username> <filename>: Change ownership of file
  8. find <path> <selection criteria> <action>: Finds a file in the given path

eg: find /home -name a.txt -print

eg. find /home -name '*.txt'           [We can use wild card character]

Note: Wildcard characters are those characters that can stand for unknown characters. In linux ? is used for single character and * is used for multiple characters

 

Redirecting Input, Output, Error

Standard output device is terminal, associated with the digit 1

> Performs overwriting of existing data
>> Performs appending to the existing data
Example: cat 1>output.txt

Standard error device is terminal, associated with the digit 2

Eg. cal adadasdasd 2>error.txt

Standard input device is keyboard, associated with the digit 0

< Performs input redirection
Eg: cat 0< a.txt

Redirecting output using pipe

Command > filename: the output of the command will be printed to a file

Example:

Ls -l >a.txt

/dev/null : All outputs are by default redirected here whose size always remains zero

 

Piping:

Output of a command will be input of another command using pipe. We can use pipe using vertical bar sign (|)

Example: ls | wc -w

ls -l | wc -l

$ cat file2.txt | sort  | uniq

In the above commands, the output of ls command will be input of wc command

The VI Editor:

Modes: 
  1. Command mode
  2. Insert mode
  3. Last line mode
Points to remember
  • Command mode to Insert Mode: Press I,o,a,r,s [Press esc to back to command mode]
  • Command mode to last line mode: Press : [Press enter to back to command mode]
  • Create new file with VI editor: $vi filename
  • Default mode is Command mode.
  • Display the current mode- :set showmode
  • Saving the text- :wq
Commands:
  1. i :Insert text
  2. a : Append text at cursor position
  3. A : Append text at the end of line
  4. o :Opens a new line below and enters insert mode
  5. O :Opens new line above and enters insert mode
  6. dd :Deletes a line
  7. R :Replace more than one character
  8. r :Replace one single character
  9. s :Replace one single character with multiple characters
  10. S : Replace the entire line irrespective of cursor position
Navigation Commands
  1. h (or Backspace) : Moves cursor left
  2. l (or Spacebar) : Moves cursor right
  3. k :Moves cursor up
  4. j : Moves cursor down
  5. 5l : moves 5 characters right [Similarly can move any number of characters in all direction]
  6. 0 (Zero) :Moves to beginning of a line
  7. $ :Moves to end of a line
Editing Commands
  1. x :Deletes character from cursor position
  2. X : Deletes character to the left of cursor
  3. J : Joins current line with next line
  4. D :Deletes from cursor to end of line
  5. yy :Yanks current line (Copy)
  6. p :puts the deleted or copied text
  7. P :puts the deleted or copied text before cursor position
  8. u :Undo last editing instruction
  9. U :Undo all changes made in the current line
Splitting window
  1. :sp :Splits window and both windows contains the same file
  2. :new :Splits window. The new window is not associated with any file
  3. Ctrl+w :Switch between windows
  4. :e :file Opens / Creates a file
  5. :q :kills the current window
  6. :on :Make the current window, only window of the screen
Last Line commands:
  1. :w : Saves the text in file
  2. :x :Saves file and quits editing mode
  3. :wq :Saves file and quits editing mode
  4. :q :Quits editing mode when no changes are made to file
  5. :q! :Quits editing mode discarding changes
  6. :w [filename] : Save a new file
  7. :w >> [filename] : appends the content to another file
  8. Ctrl +z :Stops current session and escapes to unix shell

System Administration

  1. sudo <command> : Runs command in super user mode. Enter username and password when it prompts
  2. su: Enters super user mode (Denoted by #)
  3. passwd : change password of current user. You can enter a new password.
  4. passwd <username> : Prompts appear for password of a specific user user
  5. logout : logout from current user
  6. who: Display the available users in a system
  7. who am i:Displays the current user
  8. uptime :Displays how long system is running
  9. adduser <username> <options>:Creates a new user
  10. userdel <username>:
  11. apt list -installed:Displays list of installed applications / packages
  12. apt-get update:Updates all the packages
  13. apt install <package>:Installs a new package [Internet connection is required]
  14. apt purge <package>:Deletes a Package and associated configuration files
  15. apt remove <package>:Deletes only the package

Network Administration

  1. ifconfig: [apt install net-tools] : Interface Configuration Displays Connectivity information of the system
  2. ping <domain name or ip>: (Packet Internet Groper)Transfers data packets to check network connectivity
    eg: ping google.com
    ping 194.245.10.56
  3. wget <exact file path>: Downloads a file from internet and stores in present working directory
  4. hostname: Displays the hostname of your system.
  5. Netstat: Network Statics. Displays all open sockets
  6. dig <domain name>: Display information about a domain
  7. nslookup <domain name>: Same as dig. Older version of dig
  8. iwconfig: Provides wireless configuration