mooc-course.com is learner-supported. When you buy through links on our site, we may earn an affiliate commission.

Linux Commands with Examples

Linux Commands with Examples

5/5 - (1 vote)

Linux commands are powerful tools that allow users to interact with and control their Linux operating system through a text-based interface. This guide covers essential Linux commands across various categories, providing explanations and examples for each.

Table of Contents

File and Directory Management Command Line in Linux

ls – List directory contents

Lists files and directories in the current directory.

See also  Difference Between Linux and Windows

Examples:

ls 
ls -l  # Long format with details
ls -a  # Show hidden files

cd – Change directory

Changes the current working directory.

Examples:

cd /home/user/documents
cd ..  # Move up one directory
cd ~   # Go to home directory

pwd – Print working directory

Displays the current directory path.

Example:

pwd

mkdir – Make a directory

Creates a new directory.

Example:

mkdir new_folder

rmdir – Remove directory

Removes an empty directory.

Example:

rmdir empty_folder

touch – Create an empty file

Creates a new empty file or updates timestamps of existing files.

Example:

touch newfile.txt

cp – Copy files and directories

Copies files or directories to another location.

Examples:

cp file.txt /home/user/documents/
cp -r folder1 folder2  # Copy directory recursively

mv – Move or rename files and directories

Moves files/directories or renames them.

Examples:

mv file.txt /home/user/documents/
mv oldname.txt newname.txt

rm – Remove files or directories

Deletes files or directories.

Examples:

rm file.txt
rm -r folder  # Remove directory and contents recursively

find – Search for files and directories

find command in Linux searches for files and directories based on various criteria.

Examples:

find /home -name "*.txt"
find . -type d  # Find directories

grep – Search text within files

Searches for patterns in files or input.

Examples:

grep "pattern" file.txt
grep -r "pattern" directory/

File Viewing and Editing Command Line in Linux

cat – Concatenate and display file contents

Displays contents of files.

Example:

cat file.txt

less – View file contents page by page

Allows scrolling through large files.

Example:

less largefile.txt

head – Display the beginning of the file

Shows the first few lines of a file.

Example:

head -n 10 file.txt  # Show first 10 lines

tail – Display end of file

Shows the last few lines of a file.

See also  How to Run Executable Files in Linux?

Example:

tail -n 20 file.txt  # Show last 20 lines

nano – Text editor

Simple text editor for the command line.

Example:

nano file.txt

vim – Advanced text editor

Powerful text editor with many features.

Example:

vim file.txt

System Information and Management Command Line in Linux

uname – Print system information

Displays system information.

Examples:

uname -a  # All system info
uname -r  # Kernel release

top – Display and manage processes

Shows a real-time view of running processes.

Example:

top

ps – Report process status

Lists running processes.

Examples:

ps aux  # Detailed process list
ps -ef  # Full-format listing

kill – Terminate processes

Sends signals to processes, often used to terminate them.

Examples:

kill 1234  # Terminate process with PID 1234
kill -9 1234  # Force terminate

df – Report file system disk space usage

Shows disk space usage.

Example:

df -h  # Human-readable format

du – Estimate file space usage

Estimates file and directory space usage.

Example:

du -sh *  # Summarize usage for current directory

free – Display the amount of free and used memory

Shows memory usage statistics.

Example:

free -m  # Display in megabytes

User and Permission Management Command Line in Linux

sudo – Execute command as superuser

Executes commands with elevated privileges.

Example:

sudo apt update

chmod – Change file permissions

Modifies file permissions.

Examples:

chmod 755 file.txt
chmod u+x script.sh  # Add execute permission for user

chown – Change file owner and group

Changes the owner and group of files/directories.

Example:

chown user:group file.txt

useradd – Create a new user

Adds a new user account.

Example:

sudo useradd newuser

passwd – Change user password

Changes password for a user account.

See also  Understanding Processes in Linux: A Practical Guide

Example:

passwd username

Networking Command Line in Linux

ping – Send ICMP echo request to network hosts

Tests network connectivity.

Example:

ping google.com

ifconfig – Configure network interface

ifconfig command in Linux displays or configures network interfaces.

Example:

ifconfig

netstat – Network statistics

Displays network connections and routing tables.

Example:

netstat -tuln

ssh – Secure shell client

Connects to remote systems securely.

Example:

ssh user@hostname

scp – Secure copy

Copies files between hosts on a network.

Example:

scp file.txt user@remote:/path/

Package Management Command Line in Linux

apt – Package management tool (Debian/Ubuntu)

Manages software packages on Debian-based systems.

Examples:

sudo apt update
sudo apt install package_name

yum – Package management tool (Red Hat/CentOS)

Manages software packages on Red Hat-based systems.

Examples:

sudo yum update
sudo yum install package_name

File Compression and Archiving Command Line in Linux

tar – Tape archiver

Linux tar command creates and extracts archive files.

Examples:

tar -cvf archive.tar files/
tar -xvf archive.tar

gzip – Compress or expand files

Linux gzip command compresses files.

Examples:

gzip file.txt
gunzip file.txt.gz

System Control Command Line in Linux

shutdown – Shut down or restart the system

Shut down or restart the system.

Examples:

sudo shutdown -h now  # Shutdown immediately
sudo shutdown -r +5  # Restart in 5 minutes

reboot – Reboot the system

Restarts the system.

Example:

sudo reboot

Miscellaneous Commands in Linux

history – Command history

Shows previously executed commands.

Example:

history

man – Manual pages

Displays manual pages for commands.

Example:

man ls

echo – Display a line of text

Prints text to the terminal.

Example:

echo "Hello, World!"

date – Display or set system date and time

Shows or sets the system date and time.

Example:

date

which – Locate a command

Shows the full path of shell commands.

Example:

which python
This guide covers a wide range of essential Linux commands, providing a solid foundation for working with Linux systems. Remember that many commands have additional options and functionalities not covered here. Always refer to the man pages or command help (–help) for more detailed information on each command.
Leave a Reply

Your email address will not be published. Required fields are marked *

Free Worldwide Courses

Learn online for free

Enroll in Multiple Courses

Learn whatever your want from anywhere, anytime

International Language

Courses offered in multiple languages & Subtitles

Verified Certificate

Claim your verified certificate