Linux Commands: Your Journey to Mastering the Terminal

Linux Commands: Your Journey to Mastering the Terminal

The terminal is the heart of Linux, a place where commands hold the power to transform complex tasks into simple actions. Whether you’re organizing files, monitoring system performance, or automating processes, Linux commands are your ultimate toolkit. In this blog, we’ll guide you step by step through the most essential and practical commands to help you harness the full potential of Linux.

  1. cd → change directory

  2. pwd → Present working directory

  3. id → logged in user information

  4. sudo -i → switch to root user (/root)

  5. mkdir → To create a new directory

  6. rmdir → To remove folder

  7. rmdir folder-* → To delete all folders

  8. ls → listing all files and directories in current location

  9. exit → logged out from current user

  10. sudo su → switch to root user with ubuntu home directory (/home/ubuntu)

  11. touch → To create a new empty file

  12. cat → To check file content

  13. vim → To edit or create a new file
    # vim filename
    Shift + i
    Insert the data
    # esc →> :wq

  14. :set nu → It will give temp numbers to each line in vim editor

  15. rm -rf → To remover file

  16. cd / → top level directory in linux

  17. ls -a → list all files (hidden & non hidden)

  18. ls -l → long listing

  19. date → To check the timing of VM

  20. ls -lrt

    l-> long format

    r -› reverse order

    t →> sort by time

  21. ls -lrtah → It will give hidden files(a) in human reading format(h)

  22. clear → terminal clear

  23. cp → copy file from one location to another location cp folder1/myfile /folder2

  24. mv → to move file from one location to another mv folder1/myfile folder2/

  25. mkdir -p folder-1/folder-2/folder-3 → create folder under another folder

  26. cd . → same directory

  27. cd .. → goes back to one folder

  28. cd ../.. → goes back to two folders

  29. cd ~ → /root

  30. cd → /root

  31. echo "welcome to Vaishali’s blog" → prints message on console

    Here different way to print on console

    • echo class7 class9

    • echo welcome to Vaishali’s blog

    • echo 'welcome to Vaishali’s blog'

    • echo '''welcome to Vaishali’s blog'''

  32. echo "I am adding 1 line" › myfile → it will add data to myfile but it will delete previous data

  33. echo "I am adding 2nd line" >> myfile → It will add data to myfile without deleting previous data

  34. cd var/logs —head auth.log → It will display top 10 logs of auth file

  35. tail auth.log → It will display bottom 10 logs of auth file

  36. tail -15 auth.log → It will gave bottom/recent 15 logs from auth file.

  37. head -n 7 filename | tail -n 4 → from number 1 to 10 you need print middle numbers 4,5,6,7 use this command.

    Here first print head 7 is 1,2,3,4,5,6,7 then tail 4 will get last 4 numbers 4,5,6,7 .

  38. cat auth.log | grep “sudo” → grep is used to search particular word.

    grep will filter sudo in auth file and displays.

  39. cat auth.log | grep -i “Status” → -i will ignore case sensitivity of letters.

  40. cat auth.log | grep -i “Status” | grep “linux” → you can use more than one grep to find particular line

  41. more auth.log → just like load more of Facebook. Press ctrl+c to get out

  42. less auth.log → it shows content in less.

    Press q to get out

  43. man → it will give detail description of commands manually.

  44. man ls> ls.txtcat ls.txt | grep “lrt” → it will apply filter on man commands

  45. ifconfig → to check ip address on linux

  46. hostname -i → it is also used to check ip address on linux

  47. traceroute www.google.com → It gives domain’s output logs

  48. tracepath www.google.com → It also give domains output logs

  49. ping www.google.com → to check response from website. It works on icmp port 1.

  50. ping -c 5 www.google.com → to check 5response from website.

  51. netstat -a → to get port information

  52. dig www.google.com → to get domain’s information

  53. nslookup www.google.com → it is mostly used to get domain’s information

  54. apt(Advanced Package Tool) → it is use to install package on Linux. apt install nginx

  55. ps -aux | grep nginx → to get active services

    ps -aux :

    ps - process status

    a - All process

    u - With user

    X - daemons/executeapt

  56. update -y → to update your local repo

  57. apt list → List of all installed packages

  58. apt install nginx → to install packages

  59. apt show nginx → to get all information of package

  60. apt info nginx → it also used to get all information about package

  61. apt remove nginx → to remove or delete package from local repo

  62. systemctl status nginx → To check status of package like active or inactive

  63. systemctl stop nginx → To stop service

  64. systemctl start nginx → To start service

  65. yum install nginx(Amazon Linux) → To install package on amazon linux

  66. cd /usr/share/nginx/html/ → html file location on Amazon linux server

  67. cd/var/www/html → html file location on ubuntu server

  68. cd /etc/nginx/ → It has nginx configuration file

  69. useradd Ram

    passwd Ram- give password → To add new user.

    When we add new user an individual group is created for that user. Mostly used

  70. adduser Ram → it is also used to create user but in small letters only ie ram.

    For capital Ram it gives error. Less used

  71. cat /etc/passwd → To check added user info

  72. groupadd Devops → To add new group

  73. cat /etc/group → To check user information

  74. chmod -g 1003 Ram → adding user to group. 1003 is group id

  75. su Ram → swich user to Ram

  76. groups Ram → to check groups of user

  77. Ram: x: 1001:1003::/home/Ram:/bin/sh → here 1001 is userId and 1003 is group Id

  78. cat /etc/passwd | grep “1003” → to check users associated with 1003 id

  79. userdel Ram → to delete user

  80. groupdel Devops → to delete group

  81. Mohan: x: 1001:1003::/ home/Mohan:/bin/sh

    awk -F: '$4 == "1003" {print $1}'

    $1 will print first value

    It will get output as Mohan

  82. **find . -name “*.txt”**→ to fina txt file in current directory

  83. locate file1 → to find file

  84. df -h → to get disk space in human readable format

  85. du -h → to get disk usage in human readable format

  86. kill -9 nginx → it will kill service forcefully

  87. wget → download file from internet

  88. cat /etc/os-release → get os information

  89. history → to get command history

  90. cut -d ' ' -f2 vaishali → it will delimit data from 2nd field

  91. cut -b 2 vaishali -> it will give 2nd alphabet

  92. echo class7 class9 | sed 's/class/jtp/g'. output→ jtp7/jtp9

  93. Good morning

    Welcome to Vaishali’s blog

    We are learning AWS/Devops Good

    bye take care

    cat file | sed 's/Good/Bad/g'

    Output : Bad morning

    Welcome to Vaishali’s blog Bad

    We are learning AWS/Devops

    bye take care

  94. sed -i 's/Good/Bad/g' file → -i will change in original file

  95. sed '1s/Bad/Good/' file → It won’t change 1st Good other will get change output -> Good Morning

    welcome to Vaishali’s blog Bad

    we are learning AWS/Devops

    bye take care, Bad night

  96. vim test — cat test | grep k8s — sed -i 's/k8s/Ram/' test — cat test | grep Ram —> It will replace k8s with Ram

  97. cat file —Bad morning

    Welcome to Vaishali’s blog Bad

    We are learning AWS/Devops

    bye take care, Good night

    wc file→it will give count of file. 4 lines, 16 words, 92 letters

    Output : 4 16 92 file

  98. wc -1 file → count of lines

  99. wc -w file → count of words

  100. top -n 1 | grep "load average" | cut -d " " -f11-18 —-> load average: 0.01, 0.04, 0.01 → it will give load average from top command.

    We can customise the desired output using this command

  101. awk '{print $13' info.txt → it will print first row in the table

  102. awk '{print $03' info.txt → print table as it is

  103. awk '{print $NF}' info.txt → print last row in the table

  104. awk '{print $NF}' info.txt | head -n 2 | tail -n 1→ print first value of last row

  105. awk '{print $NF}' info.txt | tail -n 1 → print last value of last row

  106. awk '{print 52}' info.txt l tail -n 2 | head -n 1 → print second last value from second row

  107. init 6 → restart machine ec2

  108. init 0 → stop machine ec2

  109. scp -i "batch-24.pem" anyfile ubuntu@ec2-54-174-136-191.compute-1.amazonaws. com: / tmp/ …anyfile → it will secure copy file from local to server

  110. chown ubuntu folder-1/ → make ubuntu as a owner of folder-1

  111. chgrp ubuntu folder-1/ → make ubuntu as a group of folder-1

  112. ln myfile hardlink_myfile → it will create exact copy of myfile.

if you make changes in original file, it will transfer automatically to the hardlink.

even if original file is deleted, hardlink copy will not delete

  1. ln -s myfile softlink_myfile -> it will create exact copy of myfile.

if you make changes in original file, it will transfer automatically to the softlink . Softlink will get delete when original file gets deleted

  1. tar -czvf file1.tar.gz file1 → making archive file of file1

  2. tar -xvf folder.tar.gz → unarchive folder

  • c: Creates Archive

  • x : Extract the archive

  • f: creates archive with given filename

  • t : displays or lists files in archived file

  • u : archives and adds to an existing archive file

  • v : Displays Verbose Information

  • A : Concatenates the archive files

  • z: zip, tells tar command that creates tar file using gzip

  • j: filter archive tar file using tbzip

  • W: Verify a archive file

  • r: update or add file or directory in already existed tar file