Here’s a list of basic Linux commands for beginners, tailored for aspiring DevOps engineers. Each command is followed by a simple explanation:
1. pwd
(Print Working Directory)
Usage:
pwd
Explanation: Displays the current directory you're in. It helps you confirm where you are in the file system.
2. ls
(List Directory Contents)
Usage:
ls
Common options:
ls -l
: Lists files in long format (shows permissions, size, etc.).ls -a
: Lists all files, including hidden ones (those starting with.
).
Explanation: Lists all files and directories in your current directory. Useful for navigating and viewing file structure.
3. cd
(Change Directory)
Usage:
cd [directory]
- Example:
cd /var/log
- Example:
Explanation: Moves you into the specified directory. Essential for navigating through the file system.
4. touch
(Create a New File)
Usage:
touch [filename]
- Example:
touch myfile.txt
- Example:
Explanation: Creates a new, empty file. Useful for quickly creating files.
5. mkdir
(Make Directory)
Usage:
mkdir [directory name]
- Example:
mkdir mynewfolder
- Example:
Explanation: Creates a new directory/folder. Helps organize files.
6. rm
(Remove File or Directory)
Usage:
rm [file]
: Remove a file.rm -r [directory]
: Remove a directory and its contents recursively.Example:
rm -r myfolder
Explanation: Deletes files or directories. Be careful when using this command since it’s irreversible.
7. cp
(Copy Files or Directories)
Usage:
cp [source] [destination]
: Copy a file.cp -r [source] [destination]
: Copy a directory recursively.Example:
cp file.txt /backup/file.txt
Explanation: Copies files or directories from one location to another.
8. mv
(Move or Rename Files)
Usage:
mv [source] [destination]
: Move or rename a file or directory.Example:
mv file.txt /backup/
ormv oldname.txt newname.txt
Explanation: Moves files from one location to another, or renames files/directories.
9. cat
(Concatenate and Display File Content)
Usage:
cat [filename]
- Example:
cat myfile.txt
- Example:
Explanation: Displays the content of a file in the terminal. Good for quickly viewing files.
10. nano
or vim
(Text Editors)
Usage:
nano [filename]
: Opens a file in the Nano text editor.vim [filename]
: Opens a file in the Vim text editor.
Explanation: Text editors in the terminal for editing configuration files, scripts, etc.
11. grep
(Search for Patterns in Files)
Usage:
grep [pattern] [file]
- Example:
grep 'error' /var/log/syslog
- Example:
Explanation: Searches for a specific pattern or string in files. Very useful for searching logs or large text files.
12. find
(Find Files and Directories)
Usage:
find [path] -name [filename]
- Example:
find / -name "myfile.txt"
- Example:
Explanation: Finds files and directories in the file system based on their name or other properties.
13. chmod
(Change File Permissions)
Usage:
chmod [permissions] [file]
- Example:
chmod 755
script.sh
- Example:
Explanation: Changes the permissions of a file or directory. The
755
means read, write, and execute for the owner, and read and execute for others.
14. ps
(Process Status)
Usage:
ps
orps aux
- Example:
ps aux | grep apache
- Example:
Explanation: Displays the running processes on the system.
ps aux
shows all processes with detailed information. Use withgrep
to filter for specific processes.
15. kill
(Terminate a Process)
Usage:
kill [PID]
- Example:
kill 12345
- Example:
Explanation: Terminates a process by its Process ID (PID). Used to stop unresponsive programs.
16. df
(Disk Usage)
Usage:
df -h
Explanation: Shows the available disk space on mounted file systems. The
-h
option makes the output human-readable (in GB, MB, etc.).
17. du
(Disk Usage of Files/Directories)
Usage:
du -h [path]
- Example:
du -h /var/log
- Example:
Explanation: Shows the disk space used by files and directories. The
-h
option provides human-readable sizes.
18. tar
(Archive Files)
Usage:
To create an archive:
tar -cvf [archive.tar] [files]
To extract an archive:
tar -xvf [archive.tar]
Example:
tar -cvf myarchive.tar myfolder/
Explanation: Compresses or extracts files into/from an archive. Very useful for backups and transferring multiple files.
Creating a Tar Archive
Open Your Terminal.
Navigate to the Directory: Change to the directory containing the files you want to archive. For example:
bashCopy codecd /path/to/directory
Create a Tar Archive: Use the following command to create a tar archive named
archive.tar
from filesfile1.txt
andfile2.txt
:bashCopy codetar -cvf archive.tar file1.txt file2.txt
-c
: Create a new archive.-v
: Verbose output (lists files being processed).-f
: Specifies the filename of the archive.
Extracting a Tar Archive
To extract the contents of archive.tar
, use the following command:
bashCopy codetar -xvf archive.tar
-x
: Extract files from the archive.-v
: Verbose output.-f
: Specifies the filename of the archive.
Example Summary
Create Archive:
bashCopy codetar -cvf archive.tar file1.txt file2.txt
Extract Archive:
bashCopy codetar -xvf archive.tar
19. ssh
(Secure Shell)
Usage:
ssh [user]@[server_ip]
- Example:
ssh user@192.168.1.10
- Example:
Explanation: Securely connects to a remote server via SSH. This is one of the key tools for DevOps engineers to manage remote infrastructure.
Connecting to an EC2 Instance via SSH
Open Terminal: Open your terminal on your local machine.
Navigate to Key Pair: Change to the directory where your
.pem
file is stored:bashCopy codecd /path/to/your/keypair/
Set Key Permissions: Ensure your key pair file has the correct permissions:
bashCopy codechmod 400 your-key-pair.pem
Connect to EC2 Instance: Use the SSH command to connect. Replace the placeholders with your values:
Accept the Host Key: Type
yes
if prompted to confirm the authenticity of the host.
Summary
- Use SSH to securely access your EC2 instance using your key pair.
20. sudo
(Run as Superuser)
Usage:
sudo [command]
- Example:
sudo apt update
- Example:
Explanation: Runs commands with superuser (root) privileges. Always use with caution, as it allows you to make system-wide changes.
21. apt
or yum
(Package Managers)
Usage:
apt
: For Ubuntu/Debian-based systems.yum
: For CentOS/RedHat-based systems.Example:
sudo apt install nginx
orsudo yum install nginx
Explanation: Manages the installation, update, and removal of software packages.
22. systemctl
(Manage System Services)
Usage:
systemctl start [service]
: Starts a service.systemctl stop [service]
: Stops a service.systemctl restart [service]
: Restarts a service.Example:
sudo systemctl restart nginx
Explanation: Used to start, stop, or manage system services like web servers, databases, etc.
23. top
(Real-Time Process Monitoring)
Usage:
top
Explanation: Displays real-time system information such as CPU, memory usage, and running processes. Great for monitoring system health.
24. crontab
(Schedule Tasks)
Usage:
crontab -e
Explanation: Opens the cron scheduler to define automated tasks (jobs) that run at specified intervals. Essential for automation in DevOps.
Using
crontab
Open Your Terminal:
Edit Your Crontab: To edit your crontab file, use the following command:
bashCopy codecrontab -e
Crontab Syntax: Each line in the crontab file represents a scheduled task and follows this syntax:
markdownCopy code* * * * * command_to_run
The five asterisks represent the following time fields:
Minute (0-59)
Hour (0-23)
Day of the Month (1-31)
Month (1-12)
Day of the Week (0-7) (Sunday can be represented as 0 or 7)
For example, to run a script every day at 5:30 AM, you would write:
javascriptCopy code30 5 * * * /path/to/your/script.sh
Common Examples:
Run a script every minute:
bashCopy code* * * * * /path/to/your/script.sh
Run a backup script every day at 2 AM:
bashCopy code0 2 * * * /path/to/backup.sh
Run a command every Sunday at midnight:
bashCopy code0 0 * * 0 /path/to/your/script.sh
View Your Crontab: To view the scheduled tasks in your crontab, run:
bashCopy codecrontab -l
Remove Your Crontab: If you want to delete all your scheduled tasks, use:
bashCopy codecrontab -r
Example Summary
Command to Edit Crontab:
bashCopy codecrontab -e
Example Crontab Entry:
bashCopy code30 5 * * * /path/to/your/script.sh # Runs the script every day at 5:30 AM