Basic Linux Commands

Basic Linux Commands

Day 3 of 90daysofdevops

Commonly used Linux Commands

  1. ls - List files and directories in the current directory.

  2. cd - Change directory. You can use cd followed by a directory name or a path to navigate to a different directory.

  3. pwd -Print the current working directory.

  4. uname - It shows the name of the kernel

  5. clear - It uses for clear screen

  6. whoami - It shows the current login user name

  7. date - It shows time and date

  8. mkdir - Create a new directory. You can specify the directory name as an argument.

  9. touch - Create an empty file or update the timestamp of an existing file. You can specify the file name as an argument.

  10. df - displays the amount of disk space available on the filesystem

  11. id - display the user ID (UID) and group ID (GID)

  12. cp - Copy files or directories. You need to specify the source and destination file/directory names.

    cp <option><source> destination
    
    #Options:
    # -r for recursive
    # -v for verbose
    # -f for forcefully
    
  13. mv - Move or rename files or directories. You can use it to both move and rename files/directories.

    mv <option><source> destination
    
    #Options:
    # -r for recursive
    # -v for verbose
    # -f for forcefully
    
  14. rm - Remove files or directories. Be careful when using this command, as it permanently deletes files/directories.

    rm <option><source> destination
    
    #Options:
    # -r for recursive
    # -v for verbose
    # -f for forcefully
    
  15. cat - Display the contents of a file on the terminal. It helps us to list, combine and write file content to standard output.

    #creates a new file
    cat > file1.txt 
    #copy the content of file1 to file2
    cat file1.txt > file2.txt
    
  16. grep - Search for a pattern in a file or a group of files.

    grep <options> <search pattern> <files>
    
    #Options:
    # -i -> Ignore case during search
    # -r, -R -> Search recursively
    # -v -> invert match, match everything except the pattern
    # -l -> List files that match the pattern
    # -L -> List files that do not match the pattern
    # -n -> Prefix each line of output with the line number
    
  17. chmod - Change the permissions of a file or directory. You can use it to set read, write, and execute permissions for different users.

    chmod <permission> <file/directory name>
    
    #Set permission with numeric value
    # r(read) = 4
    # w(write) = 2
    # x(execute) =1
    
  18. chown - Change the owner of the file or directory.

    chown <username> <file/directory name>
    
  19. sudo - Execute a command with administrative privileges. It's often used to perform tasks that require root access.

  20. man - Display the manual page for a command. It provides detailed documentation and usage instructions for various commands.

  21. history - Show a list of previously executed commands.

  22. top - display the top process running in the system

  23. wget - Download files from the internet using the command line. These are just a few basic Linux commands to get you started.

  24. useradd - To create a new user account

  25. passwd - To create user account password

  26. userdel - To delete user account

  27. groupadd - For add Group account

  28. groupdel - For delete group account

  29. find - find command used to search and locate list of files and directories based on conditions applied

    find <directory> -name <permission>
    
    find <directory> -perm <permission>
    
    find <directory> -user <user>
    
    find <directory> -size -10M
    
  30. locate - search a file by file name in the database, whereas the find command searches in the file system

    find <file_name>
    
  31. awk - searches files for text containing a pattern

    awk '{action}' your_file_name.txt
    
    #search for text that has a specific pattern
    awk '/regex pattern/{action}' your_file_name.txt
    
    #Built in Variable
    # NR: Keeps a current count of the number of input records. 
    # NF: Keeps a count of the number of fields within the current input record. 
    # FS: Contains the field separator character which is used to divide fields on the input line.  
    # RS: Stores the current record separator character. 
    # OFS: Stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. 
    # ORS: Stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character.
    
  32. wc - the wc(word count) is use for the count and line numbers

    #count number of lines
    wc -l <directory>  
    #count number of words
    wc -w <directory>
    
  33. head - Head command is used for to display top line of the file

    head <directory>
    
    head -n count <directory>
    
  34. tail - Tail command is used for to display the bottom line of the file

    tail<directory>
    
    tail -n count <directory>
    
  35. tar - tar is used to compress size or drive backup

    tar -<options> <files>
    
    #Options:
    # c - for create
    # x - for extract
    # v - for verbose
    # f - for forcefully
    # t - for test
    # z - for gzip
    # j - for bz2
    # J - for xz
    # C - for specific destination
    
  36. crontab - used for to execute job multiple time

    #for set cron jobs
    crontab -e 
    #for show cron jobs of current user
    crontab -l 
    # removing a existing cronjob
    crontab -r
    
  37. sudo - sudo allows us to run a program as a different user. this command runs through root privilege.

  38. ip addr - for showing ip address

  39. cal - display the current month's calendar with the current date highlighted

  40. vim - vim editor used for editing a file

    # Insert mode -> i
    # Quit -> q
    # Save & Exit -> wq
    

Task:

  1. Linux command to view what’s written in a file.

    ~ cat

  2. Linux command to change the access permissions of files.

    ~ chmod

  3. Linux command to check which commands you have run till now.

    ~ history

  4. Linux command to remove a directory/ Folder.

    ~ rmdir

  5. To create a fruits.txt file and to view the content.

    ~ vim fruits.txt

  6. Add content in devops.txt (One in each line) — Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

    ~ vim devops.txt and add fruits name one by one.

  7. To show only the top three fruits from the file

    ~ cat devops.txt | head -3

  8. To Show only the bottom three fruits from the file.

    ~ cat devops.txt | tail -3

  9. To create another file Colors.txt and to view the content. Add content in Colors.txt (One in each line) — Red, Pink, White, Black, Blue, Orange, Purple, Grey

    ~ vim Colors.txt and add colors names one by one and cat Colors.txt to view the content

  10. To find the difference between fruits.txt and Colors.txt files.

    ~ diff fruits.txt colors.txt

Thank You,

Abhisek Moharana