Motivation: Why should learn?
- Linux is important due to its widespread use in servers, cloud computing, and DevOps, making it a cornerstone of modern IT infrastructure.
- Learning commands is essential because it enables efficient interaction with operating systems, automates repetitive tasks, and improves productivity.
- Mastering commands provides greater control over system operations, troubleshooting, and scripting.
Important commands are listed as follows:
ls
- It lists files and directories in the current directory
user@ubuntu:~ $ ls
# list
user@ubuntu:~ $ ls -l
# long listing with permissions
drwxr-xr-x 6 user user 4096 Nov 1 12:42 .cache
-rw-r--r-- 1 user user 12 Nov 1 11:54 abc.txt
user@ubuntu:~ $ ls -a
# include hidden files
.docker .gitconfig .cache
# . shows hidden files
cd
- It changes directory
user@ubuntu:~ $ cd /home/user/ubuntu
user@ubuntu:~/ubuntu $ cd ..
# go up one directory level
pwd
- It shows current directory path
user@ubuntu:~ $ pwd
/home/user
mkdir
- It creates a new directory.
user@ubuntu:~ $ mkdir directory_name
user@ubuntu:~ $ mkdir test
test
rm
- It removes files or directories.
user@ubuntu:~ $ rm file_name
# remove file
user@ubuntu:~ $ rm -r directory
# remove directory and contents recursively
cp
- It copies files and directories
user@ubuntu:~ $ cp source_file destination
user@ubuntu:~ $ cp -r source_dir destination_dir
# copy directories, subdirectories and files recursively (-r)
mv
- It moves or rename files and directories
user@ubuntu:~ $ mv old_name new_name
user@ubuntu:~ $ mv file /new/path
sudo
- It allows a permitted user to execute a command as the superuser (root)
user@ubuntu:~ $ sudo command
user@ubuntu:~ $ sudo apt update
user@ubuntu:~ $ sudo apt install package_name
touch
- It creates an empty file or update file's timestamp
user@ubuntu:~ $ touch file_name
user@ubuntu:~ $ touch abc.txt
cat
- It views the contents of a file
user@ubuntu:~ $ cat file_name
user@ubuntu:~ $ cat abc.txt
acb123
nano, vim
- Command-line text editors
user@ubuntu:~ $ nano file_name
user@ubuntu:~ $ vim file_name
ps
- It displays currently running processes.
user@ubuntu:~ $ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 9 0.0 0.0 10572 5540 pts/0 S 18:32 0:00 -bash
root 74 0.0 0.0 11020 4556 pts/0 S 18:32 0:00 dockerd
top, htop
- Interactive process viewer.
user@ubuntu:~ $ top
top - 19:18:48 up 46 min, 0 users, load average: 0.00, 0.00, 0.00
Tasks: 8 total, 1 running, 7 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 12522.4 total, 11690.3 free, 185.2 used, 646.9 buff/cache
MiB Swap: 4096.0 total, 4096.0 free, 0.0 used. 12088.1 avail Mem
PIDUSER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
75 root 20 0 1845 1122 54884 0.7 0.9 0:21.04 dockerd
kill
- It terminates a process by PID.
user@ubuntu:~ $ kill PID_number
user@ubuntu:~$ sleep 100 &
# sleep 100 sec background
[1] 1425
user@ubuntu:~$ ps
PID TTY TIME CMD
1425 pts/0 00:00:00 sleep
user@ubuntu:~$ kill 1425
user@ubuntu:~$ kill -9 1425
# -9 force to terminate process
pkill
- It terminates processes by name.
user@ubuntu:~ $ pkill process_name
user@ubuntu:~ $ sleep 100 &
[1] 1402
user@ubuntu:~ $ pkill sleep
[1]+ Terminated sleep 100
df
- It shows disk usage of file systems.
user@ubuntu:~ $ df -h
# human-readable format
user@ubuntu:~ $ df -ah
Filesystem Size Used Avail Use% Mounted on
/dev/sdb 251G 23G 216G 10% /
tmpfs 6.2G 0 6.2G 0% /mnt/a
free
- It displays memory usage.
user@ubuntu:~ $ free -h
# human-readable format
total used free shared buff/cache available
Mem: 12Gi 164Mi 11Gi 0.0Ki 646Mi 11Gi
Swap: 4.0Gi 0B 4.0Gi
ifconfig
- It displays or configures network interfaces and IP.
user@ubuntu:~ $ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.7 netmask 255.255.240.0 broadcast 192.168.255.255
inet6 ab80::215:5cdd:efc2:8a3b prefixlen 64 scopeid 0x20<link>
ether 00:23:a3:b1:cd:ef txqueuelen 1000 (Ethernet)
RX packets 528 bytes 48315 (48.3 KB)
RX errors 0 dropped 2 overruns 0 frame 0
TX packets 25 bytes 1838 (1.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ping
- It tests connectivity to a network host.
user@ubuntu:~ $ ping example.com
user@ubuntu:~ $ ping 192.168.2.5
PING 192.168.2.5 (192.168.2.5) 56(84) bytes of data.
64 bytes from 192.168.2.5: icmp_seq=1 ttl=61 time=18.5 ms
^C
--- 192.168.2.5 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 18.461/19.088/19.716/0.627 ms
netstat
- It displays network connections, routing tables, and more.
user@ubuntu:~ $ netstat -tuln
# -t => tcp, -u => udp, -l => listening
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN
wget
- It downloads files from the internet.
user@ubuntu:~ $ wget http://example.com/file.zip
curl
- It tests and fetches information from URLs, APIs, and web pages.
user@ubuntu:~ $ curl http://ubuntu.com
user@ubuntu:~ $ curl -X POST -H "Content-Type: application/json" -d '{"name": "user", "age": 30}' http://example.com/api
# -X POST: Specifies the request type as POST.
# -H: Sets the request header, here specifying JSON content.
# -d: Sends data with the request.
user@ubuntu:~ $ curl -u username:password -O http://example.com/protectedfile.zip
chmod
- It changes file permissions.
user@ubuntu:~ $ chmod 755 file_name
# read, write, execute for owner; read, execute for others
# rwx => binary 111 = decimal 7
# owner, group, others
# - rwxrwxrwx => 777
chown
- It changes file owner and group.
user@ubuntu:~ $ chown user:group file_name
user@ubuntu:~ $ chown user2:developers example.txt
# user2: the new owner (user) of the file.
# developers: the new group associated with the file.
# example.txt: the name of the file whose ownership is being changed
tar
- Archive files (create, extract .tar, .tar.gz files)
user@ubuntu:~ $ tar -cvf archive.tar file_or_directory
# create archive
user@ubuntu:~ $ tar -xvf archive.tar
# extract archive
user@ubuntu:~ $ tar -czvf archive.tar.gz file_or_dir
# create compressed archive
grep
- It searches text within files.
user@ubuntu:~ $ grep "search_term" file_name
user@ubuntu:~ $ find /path/to/search | grep "pattern" # with pipe |
# pipe | => cmd1 | cmd2 => sends cmd1's output directly into cmd2
find
- It finds files and directories based on conditions.
user@ubuntu:~ $ find /path -name "file_name"
user@ubuntu:~ $ find / -name "history.txt" # type file by default
/home/user/history.txt
find: ‘/home/user/dockertest/volumedata’: Permission denied
user@ubuntu:~ $ find / -type d -name "docker" # type d=directory
/etc/docker
adduser
- It creates a new user.
user@ubuntu:~ $ adduser newuser1
user@ubuntu:~ $ usermod -aG sudo newuser1
# add the user to the sudo group
# with adduser command, the home folder for the user will be created as default
# with useradd command, there is no home folder for the user
passwd
- It sets or changes user password.
user@ubuntu:~ $ passwd user
# user => username
Changing password for user.
Current password:
New password:
Retype new password:
Password changed
apt
- It installs, removes, or manages packages Debian-based systems (like Ubuntu)
user@ubuntu:~ $ apt update
# update package list
user@ubuntu:~ $ apt install package_name
# install a package
user@ubuntu:~ $ apt remove package_name
# remove a package
yum
- It installs, removes, or manages packages RedHat-based systems (like CentOS)
[user@localhost ~]:# yum install package_name
# install a package
[user@localhost ~]:# yum remove package_name
# remove a package
Conclusion
Linux CLI commands are significant for all IT guys: developers, devops, ops, security, cloud developers. Mastering commands provides greater control over system operations, troubleshooting, and scripting.
If you found the tutorial interesting, I’d love to hear your thoughts in the blog post comments. Feel free to share your reactions or leave a comment. I truly value your input and engagement 😉
For other posts 👉 https://dev.to/omerberatsezer 🧐
Follow for Tips, Tutorials, Hands-On Labs for AWS, Kubernetes, Docker, Linux, DevOps, Ansible, Machine Learning, Generative AI, SAAS.
https://github.com/omerbsezer/
https://www.linkedin.com/in/omerberatsezer/
Top comments (0)