Learn basic
commands for Linux
What Is Linux?
Linux is an operating system's kernel. You might have heard of UNIX. Well, Linux is a UNIX clone. But it was actually created by Linus Torvalds from Scratch. Linux is free and open-source, that means that you can simply change anything in Linux and redistribute it in your own name! There are several Linux Distributions, commonly called “distros”.Ubuntu Linux
- Red Hat Enterprise Linux
- Linux Mint
- Debian
- Fedora
Linux is Mainly used in servers.
About 90% of the internet is powered by Linux servers. This is because Linux is
fast, secure, and free! The main problem of using Windows servers are their
cost. This is solved by using Linux servers. The OS that runs in about 80% of
the smartphones in the world, Android, is also made from the Linux kernel. Most
of the viruses in the world run on Windows, but not on Linux!
Tips and Tricks for Using Linux Command Line
- You can use the clear command to clear the
terminal if it gets filled up with too many commands.
- TAB can be used to fill up in terminal. For
example, You just need to type “cd Doc” and then TAB and
the terminal fills the rest up and makes it “cd Documents”.
- Ctrl+C can be used to stop any command in
terminal safely. If it doesn't stop with that, then Ctrl+Z can
be used to force stop it.
- You can exit from the terminal by using the exit command.
- You can power off or reboot the computer by using the command sudo halt and sudo reboot.
1. pwd command
When you first open the terminal, you are in the home directory of your user. To know which directory you are in, you can use the “pwd” command. It gives us the absolute path, which means the path that starts from the root. The root is the base of the Linux file system. It is denoted by a forward slash( / ). The user directory is usually something like "/home/username".
2. ls command
Use the "ls" command to know what files are in the directory you are in. You can see all the hidden files by using the command “ls -a”.
3. cd command
Use the "cd" command to go to a directory. For example, if you are in the home folder, and you want to go to the downloads folder, then you can type in “cd Downloads”. Remember, this command is case sensitive, and you have to type in the name of the folder exactly as it is. But there is a problem with these commands. Imagine you have a folder named “Raspberry Pi”. In this case, when you type in “cd Raspberry Pi”, the shell will take the second argument of the command as a different one, so you will get an error saying that the directory does not exist. Here, you can use a backward slash. That is, you can use “cd Raspberry\ Pi” in this case. Spaces are denoted like this: If you just type “cd” and press enter, it takes you to the home directory. To go back from a folder to the folder before that, you can type “cd ..” . The two dots represent back.
4. mkdir & rmdir command
Use the mkdir command when you need to create a folder or a directory. For example, if you want to make a directory called “DIY”, then you can type “mkdir DIY”. Remember, as told before, if you want to create a directory named “DIY Hacking”, then you can type “mkdir DIY\ Hacking”. Use rmdir to delete a directory. But rmdir can only be used to delete an empty directory. To delete a directory containing files, use rm.
5. rm command
Use the rm command to delete files and directories. Use "rm -r" to delete just the directory. It deletes both the folder and the files it contains when using only the rm command.
6. touch command
The touch command is used to create a file. It can be anything, from an empty txt file to an empty zip file. For example, “touch new.txt”.
7. man & --help command
To know more about a command and how to use it, use the man command. It shows the manual pages of the command. For example, “man cd” shows the manual pages of the cd command. Typing in the command name and the argument helps it show which ways the command can be used (e.g., cd –help).
8. cp command
Use the cp command to copy files through the command line. It takes two arguments: The first is the location of the file to be copied, the second is where to copy.
9. mv command
Use the mv command to move files through the command line. We can also use the mv command to rename a file. For example, if we want to rename the file “text” to “new”, we can use “mv text new”. It takes the two arguments, just like the cp command.
10. locate command
The locate command is used to locate a file in a Linux system, just like the search command in Windows. This command is useful when you don't know where a file is saved or the actual name of the file. Using the -i argument with the command helps to ignore the case (it doesn't matter if it is uppercase or lowercase). So, if you want a file that has the word “hello”, it gives the list of all the files in your Linux system containing the word "hello" when you type in “locate -i hello”. If you remember two words, you can separate them using an asterisk (*). For example, to locate a file containing the words "hello" and "this", you can use the command “locate -i *hello*this”.
11. alias command
alias is
a useful shell built-in command for creating aliases (shortcut) to a Linux
command on a system. It is helpful for
creating new/custom commands from existing Shell/Linux commands (including
options):
$ alias home='cd /home/tecmint/public_html'
12. arp command
ARP (Address Resolution Protocol)
is a protocol that maps IP network addresses of a network neighbor with the
hardware (MAC) addresses in an IPv4 network.
You can use it as below to find all alive hosts on a network:
$ sudo arp-scan --interface=enp2s0 --localnet
13. clear command
clear command
lets you clear the terminal screen, simply type.
$ clear
14. cmp command
cmp performs
a byte-by-byte comparison of two files like this.
$ cmp file1 file2
15. history command
history command is used to show previously
used commands or to get info about command executed by a user.
$ history
16. echo command
The "echo" command helps us move some data, usually text into a file. For example, if you want to create a new text file or add to an already made text file, you just need to type in, “echo hello, my name is alok >> new.txt”. You do not need to separate the spaces by using the backward slash here, because we put in two triangular brackets when we finish what we need to write.
17. cat command
Use the cat command to display the contents of a file. It is usually used to easily view programs.
18. nano, vi, jed command
nano and vi are already installed text editors in the Linux command line. The nano command is a good text editor that denotes keywords with color and can recognize most languages. And vi is simpler than nano. You can create a new file or modify a file using this editor. For example, if you need to make a new file named "check.txt", you can create it by using the command “nano check.txt”. You can save your files after editing by using the sequence Ctrl+X, then Y (or N for no). In my experience, using nano for HTML editing doesn't seem as good, because of its color, so I recommend jed text editor. We will come to installing packages soon.
19. sudo command
A widely used command in the Linux command line, sudo stands for "SuperUser Do". So, if you want any command to be done with administrative or root privileges, you can use the sudo command. For example, if you want to edit a file like viz. alsa-base.conf, which needs root permissions, you can use the command – sudo nano alsa-base.conf. You can enter the root command line using the command “sudo bash”, then type in your user password. You can also use the command “su” to do this, but you need to set a root password before that. For that, you can use the command “sudo passwd”(not misspelled, it is passwd). Then type in the new root password.
20. df command
Use the df command to see the available disk space in each of the partitions in your system. You can just type in df in the command line and you can see each mounted partition and their used/available space in % and in KBs. If you want it shown in megabytes, you can use the command “df -m”.
21. du command
Use du to know the disk usage of a file in your system. If you want to know the disk usage for a particular folder or file in Linux, you can type in the command df and the name of the folder or file. For example, if you want to know the disk space used by the documents folder in Linux, you can use the command “du Documents”. You can also use the command “ls -lah” to view the file sizes of all the files in a folder.
22. tar command
Use tar to work with
tarballs (or files compressed in a tarball archive) in the Linux command line.
It has a long list of uses. It can be used to compress and uncompress different
types of tar archives like .tar, .tar.gz, .tar.bz2,etc. It works on
the basis of the arguments given to it. For example, "tar -cvf"
for creating a .tar archive, -xvf to untar a tar
archive, -tvf to list the contents of the archive, etc. Since it is
a wide topic,
Extract a
tar.gz archive
# tar -xvzf
bigfile.tar.gz
Create a tar/tar.gz archive
# tar -cvf
Videos.tar ./Latest/
23. zip, unzip command
Use zip to compress files into a zip archive,
and unzip to extract files from a zip archive.
Install Zip on Ubuntu and Debian
#sudo apt install
zip
Install Zip on CentOS and Fedora
#sudo yum install
zip
Syntax:
#zip [options]
zipfile files_list
Syntax for Creating
a zip file:
$zip myfile.zip
filename.txt
Unzip
$unzip myfile.zip
24. uname command
Use uname to show the information about the system your Linux distro is running. Using the command “uname -a” prints most of the information about the system. This prints the kernel release date, version, processor type, etc.
25. apt-get command
Use apt to work with packages in the Linux command line. Use apt-get to install packages. This requires root privileges, so use the sudo command with it. For example, if you want to install the text editor jed (as I mentioned earlier), we can type in the command “sudo apt-get install jed”. Similarly, any packages can be installed like this. It is good to update your repository each time you try to install a new package. You can do that by typing “sudo apt-get update”. You can upgrade the system by typing “sudo apt-get upgrade”. We can also upgrade the distro by typing “sudo apt-get dist-upgrade”. The command “apt-cache search” is used to search for a package. If you want to search for one, you can type in “apt-cache search jed”(this doesn't require root).
26. chmod command
Use chmod to make a file executable and to change the permissions granted to it in Linux. Imagine you have a python code named numbers.py in your computer. You'll need to run “python numbers.py” every time you need to run it. Instead of that, when you make it executable, you'll just need to run “numbers.py” in the terminal to run the file. To make a file executable, you can use the command “chmod +x numbers.py” in this case. You can use “chmod 755 numbers.py” to give it root permissions or “sudo chmod +x numbers.py” for root executable.
27. hostname command
Use hostname to know your name in your host or network. Basically, it displays your hostname and IP address. Just typing “hostname” gives the output. Typing in “hostname -I” gives you your IP address in your network.
28. ping
Use ping to check your connection to a server. Wikipedia says, "Ping is a computer network administration software utility used to test the reachability of a host on an Internet Protocol (IP) network". Simply, when you type in, for example, “ping google.com”, it checks if it can connect to the server and come back. It measures this round-trip time and gives you the details about it. The use of this command for simple users like us is to check your internet connection. If it pings the Google server (in this case), you can confirm that your internet connection is active!
29. wget command
You can use the wget command if you want to download a file from a server to your local computer:
The command will attempt to download the resource you supplied
and it will automatically detect the file extension. For example: If you point
it to a website, it’ll download an HTML file. If you’re transferring a large
file, you can simply log out and the tool will finish the task in the
background.
30. ssh command
SSH command is used to login into
remote host. For example the below ssh command will connect to remote host (192.168.50.2) using user
as narad.
# ssh narad@192.168.50.2
To check the version of ssh use
option -V (uppercase)
shows version of ssh.
# ssh -V
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
31. service Command
Service command
call script located at /etc/init.d/ directory
and execute the script. There are two ways to start the any service. For example,
we start the service called httpd with
service command.
# service httpd start
OR
# /etc/init.d/httpd start
he basic syntax of Linux service is:
$ service [Script] [Action] [Option]
If you don’t specify the “[Action]” parameter, you’ll see all allowed actions in the command line.
What options and actions does Linux service have?¶
Strictly speaking, Linux service only has the default options “-h” or “–help”, “-v” or “–version”, and “-status-all”. However, you can specify the command with various actions. These are the most important ones:
- start: Starts the script.
- stop: Stops the script.
- status: Shows the current status of the script.
- restart: Ensures that the script is restarted.
Examples of using the Linux service command¶
To help you understand how the service command is used in practice, we’ll conclude with some examples of its main use.
$ sudo service sshd start
This starts the sshd script.
$ sudo service sshd stop
This forces the sshd script to stop.
$ sudo service sshd restart
This command restarts the corresponding script.
$ sudo service sshd status
This command is used to query the status of the script.
32. tar Command
tar command
is used to compress files and folders in Linux. For example the below command
will create a archive for /home directory
with file name as archive-name.tar.
# tar -cvf archive-name.tar /home
To extract tar archive file use the option as follows.
# tar -xvf archive-name.tar
33. grep Command
grep search for a given string in
a file. Only tecmint user
displays from /etc/passwd file.
we can use -i option
for ignoring case sensitive.
# grep tecmint /etc/passwd
tecmint:x:500:500::/home/tecmint:/bin/bash
34. yum command (RHEL/CentOS/Fedora)
The Yellowdog Updater,
Modified (YUM) is a free and open-source command-line package-management
utility for computers running the Linux operating system using the RPM Package
Manager.
RPM Package Manager (RPM)
(originally Red Hat Package Manager, now a recursive acronym) is a free and
open-source package management system.[5] The name RPM refers to .rpm file
format and the package manager program itself. RPM was intended primarily for
Linux distributions; the file format is the baseline package format of the
Linux Standard Base.
YUM can perform operations such as:
· installing packages
· deleting packages
· updating existing installed packages
· listing available packages
· listing installed packages
Install a
Package with YUM
# yum install firefox
Removing a Package with YUM
# yum remove firefox
Updating a Package using YUM
# yum update mysql
List a
Package using YUM
# yum list openssh
Search for a
Package using YUM
# yum search vsftpd
Get
Information of a Package using YUM
# yum info firefox
List all
Available Packages using YUM
# yum list | less
List all
Installed Packages using YUM
# yum list installed
Install a Group Packages
# yum groupinstall 'MySQL Database'
35. ifconfig command
ifconfig command is used to configure a
Linux systems network interfaces. It is used to configure, view and control
network interfaces.
$ ifconfig
$ sudo ifconfig eth0 up
$ sudo ifconfig eth0 down
$ sudo ifconfig eth0 172.16.25.125
36. ip command
ip command is used to display or
manage routing, devices, policy routing and tunnels. It also works as a
replacement for well known ifconfig command.
This command will assign an IP address to a specific interface
(eth1 in this case).
$ sudo ip addr add 192.168.56.10 dev eth1
37. last command
last command display
a listing of last logged in users.
$ last
38. login command
login command is
used to create a new session with the system. You’ll be asked to provide a
username and a password to login as below.
$ sudo login
39. passwd command
passwd command is
used to create/update passwords for user accounts, it can also change the
account or associated password validity period. Note that normal system users
may only change the password of their own account, while root may modify the
password for any account.
$ passwd tecmint
40. reboot command
reboot command may
be used to halt, power-off or reboot a system as
follows.
$ reboot
41. shutdown command
shutdown command schedules a time for the
system to be powered down. It may be used to halt, power-off or reboot the
machine like this.
$ shutdown --poweroff
42. su command
su command is
used to switch to another user ID or become root during a login session. Note
that when su is
invoked without a username, it defaults to becoming root.
$ su
$ su tecmint
43. users command
users command shows
the user names of users currently logged in to the current host like this.
$ users
44. ping command
ping command is
used to determine connectivity between hosts on a network (or the Internet):
$ ping google.com
45. nslookup command
nslookup (name server lookup)
is a tool used to perform DNS lookups in Linux. It is used to display DNS
details, such as the IP address of a particular computer, the MX records for a
domain or the NS servers of a domain.
46. systemctl command
How To Use Systemctl to Manage Systemd Services and Units?
systemd
is an init system and system manager that has widely
become the new standard for Linux distributions. we will be discussing
the systemctl
command, which is the central management
tool for controlling the init system.
Starting and Stopping Services
To
start a systemd
service,
executing instructions in the service’s unit file, use the start
command. If you are running as a non-root user, you will
have to use sudo
since this will
affect the state of the operating system:
$ sudo systemctl start application.service
NB: application means httpd, mysqld, sshd etc
As we mentioned above, systemd
knows to look for *.service
files for service management commands, so the command
could just as easily be typed like this:
$ sudo systemctl start application
Although you may use the above format for general
administration, for clarity, we will use the .service
suffix for the remainder of the commands, to be explicit
about the target we are operating on.
To
stop a currently running service, you can use the stop
command instead:
$ sudo systemctl stop application.service
Enabling and Disabling Services
The
above commands are useful for starting or stopping services during the current
session. To tell systemd
to start services
automatically at boot, you must enable them.
To
start a service at boot, use the enable
command:
$ sudo systemctl enable application.service
To disable the service from starting automatically, you can
type:
$ sudo
systemctl disable application.service
This will remove the symbolic link that indicated that the
service should be started automatically.
Keep
in mind that enabling a service does not start it in the current session. If
you wish to start the service and also enable it at boot, you will have to
issue both the start
and enable
commands.
Checking the Status of Services
To
check the status of a service on your system, you can use the status
command:
$ systemctl
status application.service
Listing Current
Units
Units are the objects that systemd
knows how to manage. To see a list of all
of the active units that systemd
knows about, we
can use the list-units
command:
$ systemctl
list-units
We can tell systemctl
to output
different information by adding additional flags. For instance, to see all of
the units that systemd
has loaded (or
attempted to load), regardless of whether they are currently active, you can
use the --all
flag, like this:
$ systemctl list-units --all
To list all loaded services on your system (whether active; running, exited, or failed, use the list-units subcommand and --type
switch with a value of service.
47. man command
man command in Linux is used to display the user manual of any command that we can run on
the terminal. It provides a detailed view of the command which includes NAME, SYNOPSIS,
DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES,
AUTHORS and SEE ALSO.
Every manual is divided into the following sections:
Executable programs or shell commands
System calls (functions provided by the kernel)
Library calls (functions within program libraries
Games
Special files (usually found in /dev)
File formats and conventions eg /etc/passwd
Miscellaneous (including macro packages and conventions),
System administration commands (usually only for root)
Kernel routines [Non standard]
Syntax :
$man [OPTION]... [COMMAND NAME]...
Options and Examples
No Option: It displays the whole manual of the command.
Syntax :
$ man [COMMAND NAME]
48. uptime command
uptime
into your terminal and press Enter
, the command returns four key pieces of information: the current time, how long the system has been running, how many users are currently logged in, and the system load averages.In this example, the uptime
command is showing:
- The current system time (
15:30:00
) - The system’s uptime (
10 days and 8 hours
) - The number of users currently logged in (
2 users
) - The system load averages (
0.05, 0.04, 0.05
)
49. netstat command
`netstat`
stands for network statistics. It allows users to display network-related information and diagnose various networking issues. The command has several options that can be combined to retrieve specific details.
Basic Syntax of `netstat`Command in Linux
Below is the general syntax of the netstat
command:
netstat [options]
50. route command
The route command is used to view and make changes to the kernel routing table. running route at the command line without any options displays the routing table entries:
$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.1.2 0.0.0.0 UG 1024 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0