Skip to main content

Command Palette

Search for a command to run...

Day 3 :Linux command

Updated
7 min readView as Markdown
R

my self Rohit kumar yadav , I am tech learner . i am always curious about to learn new technology ,I have hard working capacity with smart work, i have good time management skill , and leadership. i have good experience in marketing industry.

Linux Commands: From Basics to Advanced

Whether you’re a beginner or an experienced DevOps engineer, mastering Linux commands is essential for effective system administration, automation, and troubleshooting. This guide covers Linux commands, starting from the basics and progressing to advanced topics.


1. Basic Linux Commands

These are fundamental commands every Linux user should know.

File and Directory Management

  • pwd: Displays the current directory (print working directory).

      pwd
    
  • ls: Lists files and directories in the current directory.

      ls
    
    • Options:

      • ls -l: Detailed list view (long format).

      • ls -a: Shows hidden files (files starting with a dot).

  • cd: Changes the current directory.

      cd /path/to/directory
    
  • cp: Copies files or directories.

      cp file.txt /backup/file.txt
    
  • mv: Moves or renames files or directories.

      mv oldfile.txt newfile.txt
    
  • rm: Deletes files or directories.

      rm file.txt
    
    • rm -r: Recursively deletes a directory and its contents.
  • mkdir: Creates a new directory.

      mkdir new_directory
    
  • rmdir: Removes an empty directory.

      rmdir empty_directory
    

2. File Viewing and Editing

These commands help in viewing or editing files.

  • cat: Displays the content of a file.

      cat file.txt
    
  • less: Allows scrolling through a file page by page.

      less file.txt
    
  • head: Shows the first few lines of a file.

      head file.txt
    
  • tail: Displays the last few lines of a file. Useful for reading logs.

      tail file.txt
    
    • tail -f file.log: Follows the log file in real-time.
  • nano: A simple text editor.

      nano file.txt
    
  • vim: A powerful text editor.

      vim file.txt
    

3. User and Group Management

Managing users and groups is crucial for system security and resource control.

  • adduser: Adds a new user.

      adduser username
    
  • usermod: Modifies user account settings.

      usermod -aG groupname username
    
  • passwd: Changes a user's password.

      passwd username
    
  • groups: Displays the groups a user belongs to.

      groups username
    
  • chown: Changes the ownership of a file or directory.

      chown user:group file.txt
    
  • chmod: Changes file or directory permissions.

      chmod 755 script.sh
    

4. Package Management

Installing, updating, and managing software is a core part of Linux system administration.

  • APT (for Debian/Ubuntu)

    • apt update: Updates the list of available packages.

        sudo apt update
      
    • apt upgrade: Installs the latest versions of all packages.

        sudo apt upgrade
      
    • apt install: Installs a package.

        sudo apt install nginx
      
  • YUM/DNF (for RedHat/CentOS)

    • yum update: Updates all installed packages.

        sudo yum update
      
    • yum install: Installs a package.

        sudo yum install httpd
      

5. Process Management

These commands help you monitor and manage running processes.

  • ps: Displays a snapshot of current processes.

      ps aux
    
  • top: Displays real-time system resource usage.

      top
    
  • htop: An enhanced version of top with a more interactive interface.

      htop
    
  • kill: Terminates a process by its PID (Process ID).

      kill 1234
    
  • killall: Terminates all processes with a specific name.

      killall firefox
    
  • nice and renice: Adjusts the priority of a running process.

      renice 10 1234
    

6. Disk and File System Management

Managing storage and file systems is crucial for maintaining healthy servers.

  • df: Shows disk space usage.

      df -h
    
  • du: Displays disk usage of files and directories.

      du -sh /path/to/directory
    
  • fdisk: A partition table manipulator.

      sudo fdisk /dev/sda
    
  • mount: Mounts a file system.

      sudo mount /dev/sdb1 /mnt/usb
    
  • umount: Unmounts a file system.

      sudo umount /mnt/usb
    
  • fsck: Checks and repairs file systems.

      sudo fsck /dev/sda1
    

7. Networking Commands

Linux provides powerful tools to manage and troubleshoot network configurations.

  • ifconfig: Displays network interface configuration.

      ifconfig
    
  • ip: Newer tool for network management, replacing ifconfig.

      ip addr show
    
  • ping: Tests connectivity to a network host.

      ping google.com
    
  • netstat: Displays network connections and routing tables.

      netstat -tuln
    
  • ss: Displays network connections, faster and more detailed than netstat.

      ss -tuln
    
  • scp: Securely copies files between systems.

      scp file.txt user@remote:/path/to/destination
    
  • ssh: Securely connects to a remote server via SSH.

      ssh user@remote_host
    

8. System Monitoring

Monitoring the system's health is critical for performance tuning and troubleshooting.

  • uptime: Displays how long the system has been running.

      uptime
    
  • vmstat: Reports virtual memory statistics.

      vmstat 5
    
  • iostat: Reports CPU and input/output statistics.

      iostat
    
  • sar: Collects and displays system activity information.

      sar -u 5
    
  • free: Displays memory usage.

      free -h
    

9. Advanced Linux Commands

Advanced Networking

  • tcpdump: Captures and analyzes network traffic.

      sudo tcpdump -i eth0
    
  • nmap: Network exploration tool and security scanner.

      sudo nmap -sP 192.168.1.0/24
    

Advanced File Manipulation

  • grep: Searches for patterns in files.

      grep "search_string" file.txt
    
  • awk: A text-processing language.

      awk '{print $1, $3}' file.txt
    
  • sed: A stream editor for modifying file content.

      sed 's/old_text/new_text/g' file.txt
    

System Performance and Debugging

  • strace: Traces system calls and signals.

      strace -p 1234
    
  • lsof: Lists open files and the processes that opened them.

      lsof /path/to/file
    
  • dmesg: Prints kernel ring buffer messages (useful for debugging hardware issues).

      dmesg
    

🚀 Today’s focus: mastering essential Linux commands 🖥️, but with a creative twist! Let’s turn the basics into powerful tools for problem-solving.

Task 1: View the content of a file and display line numbers.

Cat -n hello.txt

Task 2: Change the access permissions of files to make them readable, writable, and executable by the owner only.

Chmod 700 hello.txt

Task 3: Check the last 10 commands you have run.

history | tail -10

Task 4: Remove a directory and all its contents.

rmdir anjali (anjali is the directory/folder name here)

Task 5: Create a fruits.txt file, add content (one fruit per line), and display the content.

To Create file:- touch fruits.txt

To add content with Vim editor:- vim fruits.txt

To display content:- cat fruits.txt ( to display content with numbers add -n)

Task 6: Add content in devops.txt (one in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava. Then, append "Pineapple" to the end of the file.

echo “Pineapple” » devops.txt

Task 7: Show the first three fruits from the file in reverse order.

head -3 fruits.txt | tac

Task 8: Show the bottom three fruits from the file, and then sort them alphabetically.

tail -3 fruits.txt | sort

Task 9: Create another file Colors.txt, add content (one color per line), and display the content.

Method 1:-

To Create file:- touch colors.txt

To add content with Vim editor:- vim colors.txt

To display content:- cat colors.txt ( to display content with numbers add -n)

Method 2:-

echo -e “Red\nBlue\nBlack\nGreen\nYellow\nPink\nWhite\nGrey” > colors.txt

Task 10: Add content in Colors.txt (one in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey. Then, prepend "Yellow" to the beginning of the file.

echo -e “Yellow\n$(cat colors.txt)” > colors.txt

Task 11: Find and display the lines that are common between fruits.txt and Colors.txt.

comm -12 <(sort colors.txt) <(sort fruits.txt)

Task 12: Count the number of lines, words, and characters in both fruits.txt and Colors.txt.

wc fruits.txt colors.txt

Feeling more empowered with Linux now? 💪 Keep pushing your boundaries and exploring new ways to enhance your skills! Stay tuned for more exciting tasks ahead!

Conclusion

Mastering Linux commands, from basic to advanced, is a crucial skill for any system administrator or DevOps engineer. By understanding and regularly using these commands, you can efficiently manage and troubleshoot Linux systems