Linux Basic commands
Linux Basic commands
As you can see in the above image, using the command by itself without any arguments
will give us an output with all the files and directories in the directory. The command
offers a lot of flexibility in terms of displaying the data in the output.
Now, your terminal prompt should usually have the complete directory anyway. But in
case it doesn’t, this can be a quick command to see the directory that you’re in. Another
application of this command is when creating scripts where this command can allow us
to find the directory where the script has been saved.
As you can see in the above command, I simply typed cd /etc/ to get into the /etc
directory. We used the pwd command to print the current working directory.
The mkdir command allows you to create directories from within the terminal.
root@ubuntu:~# mkdir <folder name>
Copy
As you can see in the above screenshot, we created the JournalDev directory with just
this simple command.
In the above command, we created a copy of the file named Sample. Let’s see how
what happens if we use the mv command in the same manner.
root@ubuntu:~# mv <source> <destination
Copy
In the above case, since we were moving the file within the same directory, it acted as a
rename. The file name is now changed.
To delete a directory, you must add the -r argument to it. Without the -r argument,
the rm command won’t delete directories.
root@ubuntu:~# rm -r <folder/directory name>
Copy
The -r flag in the rm command in Linux stands for “recursive”. When used with
the rm command, it will remove not only the specified file but also all of its subdirectories
and the files within those subdirectories recursively.
Note: It’s important to be careful when using the rm command with the -r flag, as it can
quickly and permanently delete a large number of files and directories. It’s a good idea to
use the -i flag in conjunction with the -r flag, which will prompt you for confirmation before
deleting each file and directory.
For example, to remove the mydir directory and its contents with confirmation, you can use
this command:
root@ubuntu:~# rm -ri mydir
Copy
This will prompt you for confirmation before deleting each file and directory within
the mydir directory.
The touch command in Linux
The touch command in Linux creates an empty file or updates the timestamp of an
existing file.
root@ubuntu:~# touch <file name>
Copy
The -s flag creates a symbolic link (also known as a symlink or soft link) to a file or
directory. A symbolic link is a special type of file that acts as a shortcut or pointer to
another file or directory.
By default, the ln command will make hard links instead of symbolic or soft links.
Note: Say you have a text file. If you make a symbolic link to that file, the link is only a
pointer to the original file. If you delete the original file, the link will be broken, as it no longer
has anything to point to.
A hard link is a mirror copy of an original file with the exact same contents. Like symbolic
links, if you edit the contents of the original file, those changes will be reflected in the hard
link. If you delete the original file, though, the hard link will still work, and you can view and
edit it as you would a normal copy of the original file.
The clear command in Linux
The clear command in Linux clears the terminal screen. It removes all the text and
output currently displayed on the terminal and gives you a clean slate to work with.
Here is an example of how to use the clear command:
root@ubuntu:~# clear
Copy
This will clear the terminal screen and move the cursor to the top-left corner of the
screen.
You can also use the clear command in combination with other commands, like this:
root@ubuntu:~# ls -l; clear
Copy
This will list the files and directories in the current directory, and then clear the terminal
screen.
Note: The clear command does not delete any files or data from your system. It only
affects the display of the terminal.
As you can see in the above example, the cat command, when used on our New-File,
prints the contents of the file. At the same time, when we use echo command, it simply
prints whatever follows after the command.
The less command is used when the output printed by any command is larger than the
screen space and needs scrolling. The less command allows the user to break down
the output and scroll through it with the use of the enter or space keys.
The simple way to do this is with the use of the pipe operator (|).
root@ubuntu:~# cat /boot/grub/grub.cfg | less
Copy
Note: Use the -S flag with less to enable line wrapping. This will allow you to view long
lines of text without scrolling horizontally.
Use the -N flag with less to display line numbers. This can be useful when you need to
know the line number of a specific piece of text.
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by
default).
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
Let’s see the output of both the commands and the way we can use these.
root@ubuntu:~# uname -a
Copy
The parameter -a with uname command stands for “all”. This prints out the complete
information. If the parameter is not added, all you will get as the output is “Linux”.
Note: Some important flags you can use with the uname command.
1. Use uname -s to display the kernel name.
2. Use uname -n to display the hostname.
3. Use uname -r to display the kernel release.
4. Use uname -v to display the kernel version.
5. Use uname -m to display the machine hardware name.
The tar, zip, and unzip commands
The tar command in Linux is used to create and extract archived files. We can extract
multiple different archive files using the tar command.
To create an archive, we use the -c parameter, and to extract an archive, we use the -
x parameter. Let’s see how it works.
#Compress
root@ubuntu:~# tar -cvf <archive name> <files separated by space>
#Extract
root@ubuntu:~# tar -xvf <archive name>
Copy
In the first line, we created an archive named Compress.tar with the New-File and
New-File-Link. In the next command, we have extracted those files from the archive.
Let’s discuss the zip and unzip commands. Both are very straightforward. You can use
them without any parameters, and they’ll work as intended. Let’s see an example below.
root@ubuntu:~# zip <archive name> <file names separated by space>
root@ubuntu:~# unzip <archive name>
Copy
Since we already have those files in the same directory, the unzip command prompts
us before overwriting those files.
The grep command in Linux
The grep command is a powerful and versatile text search tool in Linux and Unix-based
operating systems. It can search for specific patterns or strings in one or more files and
filter the output of other commands.
The grep command stands for “global regular expression print,” which reflects its ability
to search for regular expressions across multiple lines and files.
root@ubuntu:~# <Any command with output> | grep "<string to find>"
Copy
The tail command outputted the bottom 10 lines from the file.
These commands can be used to quickly view a file’s contents, monitor real-time
updates for troubleshooting issues, filter output from other commands, and perform log
analysis.
The cmp command is used to compare two files and display the first byte that is different
between them. It can be used to identify differences between binary files or to check for
corruption in files.
root@ubuntu:~# cmp <file 1> <file 2>
Copy
The cmp command only tells us the line number, which is different. Not the actual text.
The comm command is used to compare two sorted files and display the lines that are
unique to each file, as well as the lines that are common to both files.
root@ubuntu:~# comm <file 1> <file2>
Copy
The text that’s aligned to the left is only present in file 1. The centre-aligned text is
present only in file 2. And the right-aligned text is present in both files.
By the looks of it, comm command makes the most sense when we’re trying to compare
larger files and would like to see everything arranged together.
All three of these commands are essential tools for working with files in Linux and Unix-
based operating systems. By understanding how to use the diff, comm,
and cmp commands effectively, you can identify differences between files, merge
changes, and perform other file comparison tasks.
These commands can help you to identify and resolve issues with files, as well as to
track changes and maintain version control. Whether you’re a developer or a system
administrator, these commands are an essential part of your toolkit.
By default, the sort command sorts lines in ASCII collating sequence, which can lead
to unexpected results when sorting numbers or special characters. To sort numbers in
numerical order, you can use the -n option.
Here’s an example of using the -n option:
root@ubuntu:~# sort -n file.txt
Copy
The above command will sort the lines in file.txt in numerical order.
The sort command can also be used to sort lines based on specific fields using the -
k option.
Here’s an example of using the -k option:
root@ubuntu:~# sort -k 2 file.txt
Copy
This command will sort the lines in file.txt based on the second field.
The sort command is a powerful and flexible tool for working with text files in Linux and
Unix-based operating systems. By understanding how to use the sort command
effectively, you can sort lines in text files, sort lines based on specific fields, and perform
other sorting operations.
These commands can help you organize and analyze data and perform other file
manipulation tasks. Whether you’re a developer or a system administrator,
the sort command is an essential part of your toolkit.
Jump back to commands list ↑
The export command in Linux
The export command in Linux and Unix-based operating systems is used to set
environment variables. Environment variables are used to store information that can be
used by processes or commands.
As you can see in the image, the ssh server is running on our system.
The ps, kill, and killall commands
The ps, kill, and killall commands are all used to manage processes in
Linux.
The ps command is used to display information about the current running
processes on the system. Here are some examples of using the ps command:
Display a list of all running processes:
root@ubuntu:~ ps -ef
Copy
Display a list of all processes for a specific process ID (PID):
root@ubuntu:~ ps -p PID
Copy
Let’s see all of this in action:
root@ubuntu:~ ps
root@ubuntu:~ kill <process ID>
root@ubuntu:~ killall <process name>
Copy
For demonstration purposes, we will create a shell script with an infinite loop and
will run it in the background.
With the use of the & symbol, we can pass a process into the background. As you
can see, a new bash process with PID 14490 is created.
Now, to kill a process with the kill command, you can type kill followed by the
PID(Process Id) of the process.
But if you do not know the process ID and just want to kill the process with the
name, you can make use of the killall command.
You will notice that PID 14490 stayed active. That is because, both times, we
killed the sleep process.
The df and mount commands
When working with Linux, the df and mount commands are very efficient utilities
to mount filesystems and get details of the file system.
The df command is used to display the amount of disk space used and available
on the file systems, and the mount command is used to mount a file system or
device to a specific directory.
When we say mount, it means that we’ll connect the device to a folder so we can
access the files from our filesystem. The default syntax to mount a filesystem is
below:
root@ubuntu:~ mount /dev/cdrom /mnt
root@ubuntu:~ df -h
Copy
In the above case, /dev/cdrom is the device that needs to be mounted. Usually,
a mountable device is found inside the /dev folder. mnt is the destination folder to
which to mount the device. You can change it to any folder you want, but we
have used /mnt as it’s the system’s default folder for mounting devices.
To see the mounted devices and get more information about them, we use the df
command. Just typing df will give us the data in bytes, which is not readable. So,
we’ll use the -h parameter to make the data human-readable.
The chmod and chown commands
The chmod and chown commands are used to modify file permissions and
ownership in Linux.
The chmod command is used to change the permissions of a file or directory, and
the chown command is used to change the ownership of a file or directory
The default syntax for both the commands is chmod <parameter>
filename and chown <user:group> filename
root@ubuntu:~ chmod +x loop.sh
root@ubuntu:~ chmod root:root loop.sh
Copy
In the above example, we’re adding executable permissions to the loop.sh file
with the chmod command. In addition, with the chown command, we’ve made it
accessible only to the root user and users within the root group.
As you will notice, the root root part is now changed to www-data which is the
new user who has full file ownership.
The ifconfig and traceroute commands
The ifconfig and traceroute commands manage network interfaces and trace
the route of network packets in Linux.
The ifconfig command will give you the list of all the network interfaces along
with the IP addresses, MAC addresses and other information about the interface.
root@ubuntu:~ ifconfig
Copy
There are multiple parameters that can be used, but we’ll work with the basic
command here.
The traceroute command is used to trace the route of network packets and
determine the path they take to reach a specific destination.
When working with traceroute, you can simply specify the IP address,
hostname, or domain name of the endpoint.
root@ubuntu:~ traceroute <destination address>
Copy
Now, obviously, localhost is just one hop (the network interface itself). You can
try this same command with any other domain name or IP address to see all the
routers your data packets pass through to reach the destination.
The wget command in Linux
If you want to download a file from within the terminal, the wget command is one
of the handiest command-line utilities available. It is one of the important Linux
commands you should know when working with source files.
When you specify the link for download, it has to directly be a link to the file. If the
file cannot be accessed by the wget command, it will simply download the
webpage in HTML format instead of the actual file that you wanted.
Let’s try an example. The basic syntax of the wget command is :
root@ubuntu:~ wget <link to file>
Copy
Or,
root@ubuntu:~ wget -c <link to file>
The ufw and iptables commands
The ufw and iptables commands are used to manage firewalls in Linux.
UFW and IPTables are firewall interfaces for the Linux Kernel’s netfilter firewall.
IPTables directly passes firewall rules to Netfilter while UFW configures the rules
in IPTables, which then sends those rules to Netfilter.
Why do we need UFW when we have IPTables? Because IPTables is pretty
difficult for a newbie. UFW makes things extremely easy. See the below example
where we are trying to allow port 80 for our webserver.
root@ubuntu:~# iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
root@ubuntu:~# ufw allow 80
Copy
I’m sure you now know why UFW was created! Look at how easy the syntax
becomes. Both these firewalls are very comprehensive and can allow you to
create any kind of configuration required for your network. Learn at least the
basics of UFW or IPTables firewall, as these are the Linux commands you must
know.
Below are the commands on how to use these package managers on different Linux
distributions.
Getting yourself well versed with the package manager of your distribution will make
things much easier for you in the long run. So even if you have a GUI based package
management tool installed, try an make use of the CLI based tool before you move on
to the GUI utility. Add these to your list of Linux commands you must know.
The cal command displays a well-presented calendar on the terminal. Just enter the
command cal on your terminal prompt.
root@ubuntu:~# cal
root@ubuntu:~# cal March 2024
Copy
Although we don’t need it often, it’s a great addition! It’s an amazing option for terminal
fans.
If you know a command that you run very often, it’s time to create an alias.
What’s an alias? In simple terms, it’s another name for a command that you’ve defined.
root@ubuntu:~# alias lsl="ls -l"
OR
root@ubuntu:~# alias rmd="rm -r"
Copy
Now, every time you enter lsl or rmd in the terminal, you’ll receive the output that you’d
have received if you had used the full commands.
The examples here are for really small commands that you can still type by hand every
time. But in some situations where a command has too many arguments that you need
to type, it’s best to create a shorthand version of the same.
The dd command in Linux is a versatile command used for low-level copying and
conversion of data. It stands for “data-description” or “data definition,” and it can be
used to copy and convert data between different file formats and storage devices.
For example, if we wanted to back up the entire hard drive as is to another drive, we
would use the dd command.
root@ubuntu:~# dd if=/dev/sdb of=/dev/sda
Copy
The if and of arguments stand for input file and output file.
It’s a powerful and flexible tool, but it can also be dangerous if not used carefully.
Always double-check your syntax and make sure you know what the command will do
before executing it.
This will sort the process list by memory usage, with the most memory-intensive processes
at the top.
It’s a powerful and flexible tool for monitoring system activity and troubleshooting
performance issues.
The above command will create a new user named JournalDev with the home directory
as /home/JD.
The usermod command, on the other hand, is used to modify existing users. You can
modify any value of the user including the groups, the permissions, etc.
For example, if you want to add more groups to the user, you can type in:
root@ubuntu:~# usermod JournalDev -a -G sudo, audio, mysql
This will prompt you to enter a new password for the current user.
Replace username with the name of the user whose password you want to change.
3. Force a user to change their password at the next login:
root@ubuntu:~# passwd -f username
Copy
4. Set an expiration date for a user’s password:
root@ubuntu:~# passwd -e -n days -w warndays username
Copy
Replace days with the number of days before the password expires and warm days with
the number of days before the password expires that the user will be warned.
These are just a few examples of using the passwd command in Linux. By
understanding how to use this command effectively, you can manage user accounts
and ensure that your system is secure.