Blogs

Learn Basic Linux Commands

File operating  Linux Commands

ls: List files and directories in the current directory.

cd: Change the current directory.

pwd: Print the current working directory.

mkdir: Create a new directory.

rmdir: Remove an empty directory.

touch: Create an empty file or update the timestamp of an existing file.

cp: Copy files or directories.

mv: Move or rename files or directories.

rm: Remove files or directories.

cat: Display file content.

more and less: Display file content page by page.

head and tail: Display the beginning or end of a file.

grep: Search for a pattern in files.

find: Search for files and directories in a directory hierarchy.

chmod: Change file permissions.

chown: Change file ownership.

df: Display disk space usage.

du: Display file and directory space usage.

tar: Create and extract tar archives.

zip and unzip: Create and extract ZIP archives.

These are some of the fundamental Linux commands for working with files and directories. There are many more options and advanced commands available, so you can refer to the man pages (e.g., man ls, man cp, etc.) for detailed information on each command.

Process Operation Linux Commands

ps: List processes.

ps: List processes for the current user.

ps aux: List all processes with detailed information.

top: Display dynamic information about running processes.

top: Continuously updated list of processes and system information.

Press ‘q’ to exit top.

htop: Interactive process viewer.

htop: Provides a dynamic and interactive view of processes.

Install it if not available (sudo apt install htop or sudo yum install htop).

kill: Terminate processes.

kill process_id: Terminate a process by its ID.

kill -9 process_id: Forcefully terminate a process.

killall: Terminate processes by name.

killall process_name: Terminate all processes with the specified name.

pkill: Signal processes based on their name.

pkill -9 process_name: Send a signal to processes with a specific name.

pgrep: List processes by name.

pgrep process_name: List process IDs that match the specified name.

nohup: Run a command that persists even after you log out.

nohup command &: Run a command in the background with no hangup.

bg: Resume suspended processes in the background.

bg %job_number: Move a suspended job to the background.

fg: Bring background processes to the foreground.

fg %job_number: Bring a background job to the foreground.

jobs: List background jobs.

jobs: List background jobs and their statuses.

nice and renice: Set or adjust process priority.

nice -n 10 command: Start a process with a lower priority.

renice -n 10 -p process_id: Change the priority of a running process.

at and cron: Schedule tasks to run at specific times.

at: Execute commands at a specified time.

cron: Schedule recurring tasks.

systemctl: Control system services (systemd).

systemctl start service_name: Start a service.

systemctl stop service_name: Stop a service.

systemctl restart service_name: Restart a service.

systemctl status service_name: Check the status of a service.

ps aux | grep process_name: List processes that match a specific name.

Use this command to find and filter processes by name.

These are some of the essential Linux commands for process management. You can use them to monitor, control, and troubleshoot running processes on your Linux system.

Directory Operation Linux Command

ls: List files and directories in the current directory.

pwd: Print the current working directory.

cd: Change the current directory.

mkdir: Create a new directory.

rmdir: Remove an empty directory.

rm: Remove files or directories.

cp: Copy files or directories.

mv: Move or rename files or directories.

touch: Create an empty file or update the timestamp of an existing file.

find: Search for files and directories in a directory hierarchy.

du: Display file and directory space usage.

tree: Display directory structure in a tree-like format.

chown: Change file or directory ownership.

chmod: Change file or directory permissions.

ln: Create links (hard or symbolic) to files or directories.

pushd and popd: Change directories and maintain a directory stack.

basename: Extract the base name from a path.

dirname: Extract the directory name from a path.

realpath: Get the absolute path of a file or directory.

These are some of the commonly used Linux commands for directory operations. They allow you to navigate, create, modify, and manage directories and their contents efficiently from the command line.

File Permissions Linux Commands

chmod: Change file permissions.

chmod 755 file_name   # Example: Give read, write, and execute permission to the owner and read/execute permission to others.

chgrp: Change file group ownership.

ls -l: List files and directories with detailed permissions.

umask: Set default permissions for newly created files.

setfacl: Set Access Control Lists (ACLs) on files and directories.

getfacl: Get ACL information for files and directories.

These commands help you control who can access and manipulate files and directories on your Linux system. Understanding and using file permissions effectively is crucial for system security and data protection.

Networking Linux Commands

ifconfig or ip: Display or configure network interfaces.

ping: Send ICMP echo requests to a host to check network connectivity.

traceroute or tracepath: Trace the route that packets take to reach a destination.

netstat: Display network statistics, including open ports and active connections.

ss: A modern replacement for netstat to display socket statistics.

ifup and ifdown: Enable or disable network interfaces.

dhclient: Obtain an IP address from a DHCP server.

ifconfig: Configure network interfaces.

hostname: Display or set the system’s hostname.

nslookup or dig: Query DNS servers to resolve domain names to IP addresses.

route: Display or modify the kernel’s IP routing table.

iptables or firewalld: Configure the firewall rules.

sshd: Start or stop the SSH server.

netplan or ifcfg (depending on the Linux distribution): Configure network settings, especially for systems using systemd-networkd (netplan) or the older ifcfg-style network configuration files.

These are just a few of the many networking commands available in Linux. The specific commands and tools you use may vary depending on your distribution and networking needs. You can refer to the respective manual pages (man command_name) for detailed information on each command.

Archives and Compressions Linux Commands

tar: Create and extract tar archives.

gzip: Compress files using the gzip algorithm.

gunzip: Decompress gzip-compressed files.

bzip2: Compress files using the bzip2 algorithm.

bunzip2: Decompress bzip2-compressed files.

xz: Compress files using the xz algorithm.

unxz: Decompress xz-compressed files.

zip and unzip: Create and extract ZIP archives.

rar and unrar: Create and extract RAR archives (note that rar is not open source).

7z and 7za: Create and extract 7z archives.

tar.gz or tar.bz2: Create and extract tar archives with gzip or bzip2 compression in a single command.

These commands allow you to work with a variety of archive formats and compression algorithms, making it easier to manage and share files in a compressed and archived form.

 Disk Usage Linux Commands

df: Display disk space usage for mounted file system

du: Display disk usage for files and directories.

ncdu: An interactive utility for viewing disk usage statistics.

Is: List files and directories with their sizes.

ncurses-based file managers: Some file managers, like Midnight Commander (mc) or Ranger, provide disk usage information and directory navigation capabilities.

find: Search for large files on your system.

baobab: A graphical disk usage analyzer.

du -s: Display the total size of a directory (in blocks).

quota: Display user disk usage and set disk quotas.

These commands and tools help you monitor and manage disk space on your Linux system. You can use them to identify large files or directories that may be taking up unnecessary space and take action accordingly to free up disk space.

Text Processing Linux Commands

cat: Display the contents of a text file.

less and more: Display text files page by page.

head and tail: Display the beginning or end of a text file.

grep: Search for patterns in text files.

sed: Stream editor for text manipulation.

awk: A versatile text processing tool for data extraction and reporting.

cut: Remove sections from lines of files.

sort: Sort lines in a text file.

uniq: Display or filter repeated lines in a file.

tr: Translate or delete characters from a text stream.

wc: Count lines, words, and characters in a text file.

paste: Merge lines from multiple files.

join: Join lines from two files based on a common field.

comm: Compare two sorted files line by line.

sort | uniq: Combine the sort and uniq commands to find unique lines in a file.

awk ‘{print NR, $0}’: Number lines in a text file.

rev: Reverse lines character-wise.

cut -d ‘delimiter’ -f field_number: Extract fields from lines using a specific delimiter.

These Linux text processing commands are powerful tools for manipulating, searching, and analyzing text data efficiently from the command line. You can combine and use them creatively to meet various text processing needs.

Package Installation Linux Commands

Debian/Ubuntu (using APT):

apt-get: The traditional package management tool.

Update the package list and upgrade installed packages

Install a package

Remove a package (but keep its configuration files)

Remove a package and its configuration files

apt: A simplified package management tool.

Update the package list and upgrade installed packages

Fedora/RHEL/CentOS (using YUM or DNF)

yum (for RHEL/CentOS 6 and earlier)

Update the package list and upgrade installed packages

Remove a package

dnf (for RHEL/CentOS 8 and later, Fedora)

Update the package list and upgrade installed packages

Arch Linux (using Pacman)

pacman: The package manager for Arch Linux.

Update the package list and upgrade installed packages

zypper

Update the package list and upgrade installed packages

Generic (using source code or manual installation)

make: Build and install software from source code.

These are some of the most commonly used package installation commands for various Linux distributions. The actual command may vary based on your specific distribution and package management system. Always check your distribution’s documentation for the most accurate package management commands and practices.

Version Control Linux Commands

Git Commands:

git init: Initialize a new Git repository in the current directory.

git clone: Clone a remote Git repository to your local machine.

git add: Stage changes for commit.

git commit: Commit staged changes with a message.

git status: Check the status of your working directory and staged changes.

git log: View the commit history.

git pull: Fetch changes from a remote repository and merge them into the current branch.

git push: Push local commits to a remote repository.

git branch: List, create, or delete branches.

git checkout: Switch between branches or commit history.

git merge: Merge changes from one branch into another.

git stash: Temporarily save changes that are not ready to be committed.

git tag: Create, list, or delete tags.

git remote: List and manage remote repositories.

git reset: Unstage changes or reset to a previous commit.

git fetch: Download objects and refs from another repository.

git show: Show various types of objects (commits, tags, etc.).

git diff: Show changes between commits, branches, or working directory.

Environment Variables Linux Command

Print Environment Variables

Setting Environment Variables

Unsetting Environment Variables

Print Environment Variables in a Script

Environment Variables for a Single Command

List Environment Variables in a File

Job Scheduling Linux Command

corn

Edit your user’s crontab

List your user’s crontab

Remove your user’s crontab

at

After entering the at command, you’ll be prompted to enter the command or script to run. Press Ctrl+D to finish and schedule the job.

List at jobs

Remove an at job 

Shell Scripting Linux Command

Creating a Shell Script

Making a Shell Script Executable

Running a Shell Script

Shell Scripting Constructs

Conditional Statements (if-else)

Loops (for, while)

Functions

Command-Line Argument

Error Handling

 

Leave a Comment