basic linux commands

Essential Command Line Reference

muntaha Ghafoor

 

Table of Content:

1-Introduction: 2

2-Navigating the file systems: 2

3-File and directory operations: 3

4-Viewing and editing file content: 3

5-System Information Commands: 4

6-File permissions and ownership: 4

7-Viewing file content: 5

8-Text Editors: 5

9-Networking Commands: 6

10-Searching for Files and Content: 6

11-Disk Usage Connands: 7

12-Understanding File Permissions: 7

13-Archiving with tar: 7

14-System Information Commands: 8

15-Getting Help: 8

16-Conclusion: 9

Introduction:

The command line, also known as the Shell or terminal. The command line interface (CLI) is the heart of Linux. While graphical interfaces exist, the true power of Linux lies in its terminal. A text-based interface where users interact directly with the operating system by typing commands. Mastering basic Linux commands is the first and most important step in becoming proficient with Linux, whether for system administration, software development, or cybersecurity.

Navigating the file systems:

Linux organizes files and directories in a hierarchical tree structure starting from the root directory, represented by a forward slash (/). These commands are used to move around and understand this structure such as:

$ pwd

The pwd (Print Working Directory) command displays the full path of the current directory. This is essential for understanding where you are in the file system at any given time.

$ ls -la

The ls command lists files and directories. The -l flag provides detailed listing including permissions, owner, size, and modification date. The -a flag shows hidden files (files beginning with a dot). Combining these as -la gives a comprehensive view of all files in a directory.

$ cd /home/user/Documents

The cd (Change Directory) command moves to a specified directory. Using cd .. moves up one level, cd ~ returns to the home directory, and cd – returns to the previous directory.

Command Description Example
Pwd Prints the current working directory pwd
ls Lists files and directories ls -la
cd Changes the current directory Cd /home/user
tree Displays directory structure as a tree tree / etc

File and directory operations:

Creating, copying, moving and deleting files and directories are most important and common tasks performed in the terminal. These commands are the backbone of daily Linux usage.

Command Description Example
touch Creates an empty file touch notes.txt
mkdir Creates a new directory mkdir project
cp Copies files or directories cp file.txt backup.txt
mv Moves or rename files mv old.txt new.tct
rm Deletes files rm file.txt
rm -rf Deletes directories recursively rm -rf folder/
rmdir Removes an empty directory rmdir empty_folder

 

Be careful when use rm command, particularly rm -rf, since linux does not have built-in recycle bin for terminal deletions.

Viewing and editing file content:

Linux provides multiple commands for viewing file contents, each suited to different situations depending on the file size and the user’s needs.

$ cat /etc/passwd

The cat (concatenate) command displays the entire content of a file at once. It is best suited for small files. For larger files, it can be overwhelming as it outputs everything at once.

$ less /var/log/syslog

The less command provides a paginated view of file contents, allowing users to scroll up and down through large files. Press q to quit, / to search, and n to find the next match.

$ head -20 file.txt

$ tail -20 file.txt

The head command shows the first N lines of a file (default 10), while tail shows the last N lines. The tail -f command is particularly useful for monitoring log files in real time as it continuously displays new lines as they are added.

System Information Commands:

Understanding the state of a Linux system requires commands that can report on processes, memory, disk usage, and system resources.

Command Description
uname -a Display all system information including kernel version
whoami Show current logged-in username
id Show user ID and group memberships
df -h Show disk space usage in human-readable format
free -h Show memory usage in human-readable format
top Interactive process viewer showing CPU and memory usage
ps aux List all running processes with details
uptime Show how long the system has been running

File permissions and ownership:

Linux has a robust permission system that controls who can read, write, or execute files. Understanding and managing permissions is critical for system security.

$ chmod 755 script.sh

The chmod command changes file permissions. Permissions are represented as three octet values for owner, group, and others. The value 7 (rwx) gives full permissions, 5 (r-x) gives read and execute, and 4 (r–) gives read only.

$ chown muntaha:users file.txt

The chown command changes file ownership. The format is chown user:group filename. This is essential for managing who has access to specific files and directories.

$ sudo command

The sudo (superuser do) command allows permitted users to run commands with elevated privileges. It is safer than logging in as root because it creates an audit trail and requires authentication.

Viewing file content:

commands Description
cat file.txt Reads and displays file content / print entire file
less file.txt Scroll through file
head file.txt Show first 10 lines
tail file.txt Show last 10 lines
tail -f file.txt Live file monitering

Text Editors:

Editor Commands Best for
nano Nano file.txt Beginners
vim Vim file.txt Advanced users
Gedit Gedit file.txt GUI editor

Nano shortcuts:

Ctrl+o = save

Ctrl+x = Exit

Ctrl+k = Cut line

Networking Commands:

Commands Example Description
ifconfig ifcofig Shows your ip and network info
ip a ip a Modern version of ifconfig
ping ping google.com Test connection to a host
netstat netstat -an Shows active network connections
curl curl <url> Fetches data from a URL
wget wget <url> Downloads a file from internet
Ssh ssh user@192.168.1.1 Connect to remote machine
Traceroute traceroute google.com Shows path packets take to reach host

Searching for Files and Content:

Locating specific files or text within files is a frequent task, particularly during troubleshooting or security investigations.

Command Description Example
find Searches for files based on criteria find / -name “*.log”
grep Searches for text patterns within files grep “error” log.txt
locate Quickly finds files using a prebuilt index locate passwd
which Shows the location of a command’s executable which python3

The grep command is one of the most heavily used tools in Linux, especially in cybersecurity contexts, where it is often used to search log files for suspicious activity, filter command output, or scan scripts for specific patterns.

Disk Usage Connands:

Commands Description
df -h Show total disk space
du -sh folder Show size of a folder
lsblk List disks & partitions
fdisk -l Detailed disk info (sudo)

Understanding File Permissions:

Every file has 3 permission sets

Owner, Group, others

3 types

Read(r), write(w), Execute(x)

Example:

-rwxr – xr–

Owner rwx (full access)

Group r -x (read+execute)

Others r– (read only)

Archiving with tar:

Commands Description
tar -cvf file.tar folder Create archive
tar -xvf file.tar Extract archive
tar -czvf file.tar.gz folder Create compressed archive
tar -xzvf file.tar.gz Extract compressed archive

System Information Commands:

Understanding system status and resource usage is essential for administration and troubleshooting.

Command Description Example
uname -a Displays system and kernel information uname -a
top / htop Shows real-time running processes and resource usage top
df -h Displays disk space usage in human-readable form df -h
du -sh Shows the size of a directory du -sh /var/log
ps aux Lists all currently running processes ps aux
whoami Displays the current logged-in user whoami

Getting Help:

Nearly every Linux command includes built-in documentation accessible directly from the terminal:

  • man <command>: Opens the manual page for a command, providing a full description of its options and usage.
  • <command> –help: Displays a brief summary of a command’s usage and available flags.

Conclusion:

Mastery of basic Linux commands forms the foundation for nearly every advanced skill in system administration, DevOps, and cybersecurity. Commands for navigating the filesystem, managing files, controlling permissions, searching content, and monitoring system resources are used constantly in real-world environments. Building fluency with these tools not only increases efficiency but also becomes essential when working with the remote servers and security tools commonly encountered in professional practice.

 

Post Your Comment