๐ Mastering Linux Commands: Basics to Advanced
Welcome to my adventure of helping people out there not familiar with these terms. Today we will dive into the world of Linux! Whether you're a beginner or looking to advance your skills, this guide is your go to cheat sheet for Linux commands, from basic to advanced. Let's dive in! ๐
๐ข Basic Commands
1. cd
- Change Directory
The cd
command is used to change the current working directory.
Syntax:
cd [directory_name]
Examples:
cd /home/tanmaya # Change to the /home/tanmaya directory
cd .. # Move up one directory level
cd ~ # Change to the home directory
2. ls
- List Directory Contents
The ls
command lists the files and directories in the current directory.
Syntax:
ls [options/flags] [directory]
Examples:
ls # List files in the current directory
ls -l # List in long format
ls -a # List all files, including hidden files
3. pwd
- Print Working Directory
The pwd
command displays the full path of the current directory.
Syntax:
pwd
Example:
pwd # Shows the current directory path
4. who
- Show Who is Logged On
The who
command shows who is currently logged into the system.
Syntax:
who
Example:
who # Lists all logged-in users
5. whoami
- Display Current User
The whoami
command shows the username of the current user.
Syntax:
whoami
Example:
whoami #Displays the current user's name. Eg: tanmaya, if done on my system.
6. touch
- Create an Empty File
The touch
command creates an empty file or updates the timestamp of an existing file.
Syntax:
touch [filename]
Example:
touch file.txt # Creates an empty file named file.txt
7. mkdir
- Make Directory
The mkdir
command creates a new directory.
Syntax:
mkdir [directory]
Example:
mkdir test_tanmaya #Creates a directory named "test_tanmaya"
8. cp
- Copy Files
The cp
command copies files or directories.
Syntax:
cp [source] [destination]
Examples:
cp file1.txt file2.txt # Copy file1.txt to file2.txt
cp -r dir1 dir2 # Copy directory dir1 to dir2
9. mv
- Move/Rename Files
The mv
command moves or renames files or directories.
Syntax:
mv [source] [destination]
Examples:
mv file1.txt file2.txt # Rename file1.txt to file2.txt
mv file.txt /home/user/ # Move file.txt to /home/user/
10. rm
- Remove Files
The rm
command deletes files or directories.
Syntax:
rm [options] [file]
Examples:
rm file.txt # Delete file.txt
rm -r dir # Delete directory dir and its contents
11. cat
- Concatenate and Display Files
The cat
command displays the contents of a file.
Syntax:
cat [file]
Example:
cat file.txt # Display the contents of file.txt
12. echo
- Display a Line of Text
The echo
command displays a line of text or a variable value.
Syntax:
echo [text]
Example:
echo "Hello, World!" # Prints "Hello, World!"
๐ก Intermediate Commands
13. nano
- Text Editor
The nano
command opens the Nano text editor, a simple and user-friendly editor.
Syntax:
nano [file]
Example:
nano file.txt # Opens file.txt in Nano
14. vi
- Text Editor
The vi
command opens the Vi text editor, a more powerful but complex editor.
Syntax:
vi [file]
Example:
vi file.txt # Opens file.txt in Vi
15. chmod
- Change File Permissions
The chmod
command changes the file permissions.
Syntax:
chmod [permissions] [file]
Example:
chmod 755 file.sh # Sets read, write, execute for owner, and read, execute for group and others
16. chown
- Change File Owner and Group
The chown
command changes the owner and group of a file.
Syntax:
chown [owner]:[group] [file]
Example:
chown tanmaya:student file.txt # Changes the owner to tannmaya and the group to student.
17. ps
- Process Status
The ps
command displays information about running processes.
Syntax:
ps [options]
Example:
ps aux # Shows detailed information about all running processes
18. top
- Display Task Manager
The top
command shows real-time system information, including CPU and memory usage.
Syntax:
top
Example:
top # Opens the real-time system monitor
19. kill
- Terminate Processes
The kill
command sends a signal to a process to terminate it.
Syntax:
kill [signal] [PID]
Example:
kill 1234 # Terminates the process with PID 1234
20. nice
- Set Process Priority
The nice
command starts a process with a specified priority.
Syntax:
nice -n [priority] [command]
Example:
nice -n 10 ./script.sh # Runs script.sh with a priority of 10
21. vmstat
- Report Virtual Memory Statistics
The vmstat
command reports information about processes, memory, paging, block IO, traps, and CPU activity.
Syntax:
vmstat [options]
Example:
vmstat # Shows a summary of virtual memory statistics
๐ต Advanced Commands
22. awk
awk
is a powerful programming language for pattern scanning and processing. It is used to manipulate data and generate reports.
Syntax:
awk 'pattern {action}' filename
Example: Print the second column of a file:
awk '{print $2}' file.txt
Example: Filter lines containing "Info" and print the first and third columns:
awk '/Info/ {print $1, $3}' file.txt
23. sed
sed
(stream line editor) is used for parsing and transforming text.
Syntax:
sed 's/pattern/replacement/' filename
Example: Replace "Linux" with "Unix" in a file:
sed 's/Linux/Unix/g' file.txt
Example: Delete lines containing "Linux":
sed '/Linux/d' file.txt
24. grep
grep
searches for patterns within files and outputs the matching lines.
Syntax:
grep 'pattern' filename
Example: Find lines containing "error":
grep 'error' logfile.txt
Search recursively in all files within a directory:
grep -r 'error' /path/to/directory/
25. find
find
searches for files and directories in a directory hierarchy.
Syntax:
find /path/to/search -name "filename"
Example: Find all .txt
files in the current directory:
find . -name "*.txt"
Example: Find files larger than 10MB:
find /path/to/search -size +10M
26. scp
scp
(secure copy) is used to copy files between hosts on a network.
Syntax:
scp source_file username@destination_host:/path/to/destination/
Example: Copy a file to a remote server:
scp file.txt user@remote:/path/to/destination/
Example: Copy a file from a remote server:
scp user@remote:/path/to/source/file.txt /local/path/
27. rsync
rsync
is used for fast, incremental file transfer. Transfers only new changes done to the file.
Syntax:
rsync options source destination
Example: Synchronize a directory to a remote server:
rsync -avz /local/dir/ user@remote:/path/to/destination/
Example: Synchronize a remote directory to the local machine:
rsync -avz user@remote:/path/to/source/ /local/dir/
Learning these Linux commands will make you more efficient and capable as a DevOps engineer.
I hope I was able to deliver these commands in an easy way making you understand. Happy to connect for any queries!!
Subscribe to my newsletter
Read articles from Tanmaya Arora directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Tanmaya Arora
Tanmaya Arora
Hey there! ๐ ๐ Welcome to my DevOps journey! ๐ I'm Tanmaya Arora, an enthusiastic DevOps Engineer. Currently, on a learning adventure, I'm here to share my journey and Blogs about DevOps. I believe in fostering a culture of resilience, transparency, and shared responsibility. Embracing agility and flexibility, in this adventure let's grow together in this vibrant DevOps space. Join me in transforming software delivery through collaboration, innovation, and excellence! ๐๐ง๐ก ๐ Connect with me for friendly chats, group discussions, shared experiences and learning moments.