Linux Tutorial with some important commands for Linux
Important Shortcuts:
- Open Terminal or Press: Ctrl + Alt + T
- Lock the Terminal: Ctrl + S
- Unlock the terminal: Ctrl + Q
Linux Commands:
- pwd : Present working Directory
- exit : exit from terminal
-
cd <Directory_name> : Changes the directory
- cd .. : bring the user above one directory
- cd / : Navigates to the root directory
- cd /home/user/Desktop : Navigates directly to Desktop
-
ls : List files
- ls <directory_name> : List content of a particular directory
- ls / : content of root directory
- ls -l : : It will show the list in a long list format
- ls -a : : Enlist the whole list of the current directory including the hidden files
- date : Displays current date
-
cal : Display Calender of current month
- cal <month> : (eg cal aug) Display calender of the given month
- cal <year> :(eg cal 2024)- Display the full calendar of the given year
- cal <month> <year> : (eg. cal aug 2024) - Display calender of the given month and year
- pwd : Present Working Directory
- bc : The calculator [crtl +d to exit]
- man command : Manual for the given command
-
Shutdown
- shutdown : schedules a shutdown
- shutdown -c : Cancels a scheduled shutdown
- shutdown now : Shuts down immediately
- shutdown -r : Reboots the system
File & Folder Operations:
- mkdir <directory name>: creates a new directory
- rmdir <directory name>: removes a directory
- rm <filename>: Removes the given file
- rm -r: removes a directory recursively
- rm * : deletes all files in the present working directory
- rm -r *: deletes all files and folders recursively from the present working directory
- cp filename location: copy file to another location
- mv filename location: Move a file to another location
- mv filename newfilename: rename a file
- echo Text > filename : Creates a new file and writes the text in the file
- echo text : writes the line of text in the terminal
- cat filename: Reads the content from a file and displays on the terminal
- wc filename: shows the line / word / character count of the given file
More File system commands:
- lp <filename>: Prints a file and returns the job ID
- cancel <job id>: Cancels an ongoing print job
- df : Displays available disk space
- gzip <filename> : Compress a file with .gz extension
- gunzip <gz file> : Uncompress a file with .gz extension
- tar -zcvf <new file .tar.gz> <foldername>: Zip a folder
- tar -xvzf filename.tar.gz Unzip a tar file
Advanced File System Commands:
- Grep <searchword> <filename>: Search a text in the given file
- /pat: Search a pattern
- touch <filename>: change timestamp of a file
- chmod <permission> <filename>: Changes file permission [777]
File Permissions:
Read permission- 4
Write permission- 2
Execute permission- 1 - 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 - chmod o+x,g-w <filename>:update multiple permissions
- chown <username> <filename>: Change ownership of file
- 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
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:
- Command mode
- Insert mode
- Last line mode
- 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:
- i :Insert text
- a : Append text at cursor position
- A : Append text at the end of line
- o :Opens a new line below and enters insert mode
- O :Opens new line above and enters insert mode
- dd :Deletes a line
- R :Replace more than one character
- r :Replace one single character
- s :Replace one single character with multiple characters
- S : Replace the entire line irrespective of cursor position
Navigation Commands
- h (or Backspace) : Moves cursor left
- l (or Spacebar) : Moves cursor right
- k :Moves cursor up
- j : Moves cursor down
- 5l : moves 5 characters right [Similarly can move any number of characters in all direction]
- 0 (Zero) :Moves to beginning of a line
- $ :Moves to end of a line
Editing Commands
- x :Deletes character from cursor position
- X : Deletes character to the left of cursor
- J : Joins current line with next line
- D :Deletes from cursor to end of line
- yy :Yanks current line (Copy)
- p :puts the deleted or copied text
- P :puts the deleted or copied text before cursor position
- u :Undo last editing instruction
- U :Undo all changes made in the current line
Splitting window
- :sp :Splits window and both windows contains the same file
- :new :Splits window. The new window is not associated with any file
- Ctrl+w :Switch between windows
- :e :file Opens / Creates a file
- :q :kills the current window
- :on :Make the current window, only window of the screen
Last Line commands:
- :w : Saves the text in file
- :x :Saves file and quits editing mode
- :wq :Saves file and quits editing mode
- :q :Quits editing mode when no changes are made to file
- :q! :Quits editing mode discarding changes
- :w [filename] : Save a new file
- :w >> [filename] : appends the content to another file
- Ctrl +z :Stops current session and escapes to unix shell
System Administration
- sudo <command> : Runs command in super user mode. Enter username and password when it prompts
- su: Enters super user mode (Denoted by #)
- passwd : change password of current user. You can enter a new password.
- passwd <username> : Prompts appear for password of a specific user user
- logout : logout from current user
- who: Display the available users in a system
- who am i:Displays the current user
- uptime :Displays how long system is running
- adduser <username> <options>:Creates a new user
- userdel <username>:
- apt list -installed:Displays list of installed applications / packages
- apt-get update:Updates all the packages
- apt install <package>:Installs a new package [Internet connection is required]
- apt purge <package>:Deletes a Package and associated configuration files
- apt remove <package>:Deletes only the package
Network Administration
- ifconfig: [apt install net-tools] : Interface Configuration Displays Connectivity information of the system
- ping <domain name or ip>: (Packet Internet Groper)Transfers data packets to check network connectivity
eg: ping google.com
ping 194.245.10.56 - wget <exact file path>: Downloads a file from internet and stores in present working directory
- hostname: Displays the hostname of your system.
- Netstat: Network Statics. Displays all open sockets
- dig <domain name>: Display information about a domain
- nslookup <domain name>: Same as dig. Older version of dig
- iwconfig: Provides wireless configuration