Linux Basic Commands used in everyday activity
- Here are some useful shortcuts which you may use while working on terminal:
Cursor Movement Control:
Ctrl-a: Move cursor to the start of a line
Ctrl-e: Move cursor to the end of a line
Ctrl+Left/Right Arrow keys: Navigate word by word (may not work in all terminals)Modify Text:
Ctrl-w: Delete the whole word to the left of the cursor
Ctrl-y: Undo the deleted word
Ctrl-k: Erase to end of line
Ctrl-u: Erase till beginning of line from cursor position
Crtl-l: Clear the terminal
Crtl+t: swap the words
2. chown command : Change the owner of a file or directory
Parameters:
-R: Capital R here means to change ownership of all sub-directories if found, and you must use this parameter if you use the command against a directory
-f: means change ownership of all sub-directories as well as files also
$chown root:root DirName
$chown -R root:root DirName
$chown -Rf root:root DirName
3. chmod command: Change the permission of a file or directory
The mode which consists of 3 parts, owner, group, and others means what will be the permissions for these modes, and you must specify them.
The permission is one of the followings:
Read =4
Write = 2
Execute =1
Every permission represented by a number as shown, and you can combine permissions.
Parameters: Same as chmod command
Example:
$chmod 755 myfile
$chmod 755 mydir
That means set permission for the file named myfile as follows:
owner: set it to 7, which means 4+2+1 means read+write+execute.
group: set it to 5, which means 4+1 means read+execute.
other: set it to 5, which means 4+1 means read+execute.
Note: execute for a folder, means opening it.
4. Locate Command: To find a file in your system, the locate command will search the system for the pattern you provide.$locate myfilename
Updatedb Command: If file not find by locate command use updatedb command and run again locate command. For Example$updatedb
$locate myfilename
5. Tar Command: Combines several files into an archive and compression if you want.
Parameters
-c Create a new archive.
-z Compress the archive using gzip package.
-j Compress the archive using the bzip2 package.
-v Verbose mode means showing the processed files.
-f Write the output to a file and not to screen.
-x Unpack files from an archive.
For Create tar: $tar –czvf myfiles.tar.gz myfiles
For Extract tar: $tar -xzvf myfiles.tar.gz
6. Cat command: Display file content to screen without limits.
Example$cat filename
7. Less Command: Displays file content with a scroll screen so you can navigate between pages using PgUp, PgDn, Home, and End (same as more command)$less myfilename
8. Grep Command: Searches for a string in the specified files and displays which line contains the matched string
Parameters
-R Recursive search inside subdirectories if found.
-i Insensitive search and ignore case.
-l Displays file name, not the text lines.$grep -ir 'search_string' <path_of_search>
$grep -ir 'test' /home
$grep -ir 'red' . # . means search in current directory
9. du command : Calculates the disk usage of a file or a directory.
Parameters
-h Display human-readable form.
-s Summarize the output total size.$du -sh <Dir/file name>
$du -sh /home
$du -sh * #Display all files and Dir size of current Directory
10. df comand: The df command (short for disk free), is used to display information related to file systems about total space and available space.$df -h
11. List Command: List files and folders in the current directory.
Parameters
–l to list the content as a detailed list.
-a Display all files (hidden + non-hidden).
-s print the allocated size of each file, in blocks
-t sort by modification time, newest first
-h human readable
-r reverse order while sorting$ls -alhtr
12. cp command: Copy the source to target.
Parameters
-i Interactive mode means waiting for the confirmation if there are files on the target, it will be overwritten.
-r Recursive copy means include sub-directories if they found.
-p preserving same mode$ cp -ir sourcedir destination_dir
13. mv command : To rename a file or directory or Move the source to target and remove the source.
Parameters
-i Interactive mode means waiting for the confirmation if there are files on the target, it will be overwritten.$mv sourceFile targetFile
$mv oldFileName newFileName
14. rm command: Delete file or directory, and you must use –r in case you want to delete a directory.
Parameters
-r Recursive delete means delete all subdirectories if found.
-i Interactive means wait till confirmation
-f with out asking delete all file ad directories$rm -ir Dirname/
$rm -rf Dir
15. ssh command: SSH command is used to login into remote host. For example
Parametre:
-i : location of key on local machine
-p: port (If not working on default port)$ssh user_name@Remote_IP
$ssh -i /location/of/key user@ip
16. Find command: Find command used to search files, strings and directories.
Parametres:
-name: Name of File or dir
-type: d- directory f-file
-xargs: run command after successfull find
Example:$find <path_where_to_find> -type <f_for_file_and_d_for_dir> -name <name_of_fle_or_dir>
$find /home/user/ -type f -name abc.txt
$find /home/user/ -type d -name test
17. ps command: To check running process$ps -ef
18 Top command : top command displays processor activity of your system and also displays tasks managed by kernel in real-time. It’ll show processor and memory are being used. Use top command with ‘u‘ option this will display specific User process details as shown below. Press ‘O‘ (uppercase letter) to sort as per desired by you. Press ‘q‘ to quit from top screen.
19. Kill command : Use kill command to terminate process. First find process id with ps command as shown below and kill process with kill -9 command.
20 scp command: Copy data frpm remote machine to local machine and vice versa$scp source_file_name username@destination_host:destination_folder
Parameters:
-P — Specifies the remote host ssh port.
-p — Preserves files modification and access times.
-q — Use this option if you want to suppress the progress meter and non-error messages.
-C — This option forces scp to compresses the data as it is sent to the destination machine.
-r — This option tells scp to copy directories recursively
-i — scp with keys
. — use for current location$scp -i <path_of_key> -r user@ip:/home/dir/ /home
20 rsync command: Rsync, which stands for “remote sync”, is a remote and local file synchronization tool. It uses an algorithm that minimizes the amount of data copied by only moving the portions of files that have changed.
Parametres:
-a: archive mode
-r: recusively transfer data
-z: compress the data
-v: verbosity
-p: progress
--exclude: exclude file or dir$rsync -<parametres> <source> <Destination>
$rsync -arzvp /home/abc.tar /var/www/
$rsync -arzvP /home/abc.tar /var/www/
21. useradd command: To add new user with password
$useradd <username>-c "Full Name" -p <password>
$useradd test -c "fotr test user" -p testing
22. Enjoy!!!!