Top 50 Essential Ubuntu Commands for Daily Use: Detailed Guide with Examples
File and Directory Management
Table of Contents
Togglels– List directory contents.- Syntax:
ls [options] [directory] - Example:
ls -la /var/www/ - Details: Lists all files and directories in
/var/www/, including hidden files, with detailed information (-la).
- Syntax:
cd– Change directory.- Syntax:
cd [directory] - Example:
cd /var/www/html - Details: Changes the current working directory to
/var/www/html.
- Syntax:
pwd– Print working directory.- Syntax:
pwd - Example:
pwd - Details: Displays the full path of the current directory.
- Syntax:
mkdir– Make a new directory.- Syntax:
mkdir [options] [directory] - Example:
mkdir /var/www/new_project - Details: Creates a new directory named
new_project.
- Syntax:
touch– Create an empty file.- Syntax:
touch [filename] - Example:
touch index.html - Details: Creates an empty file named
index.html.
- Syntax:
cp– Copy files or directories.- Syntax:
cp [source] [destination] - Example:
cp file.txt /backup/ - Details: Copies
file.txtto the/backup/directory.
- Syntax:
mv– Move (or rename) files or directories.- Syntax:
mv [source] [destination] - Example:
mv file.txt old_file.txt - Details: Renames
file.txttoold_file.txt.
- Syntax:
rm– Remove files or directories.- Syntax:
rm [options] [file/directory] - Example:
rm -rf /tmp/testdir - Details: Removes the directory
testdirand its contents recursively (-rf).
- Syntax:
cat– Concatenate and display file content.- Syntax:
cat [file] - Example:
cat /etc/hosts - Details: Displays the content of
/etc/hosts.
- Syntax:
nano– Simple text editor.- Syntax:
nano [file] - Example:
nano /etc/hostname - Details: Opens
hostnamefor editing in thenanotext editor.
- Syntax:
less– View file content page-by-page.- Syntax:
less [file] - Example:
less /var/log/syslog - Details: Displays the content of
syslogfile in a scrollable format.
- Syntax:
head– Display the first lines of a file.- Syntax:
head [options] [file] - Example:
head -n 10 file.txt - Details: Shows the first 10 lines of
file.txt.
- Syntax:
tail– Display the last lines of a file.- Syntax:
tail [options] [file] - Example:
tail -f /var/log/syslog - Details: Follows the last lines of
syslogin real-time.
- Syntax:
find– Search for files in a directory hierarchy.- Syntax:
find [directory] [options] [expression] - Example:
find / -name "*.conf" - Details: Searches for all
.conffiles starting from the root directory.
- Syntax:
grep– Search for text patterns.- Syntax:
grep [options] pattern [file] - Example:
grep "error" /var/log/syslog - Details: Searches for the word “error” in
syslog.
- Syntax:
ln– Create hard and symbolic links.- Syntax:
ln [options] [target] [link] - Example:
ln -s /var/www/html /var/www/html_link - Details: Creates a symbolic link named
html_linkpointing to/var/www/html.
- Syntax:
Permissions and Ownership
chmod– Change file modes or Access Control Lists.- Syntax:
chmod [options] mode [file] - Example:
chmod 755 script.sh - Details: Sets read, write, and execute permissions for the owner, and read and execute for others.
- Syntax:
chown– Change file owner and group.- Syntax:
chown [owner]:[group] [file] - Example:
chown user:group file.txt - Details: Changes ownership of
file.txttouserand group togroup.
- Syntax:
chgrp– Change group ownership.- Syntax:
chgrp [group] [file] - Example:
chgrp www-data index.html - Details: Changes the group ownership of
index.htmltowww-data.
- Syntax:
System Information and Monitoring
df– Display disk space usage.- Syntax:
df [options] - Example:
df -h - Details: Displays disk space usage in a human-readable format.
- Syntax:
du– Estimate file space usage.- Syntax:
du [options] [directory] - Example:
du -sh /var - Details: Displays the disk usage of the
/vardirectory.
- Syntax:
free– Display memory usage.- Syntax:
free [options] - Example:
free -m - Details: Displays memory usage in megabytes.
- Syntax:
top– Display Linux tasks.- Syntax:
top - Example:
top - Details: Shows real-time CPU and memory usage.
- Syntax:
htop– Interactive process viewer (better version oftop).- Syntax:
htop - Example:
htop - Details: Displays an interactive and improved interface for viewing system processes.
- Syntax:
ps– Report a snapshot of current processes.- Syntax:
ps [options] - Example:
ps aux - Details: Lists all running processes on the system.
- Syntax:
uptime– Show how long the system has been running.- Syntax:
uptime - Example:
uptime - Details: Displays system uptime, number of users, and average system load.
- Syntax:
uname– Print system information.- Syntax:
uname [options] - Example:
uname -a - Details: Displays all system information including kernel name, version, and architecture.
- Syntax:
whoami– Print the current user.- Syntax:
whoami - Example:
whoami - Details: Displays the name of the current user.
- Syntax:
id– Print user and group information.- Syntax:
id [username] - Example:
id user - Details: Displays user and group IDs for the specified
user.
- Syntax:
kill– Send a signal to a process.- Syntax:
kill [signal] PID - Example:
kill -9 1234 - Details: Kills the process with the specified PID forcefully.
- Syntax:
Package Management with apt
apt-get update– Update package index.- Syntax:
sudo apt-get update - Example:
sudo apt-get update - Details: Refreshes the package lists for upgrades and new installations.
- Syntax:
apt-get upgrade– Upgrade all installed packages.- Syntax:
sudo apt-get upgrade - Example:
sudo apt-get upgrade - Details: Upgrades all installed packages to their latest versions.
- Syntax:
apt-get install– Install a new package.- Syntax:
sudo apt-get install [package] - Example:
sudo apt-get install nginx - Details: Installs the
nginxpackage.
- Syntax:
apt-get remove– Remove a package.- Syntax:
sudo apt-get remove [package] - Example:
sudo apt-get remove apache2 - Details: Removes the
apache2package.
- Syntax:
apt-get autoremove– Remove unused packages.- Syntax:
sudo apt-get autoremove - Example:
sudo apt-get autoremove - Details: Removes packages that were automatically installed and are no longer needed.
- Syntax:
Network Management
ifconfig– Display network configuration.- Syntax:
ifconfig [interface] - Example:
ifconfig eth0 - Details: Displays the network settings for the
eth0interface.
- Syntax:
ping– Send ICMP echo requests to test connectivity.- Syntax:
ping [host] - Example:
ping google.com - Details: Sends ICMP echo requests to
google.comto check connectivity.
- Syntax:
curl– Transfer data from or to a server.- Syntax:
curl [options] [URL] - Example:
curl https://linuxcloudservers.com - Details: Fetches content from
linuxcloudservers.com.
- Syntax:
wget– Download files from the web.- Syntax:
wget [options] [URL] - Example:
wget https://linuxcloudservers.com/download/file.zip - Details: Downloads
file.zipfrom the given URL.
- Syntax:
ssh– Connect to a remote server securely.- Syntax:
ssh [user]@[host] - Example:
ssh [email protected] - Details: Connects to the remote server
linuxcloudservers.comvia SSH.
- Syntax:
scp– Securely copy files between hosts.- Syntax:
scp [source] [destination] - Example:
scp file.txt [email protected]:/tmp/ - Details: Copies
file.txtto/tmp/on the remote serverlinuxcloudservers.com.
- Syntax:
Compression and Archiving
gzip– Compress files.- Syntax:
gzip [file] - Example:
gzip file.txt - Details: Compresses
file.txtintofile.txt.gz.
- Syntax:
gunzip– Decompress.gzfiles.- Syntax:
gunzip [file] - Example:
gunzip file.txt.gz - Details: Decompresses
file.txt.gz.
- Syntax:
tar– Archive multiple files.- Syntax:
tar [options] [archive-file] [file/directory] - Example:
tar -czvf backup.tar.gz /var/www/html - Details: Creates a compressed archive
backup.tar.gzof the/var/www/htmldirectory.
- Syntax:
Users and Permissions
adduser– Add a new user.- Syntax:
sudo adduser [username] - Example:
sudo adduser john - Details: Creates a new user named
john.
- Syntax:
deluser– Remove a user.- Syntax:
sudo deluser [username] - Example:
sudo deluser john - Details: Deletes the user
john.
- Syntax:
passwd– Change user password.- Syntax:
passwd [username] - Example:
passwd john - Details: Changes the password for the user
john.
- Syntax:
Scheduling and Jobs
crontab– Schedule periodic jobs.- Syntax:
crontab [options] - Example:
crontab -e - Details: Edits the crontab file to add, remove, or modify scheduled jobs.
- Syntax:
at– Schedule a one-time job.- Syntax:
at [time] - Example:
echo "backup.sh" | at 02:00 - Details: Schedules
backup.shto run at2:00 AM.
- Syntax:
System Shutdown and Reboot
shutdown– Bring the system down.- Syntax:
shutdown [options] [time] - Example:
sudo shutdown -h now - Details: Immediately shuts down the system (
-hfor halt).
- Syntax:
These commands provide a comprehensive toolkit for navigating and managing an Ubuntu system effectively. Whether you are working on file operations, system monitoring, networking, or scheduling tasks, these commands will come in handy.
