Table of Contents
Hello friend! Welcome to the world of Linux. As an operating system, Linux offers you an exciting way to interact with your computer using just text commands.
I know commands can be intimidating for beginners. But don‘t worry, with this guide I‘m here to help you master the essentials, one step at a time.
By the end, these commands will start feeling like second nature to you.
Let‘s get started!
Why Learn Linux Commands
But before we jump into the commands, you may be wondering — why even bother learning this?
Well, here are some excellent reasons:
- Job Opportunities: Linux skills are highly sought after in tech jobs, with 80% of employers hiring for Linux roles.
- Flexible Control: Commands give you fine-grained control to customize your system.
- Power User: Mastering commands unlocks the full potential of Linux‘s power, speed and security.
As you can see, taking time to learn commands will give your tech skills and career a big boost.
Now let‘s get your hands dirty with some real commands! I promise with regular usage, these will become second nature to you.
Essential File Management Commands
Navigating and organizing files is one of the most frequent tasks you‘ll perform on Linux. File commands provide powerful, flexible ways of managing your storage system.
Let me walk you through the key commands for seamless file navigation:
cd
Stands for "change directory". It moves you around the Linux file system, similar to how you browse folders in Windows explorer or Mac Finder.
Some examples:
Move to folder:
cd Documents/
Navigate up one level:
cd ..
Jump to root folder:
cd /
See how easy it is! cd lets you navigate just like using graphical file explorers, except faster.
💡 Pro tip: Type
cd ~
to jump straight to your user‘s home directory from anywhere in the system.
ls
If cd moves you around folders, ls
command lifts the curtains on what‘s actually inside them.
It lists contents of current or specified directories. For example:
ls my-folder/
Would show files/sub-folders in my-folder
.
Some handy options:
ls -l
– Lists files with details like permissions and sizesls -a
– Show hidden files too beginning with .ls -la
– Details along with hidden files
pwd
While moving around so much, it‘s easy to lose track of your current location. This is where pwd
comes in.
It prints/shows the working or present working directory, i.e. your current location in the Linux file system. Simple!
mkdir
This command does what it reads like: Make Directories!
For example:
mkdir Movies Music Documents
Creates 3 directories for your media and documents.
cp
Stands for "copy". It copies files & directories from one location to another in Linux file system.
Basic syntax:
cp source destination
Some examples:
Copy file:
cp file.txt ~
Copy directory recursively:
cp -r folder /backup/
This flexibility allows you to quickly copy anything to any location in Linux.
mv
Use the mv
or "move" command to move files/dirs to another location and even rename at the same time!
For example:
Move file:
mv file.txt documents
Also rename it:
mv file.txt annual-report-2022.txt
See what I meant by flexibility? mv
lets you move AND/OR rename things in one command.
rm
If mv
moves things around, the rm
or "remove" command permanently deletes files and folders.
Delete file:
rm spam-email.txt
Recursively delete directory & contents:
rm -r MyFolder/
🛑 Caution: Deleted files don‘t go to Trash, so use rm carefully!
Well done exploring the file commands! Being able to flexibly manage files is crucial for your productivity on Linux.
These 7 commands should help you achieve most common file tasks now. Feel free to try them out on some test folders/files to get comfortable!
Managing Running Processes
When using Linux, there would be many processes running in the background that make the system work. Managing these processes is important to monitor what‘s happening.
Here are some handy commands:
ps
The ps
command gives a process snapshot, showing currently running processes along with details like PID and owner.
For example, try:
ps aux
This will show processes running under all users with additional details.
top
If ps
gives you a static snapshot, top
command provides a live view of dynamic real-time system processes.
It orders processes by resource usage allowing you to identify heavy memory and CPU consumers.
Keep pressing SPACEBAR on top
output to refresh view.
Plus SHIFT + p
or SHIFT + m
toggles display between CPU and memory usage. Very handy!
kill
As the name suggests, the kill
command ends or terminates a running process based on its PID.
For example:
kill 4509
Would terminate process with PID 4509.
A related command is pkill
which kills process based on name instead of PID.
bg
Sometimes you want a running process to keep running safely in the background. This is what bg
does.
For example:
some_process &
bg
Sends some_process
to run in background.
crontab
The crontab
command allows you to schedule commands to run automatically at specific times/dates. Think of it like an automation service for Linux administration tasks!
For example, edit root‘s cron table:
sudo crontab -e
And schedule backups:
0 2 * * * rsync -a /home /backups
This would sync /home
to backup location every day at 2 AM. See how useful crontab can be for admins?
There are many more process commands but these cover the key ones for getting started.
User Management & Permissions
Managing different types of Linux users and their access permissions is important for security.
adduser/useradd
Use adduser
or useradd
command to create a new user profile. For example:
sudo adduser john
Adds new user account named john
.
You‘ll be prompted for password and optional user details.
passwd
Once added, set a password for the new user using passwd
:
passwd john
You can also use it change any user‘s password by specifying their username.
su
Say you want to switch users and operate as another user account temporarily. The su
("switch user") command does exactly that!
su john
Logs you in as user john by prompting for john‘s password.
sudo
Similar to su, sudo
("superuser do") allows you run commands as another user usually the root or admin account.
But unlike su, it asks for your password when running commands using another user‘s privilege.
For example:
sudo adduser mark
Creates new user mark
by prompting you for your password instead of root‘s. More secure!
This allows admins to provide select users limited supervisor access via sudo without handing out root password. Very handy!
chmod
This command manages access permissions of files/dirs based on type of Linux users:
- u – Owner user
- g – Owner group
- o – Other users
- a – All above types
Permissions Types:
- r – Read
- w – Write (Edit)
- x – Execute (Run programs/scripts)
For example, to give owner read, write, exec access:
chmod u=rwx file.txt
Or give all users read and exec rights only:
chmod a=rx file.txt
See how easy yet granular Linux permissions can be?
There quite a few more user management commands but these should get you started with adding users, running commands under different identities and most importantly managing access control permissions!
Network and Internet Connectivity
Accessing networks and internet is integral for any Linux server or desktop.
Managing network connections and diagnosing issues is where these handy commands come in.
ip
The ip
command includes several sub-commands for detailed network configurations in Linux.
For example:
Show network interfaces
ip addr show
Routing table information
ip route show
ping
The good old ping
does exactly what it‘s named after — it pings or sends test request packets to target servers to check connectivity.
For example:
ping google.com
Fires test pings to google and reports back response and latency. Super useful for basic network troubleshooting.
traceroute
While ping
checks basic connectivity, traceroute
displays the entire route path data packets take to reach destination.
This allows you to pinpoint network issues down to specific hops between you and target server.
netstat
The netstat
command lists useful network related information like open ports, connections and interfaces.
For example:
netstat -antup
Lists all TCP and UDP network ports in use by various processes/services.
Helps ensure no unwanted suspicious connections or ports open without your knowledge. Useful from security standpoint.
iptables
While netstat
shows open ports, iptables
command manages the Linux software firewall by setting up traffic filtering rules.
For example:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
Allow incoming web traffic on port 80.
You can filter rules based on protocols, port numbers, IPs etc. Very handy for security hardening Linux servers by only allowing required traffic.
There are many more specialized networking commands but these essential ones should get you started with diagnosing and setting up basic connectivity scenarios in Linux.
Getting System Resource Information
In addition to running processes, having a visibility into system resource usage like CPU, memory and disk is also important for administrators.
Following commands come handy in collecting Linux system info:
uptime
A simple uptime
shows current system time along with uptime stats indicating how long your Linux machine has been running uninterrupted.
Sample output:
14:17:36 up 45 days, 23:56, 1 user, load average: 0.12, 0.20, 0.31
Also shows number of logged in users and average CPU load information.
free
Want to check available RAM memory on your Linux machine or server? free
is your friend.
Example to show RAM stats in human friendly units:
free -h
This displays output split into total, used and free memory along with swap space utilization.
df
Just as free
gave insights into RAM usage, use df
("disk filesystem") to get visibility into used and available hard disk space.
For instance:
df -h
Shows filesystem disk space usage in easy to read format with units in GB/MB for various mount points and disks.
Monitoring disk utilization is super important to avoid unexpected out of space scenarios!
lscpu
To get CPU architecture details, number of cores/threads, sockets and model name, use informative lscpu
output.
This allows administrators to easily get CPU hardware details right from CLI, without needing to crack open the server chassis!
There are additional commands for gathering specific statistics like network bandwidth usage, active user sessions etc.
But above system resource commands already allows you pretty decent visibility into the overall utilization to effectively monitor for bottlenecks.
Searching and Processing Files
Beyond just managing files, an important aspect is searching within files and processing contents programmatically.
Commands like grep
, sed
and awk
make this possible.
grep
One of the most popular commands, grep
searches for text strings or regular expression patterns within files.
For example, search for "error" in log file:
grep ‘error‘ /var/log/syslog
Any line in log file containing "error" would be printed out.
That‘s just skimming surface of grep‘s capabilities to slice and dice file contents!
sed
If grep searches files, sed
edits or manipulates file contents directly.
Think search and replace on steroids!
For example, replace email domain:
sed ‘s/@company\.com/@newcompany.com/‘ contacts.txt
This replaces all company.com emails with newcompany.com in contacts.txt file non-destructively.
awk
Finally, awk
is a full blown programming language designed for text processing duties like extraction, formatting, calculations on structured text files.
For example, extract just usernames from a system log:
log.txt | awk -F‘[‘ ‘{print $2}‘
Here -F
defines field separator as [
which prints second column usernames into output.
As you can see, commands like grep
, sed
and awk
form essential part of administrator‘s toolbox for tapping into files for automation duties!
Creating Quick Access Shortcuts
Constantly typing commonly used long commands and options can get tiring for day-to-day terminal usage.
Luckily there are handy ways to create quick access shortcuts for commands using:
alias
The alias
command allows you to give shorthand nicknames for longer commands and save typing efforts!
For example:
alias update=‘sudo apt update && sudo apt upgrade‘
Creates shortcut called update
for frequently running system updates.
Some handy aliases like:
alias ..=‘cd ..‘
alias home=‘cd ~‘
Saves you from typing commonly used cd
commands. Sweet!
Functions
You can also directly create shortcut functions which are more powerful than aliases as they allow input parameters and variable expansions.
For example:
mkd() {
mkdir -p "$1"
cd "$1"
}
This defines a function called mkd
which mkdir‘s to given directory name and cd‘s into it.
Then simply run:
mkd code-projects
To swiftly create and jump into a new folder. Functions FTW!
Getting Help With Commands
Finally, as you encounter unfamiliar commands remember help is just a command away!
man
The venerable man
("manual") pages literally provide user manuals for commands right inside terminal.
For example:
man grep
Shows options and usage instructions for grep
command.
Navigating man pages takes some getting used to but is well worth learning!
whatis
Get a quick one line summary of any command right from terminal using whatis
:
whatis df
Prints short description from manual page.
tldr
An alternative to traditional man pages is the simplified and example focused tldr pages.
It‘s an open-source community project aimed at explaining commands with illustrative examples.
Seriously go check it out once to see some commands explained cleanly!
Well that concludes this epic Linux command cheat sheet!
You now have all the essentials from filesystem management to user privileges and networking plus handy tricks under your belt to productively enjoy benefits of Linux.
Time to flex your CLI muscles by practicing them one by one at every opportunity. Very soon Linux command line won‘t feel so alien anymore!
Happy tinkering my friend!