Linux - Command Line

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
Search for a command to run...

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
No comments yet. Be the first to comment.
1. Difference between Docker and Kubernetes Docker → Builds and runs containers.Kubernetes → Orchestrates containers across multiple nodes. Key points: Docker = container runtime. Kubernetes = container orchestration tool. Kubernetes provides auto...
In this session, we learn how to monitor a Kubernetes cluster using Prometheus and Grafana.This is not just theory — there is a GitHub repository containing all installation commands and demo steps.The repo will also be enhanced later with advanced K...
1. What is a ConfigMap in Kubernetes? A ConfigMap is used to store non-sensitive configuration data that your application needs — such as: Database port Connection type Any general configuration values In normal applications (non-Kubernetes), de...
Kubernetes normally supports built-in resources like: Deployment Service Pod ConfigMap Secret Ingress These are called native resources. Sometimes companies (Istio, ArgoCD, Prometheus Operator, Kyverno, etc.) want to add new features that Kub...
1. Why Kubernetes Services Are Needed When a Pod is created in Kubernetes, it receives a dynamic IP address.If the Pod dies and restarts, its IP changes.So other Pods (like checkout → payments) cannot rely on Pod IP because it changes, creating issue...
The Linux command line is a powerful tool for interacting with the operating system, automating tasks, and performing a wide range of system operations. Mastering it can make you a more efficient and capable Linux user or system administrator. In this blog, we’ll dive into the essential commands and concepts to help you master the Linux command line.
Let’s start by learning how to navigate the Linux filesystem.
pwd – Print Working DirectoryDisplays the current directory you're in.
pwd

ls – List FilesLists the contents of a directory.
ls # List files in the current directory
ls -l # Detailed list (including permissions and file sizes)
ls -a # List hidden files

cd – Change DirectoryMoves you to another directory.
cd /path/to/directory # Change to a specific directory
cd ~ # Change to home directory
cd .. # Move up one directory

Managing files and directories is fundamental to working in the Linux command line.
touch – Create an Empty FileCreates a new, empty file.
touch filename.txt

cp – Copy Files and DirectoriesCopies files or directories from one location to another.
cp file1.txt file2.txt # Copy a file
cp -r /source/directory /target # Copy a directory recursively

mv – Move or Rename FilesMoves or renames files and directories.
mv file.txt /new/path/ # Move file to another directory
mv oldname.txt newname.txt # Rename a file

rm – Remove Files and DirectoriesDeletes files or directories.
rm file.txt # Delete a file
rm -r /path/to/dir # Delete a directory and its contents recursively

mkdir – Create DirectoryCreates a new directory.
mkdir new_directory # Create new directory
mkdir -p parent_dir/child_dir # Create parent and child directories at once

Understanding and managing file permissions and ownership is key to maintaining security and proper access control on your Linux system.
ls -l:ls -l filename

This command displays file permissions, ownership, and other details. Permissions are shown as a string like -rwxr-xr--, where:
r stands for read
w stands for write
x stands for execute
Using chmod Numeric Mode :
7 (rwx): Read, write, and execute
5 (r-x): Read and execute
0 (---): No permissions
chmod 755 file

Using chmodSymbolic Mode:
Use the following notations with the chmod command:
u: User (the file owner)
g: Group (the group that owns the file)
o: Others (everyone else)
chmod u+x file # Add execute permission for the other user

chown:chown user:group file
The chown command changes the owner and group of a file or directory. For example, chown ubuntu:dev file changes the owner to ubuntu and the group to dev.

chgrp group file

chgrp command changes the group ownership of a file or directory.Linux allows the creation of multiple users, each with specific roles and permissions.
adduser: Create a new user.sudo adduser username

passwd: Set or change a user’s password.
su: Switch user.
Managing Groups:
groupadd: Creates a new group (e.g., groupadd groupname).
usermod: Modifies user properties, including group membership (e.g., usermod -aG groupname username).

Viewing Users and Groups:
cat /etc/passwd: Lists all users.
cat/etc/group: Lists all groups.
Managing processes is essential for system stability and performance. Some useful process management commands are:
ps: (process status) command displays information about active processes
top: Provides a dynamic, real-time view of system processes.
htop: An improved version of top with a better interface.
top and htop: Both tools display real-time CPU usage.
mpstat: Provides detailed CPU usage statistics.

kill: Terminates a process by its ID (PID).kill -9 PID
df: Shows disk space usage for mounted file systems in a human-readable format.
df -h

Linux provides powerful tools to interact with networks, making it a go-to platform for system administration.
ifconfig – Network Interface ConfigurationDisplays the network interface configuration (useful for checking IP addresses).
ifconfig

ping – Test Network ConnectivitySends packets to a network address to test connectivity.
ping google.com

netstat – Network StatisticsShows network connections, routing tables, and interface statistics.
netstat -tuln # Show listening ports

Package management systems like apt (Debian/Ubuntu) or yum (RHEL/CentOS) allow for easy installation, updating, and removal of software packages.
apt-get – Manage Packages in Ubuntu/DebianInstalls, updates, or removes software packages.
sudo apt-get update # Update package list
sudo apt-get upgrade # Upgrade all installed packages
sudo apt-get install package_name # Install a package
sudo apt-get remove package_name # Remove a package
yum – Manage Packages in CentOS/RHELSimilar to apt-get but used in Red Hat-based distributions.
sudo yum install package_name
Linux offers several tools for processing text files, making it easy to manipulate, search, and transform data.
cat – Display File ContentsDisplays the content of a file.
cat file.txt

grep – Search TextSearches for patterns in a file or output.
grep 'pattern' file.txt # Search for a pattern in a file
grep -r 'pattern' /dir # Search recursively in a directory

Text editors allow you to view or edit files from the terminal. Popular choices include:
Nano: Simple and user-friendly.
nano, use the following command:nano filename

Editing Text: Simply type to insert text. Use arrow keys to navigate the file.
Saving Changes: Press Ctrl + O (write out), then press Enter to save the file.
Exiting nano: Press Ctrl + X. If you have unsaved changes, you will be prompted to save them.
Cutting and Pasting Text:
Cut: Press Ctrl + K to cut a line.
Paste: Press Ctrl + U to paste the cut text.
Vi: More advanced, with powerful features for text manipulation.
vi, use:vi filename

Modes in vi:
Normal Mode: Default mode for navigation and commands. Press Esc to enter normal mode.
Insert Mode: Used for editing text. Enter by pressing i.
Command Mode: Used for executing commands. Enter by pressing :.
Editing Text:
Enter Insert Mode: Press i (insert before the cursor) or a (append after the cursor).
Exit Insert Mode: Press Esc.
Saving Changes:
Save and Exit: Type :wq and press Enter.
Save Without Exiting: Type :w and press Enter.
Exit Without Saving: Type :q! and press Enter.
Cutting and Pasting Text:
Cut Line: In normal mode, press dd.
Paste Line: Press p after cutting.
Shell scripting allows you to automate repetitive tasks by writing a series of commands in a script file.
Create a script:
nano script.sh
Write your script:
#!/bin/bash
echo "Hello, Linux World!"

Make it executable:
chmod +x script.sh

Run the script:
./script.sh

Mastering the Linux command line is a crucial skill for system administrators, developers, and DevOps professionals. With the commands covered here, you’re well on your way to becoming proficient with the Linux terminal. Once you get comfortable, you can start automating tasks, processing large amounts of data, and managing systems with ease.