Just like the Windows operating system, when using Linux, you should also learn basic Linux commands to make manipulations quicker and easier. Here are some basic commands on Linux you should know.
With some commands below, you will see commands with simple explanations, specific examples that help you or the Linux users you are supporting to use this operating system in general and the command on Linux in particular more effectively.
1. Some important notes about Linux Terminal
To open Terminal quickly from GUI, press the key combination Clt + Alt + T
Operate most Linux commands:
[sudo] command [optional switch] [file or directory path]
Using sudo will run any command under Admin. Most Linux commands are used to install/remove program system files, are used to request sudo.
2. Get familiar with the account
These commands will help new Linux users get familiar with their Linux accounts.
Command |
Function |
Example |
pwd |
Display the current position in file |
pwd |
whoami |
Display user’s name – most useful if convert user with su command and need to be reminded about which account is being used |
whoami |
ls |
Provide a file list. With the –a parameter, the command displays the files whose name starts with a dot (eg: .bashrc). With the –l parameter, the command displays the right, the file size and the latest updated date/time. |
ls |
env |
Display user environment settings (for example: search path, saved history size, main directory, etc…) |
env |
echo |
Repeat the text that the user provides or display the value of some variables. |
echo hello |
history |
List the previous commands given. |
history |
passwd |
Change the password. Note that complex requirements can be enforced. |
passwd |
3. Create a new folder
mkdir, which is the manipulation command with the basic folder on Linux, helps you create a new folder quickly. The statement syntax:
mkdir folder
Note:
Folder is the name of the directory you want to create. For example, if you want to create a folder called backup, the syntax is:
mkdir /home/marin/backup
In case if you want to create a folder containing many subfolders, you can use the “-p” option. Suppose, you have the foo folder and have the right to access to it:
mkdir -p /foo/bar/baz
The above command will create a bar folder, a baz folder located in the bar; bar and baz located in/foo already.
4. Search for the current folder
If you want to search for your current folder, you can use pwd command.
For example:
marin@[LinuxVeda]:[~/work]$ pwd
/home/marin/work
5. Create and
edit files
The Linux systems provide commands to create files. Users can select the desired text editor. Some commands require users to be proficient before using, while others are quite simple.
Command | Function | Example |
nano | An easy-to-use text editor, requires users to move through the file with the arrow keys and provide control sequences to locate text, save changes, etc. | nano myfile |
vi | A more sophisticated editor, allows users to enter commands to find and change text, make general changes, etc. | vi myfile |
ex | A text editor is designed for programmers and includes both visual and command line modes. | ex myfile |
touch | Create a file if it has not already existed or update the timestamp if it was created. |
touch
newfile touch updatedfile |
> | Create file by directing the output to them. > create a file while >> attach an existing file. |
cal >
calendar ps > myprocs date >> date.log |
6. Read some
files
Normally, you have to consider the content of many different files. Regularly reviewing different files is often quite complicated and time-consuming. So the easiest way to read their contents is to use the cat command. Statement syntax is quite simple:
cat FILE
Just change FILE with file name you want to read.As a result, you will see the file’s content appearing at the end of Terminal.
For example:
cat script.sh
Besides, you can use the cat command for many files at the same time:
cat FILE-1 FILE-2
The output of the command will display the content of the files in the order of the content of FILE – 1 then the content of FILE -2.
Moreover, Linux provides some commands to review the content and nature of files. Here are some of the most useful commands.
Command | Function | Example |
cat | Display the whole content of a text file. | cat .bashrc |
more | Display the content of the text file. Press the spacebar to move to each additional segment. | more .bash_history |
less | Display the content of the text file, but let go back with the up arrow key. | less .bash_history |
file | Identify files by type (eg: ASCII text, executable file, image, folder) |
file myfile file ~/.bashrc file /bin/echo |
7. Copy or move files
Copying or moving files in Linux
terminal is quite simple and easy.
• To copy a
file, you can use the command: cp.
• To move a
file, you use the command: mvoflder.
Using both 2
commands is quite simple. With the cp
command. To copy a file, you enter the file name and enter the new copy file
name. For example:
cp file1 file2
On the above statement copy file1
and create file2 containing file1 content. You can use cp to copy the folder. It is important to note that when you want
to copy a folder you should use the -r
option.
In other words, cp -r will copy the
content of a certain file (and subfolders) to the folder you select. In
addition, you can use cp with full path:
cp -r /home/marin/work/ /home/marin/backup
The above statement will copy the
content of “work” folder to the new folder named “backup”.
If you want to copy all the files and the folder of a folder to another folder,
you can use the “*” character. Character is used to find appropriate ports (in
this case files and folders). For example:
cp /home/marin/work/* /home/marin/backup/
Next, the mv command. The command
syntax:
mv file1 file2
The above statement renames file1 to file2. The same goes for folder. However, if you specify a file and a folder, the file will be moved into the folder. For example:
mv /home/marin/file1 /home/marin/work/
In the above command, move file1 from “/ home / marin /” to “/ home / marin / work /”. If you want to move all the files in a folder to another folder, you can use the “*” character:
mv /home/marin/work/* /home/marin/backup/
8. Delete files and folders
If you want to delete a file or a folder, you can use the rm command. It is vital to note that when you use this command to delete a file or a folder. This files can not restore. To delete a file, you execute:
rm /home/marin/useless-file.txt
You can use rm with many different options. Some important options:
• -f: forced to delete files with reminder notifications
• -i: remind before deleting
• -r: delete recursive folders
• -d: delete empty folders
• -v: explain what the mission is doing
9. Find the file
There are two commands that can help users find files on Linux, but they work very differently. A command finds the file system, while the other command considers the database built earlier.
Command | Function | Example |
find | Locate files based on the criteria provided (name, type, owner, right, file size, etc.). Unless provided a location to start to search, otherwise this command will only search in the current directory. |
find .
-name myfile find /tmp -type d |
locate | Locate files using the content of /var/lib/mlocate/mlocate.db updated with the updateb command running through cron. No start location required |
locate
somefile locate “*.html” -n 20 |
Note: Details about the find command in the part below.
10. List the folder contents
To list the list of folders, you can use the ls command:
The “ls” command can take different parameters to help you change the output of the statement. For example, the “-a” parameter will display all files and folders, including hidden folders containing “-a”.
If you want the output of the statement to display a list of detailed information for each file and folder, you can use the “-l” (L):
ls -l You can try different
Besides, you can combine parameters to display the detailed information of files:
ls –al
11. Search for previous Linux command
Pressing the Up key will display the final Linux statement you successfully used. No error statement is displayed here.
Moreover, you can use the history command to see all the Linux commands you have used on Terminal.
12. Invisible password
When asked to enter a password, suppose in the case of using sudo, when you type the password on the screen, nothing will be displayed, no star or dot … After you enter the password, you press Enter to finish.
13. Copy and paste Linux command
To
copy or paste the Terminal command, you can not press the familiar key
combination Ctrl + C and Ctrl + V.
Instead, you can use Ctrl + Shift + C and Ctrl + Shift + V or click the right mouse then choose Copy or Paste from context menu.
14. Display process in Linux system
One of the necessary tasks to administer Linux system is to control the currently running processes. Once you know which processes are running, you can turn off processes that slow down the system. In addition, information about system processes tells us to turn off but the process makes the system operate unsteadily. Therefore, it is important to know which processes are running on the system. Linux supports a variety of process testing methods, one of which is using the ps command. When using this command all information about running processes will be displayed. You just need to enter the following command syntax into the terminal window:
# ps aux | less
Figure 1: Information about
process running in the system
In addition, this command can combine with the other parameters such as:
# ps –A: Check every process on the system.
# ps -U root -u root –N: Check every process except system processes.
# ps -u username: Check processes that a certain user executes.
Or you can use the # top command to see the processes running on the system in real time.
15. Check Socket information and TCP/UDP network information
After configuring network services of the Linux system, you need to keep the tab of the ports that are actually receiving the signal on the network interface of the system. This is very important because the system can be broken into through open ports. There are some Linux management tools that tell you information about the open ports and access the open ports on the network. One of the simplest and most reliable methods is to use the ss command to check the Socket information, in addition this command can display more TCP information and status information than other tools. This ss command provides information about:
• All Socket TCP.
• All Socket UDP.
• All ssh/ftp/http/https connections.
• All local processes are connected
to X server.
• All Socket TCP in FIN-WAIT-1 state.
Here are some ss commands:
• # ss –s: Display total Socket.
Figure 2: The output information when running the # ss –s command.
• # ss -1: Display every open port.
Figure 3: The output information when
running the # ss -1 command
• # ss –pl:
Check the open Socket process name using the following command:
• # ss -lp | grep:
Check user working with the open Socket.
• # ss -t –a:
Display all Socket TCP.
• # ss -u –a:
Display all Socket UDP.
16. Track Average CPU Load and Disk Activity
If you are a Linux system administrator, you need to know how to maintain a reasonable balance in the process of loading input and output between physical disk drives. You can change the system configuration to perform this task. However, a much simpler method is to use the isostat command to manage the input and output loading system in Linux by monitoring the operation time and average transmission speed of devices. This command will announce information of CPU (Central Processing Unit), input and output information for devices, partitions and network file system (NFS).
When running the isostat command, the output information is in the form:
Figure
4: Information displayed when running the isostat command.
To get the NFS folder information, you use the following command:
# iostat –n
17. Check Memory Map of processes in Linux
When you work in Linux system, you may need to check the memory capacity used in the system. Linux integrates many commands that allow you to check the memory capacity occupied. In there, a simple command which helps display information about the total capacity occupied and unused space of physical memory and the total memory capacity is the free command.
After running this command you will see the total capacity occupied and unused space of physical memory and the total memory capacity in the system. In addition, it also displays information about cache memory that individuals use.
Figure 5: Information displayed after
running the free command.
18. Check the operating time of the system
Do you want to know how long the server has been running? If you want, you just need to use the uptime command to check the time that the system has been running. This simple command not only tells you how long the system has been running but also indicates how many users have logged into the system in a previous period of time.
Figure 6: The result of the uptime command.
19. Check user login
In addition to Linux management tools, you can use a command to check which users have logged into the system and what they have done. This command will show the current time, the time the system has run, the number of users logged in.
This command also displays the average load in every 1.5 and 15 minutes. This command is very useful for system administrators who want to use average load information to plan capacity.
To check which users have logged into the system and what they have done,you just use the following command:
# w username
Figure 7: Information displayed after
running the # username command.
20. Control system behavior, hardware and system information in Linux
For many Linux users, controlling the system is a complex task. Most Linux distributions integrate a lot of control tools. These control tools provide methods that can be applied to check system behavior information. Controlling the system allows users to monitor the cause of the system’s performance being prevented. One of the essential tasks of the system control process is to look up information about system behavior, hardware and memory information. A simple command which displays information about the progress, memory, record page, IO group, error and CPU behavior is the vmstat command.
You just need to enter the following command into the terminal window:
# vmstat 3
Figure 8: The output information of the #
vmstat 3 command.
Moreover you can use the # vmstat –m command to check the memory information, and the # vmstat –a command to display the memmory page that is active and inactive.
Figure 9: Information displayed after
running the # vmstat –a command.
21. Check hardware information of the Linux system
For some Linux users, checking hardware information is not easy. Linux is a complex system but it integrates some tools to take detailed information of hardware, for example we can use a quite simple command that is the hdparm command to check the hard disk information on this system. This command provides a command-line interface to perform the management of many types of hard disks supported by the ATA/IDE device control subsystem of Linux. It provides a command to display verification information such as capacity, detailed information, … directly from the drive. This information is saved in a new extension format. You only need to log in as root user and use the following command:
# hdparm -I /dev/sda
Or use the command:
$ sudo hdparm -I /dev/sda
Then the system hard disk information will immediately display.
Figure 10: Detailed
information of hard disk.
22. Commands process file on Linux
The command to move around the Linux file system is ls, but there are many variations.
Linux command | Task |
ls | List current folder content |
ls -al | List folder content including hidden files |
cd dir | Move from the current folder to the dir folder |
cd | Move from the current folder to the private folder (usually [User Name] at Home) |
cd .. | Move up (to /) a folder from the current location. |
cd <location> |
Take the user to the specified location. If
the position starts with /, it is considered relatively to the root folder
and the current location. ~ Character represents the Home folder. For
example: cd /tmp cd Documents cd ~/Documents |
pwd | Show current folder |
mkdir quantrimang | Create a new folder named quantrimang |
rm filemuonxoa | Delete file named filemuonxoa |
rm -r thumuccanxoa | Delete folder named quantrimang |
rm -f filecanxoa | Force to delete file named filecanxoa |
rm -rf thumuccanxoa | Force to delete folder named thumuccanxoa |
cp file1 file2 | Copy file1 to file2 |
cp -r dir1 dir2 | Copy folder dir1 to dir2 and create dir2 if not yet dir2 |
mv file1 file2 | Move file1 to file2 or rename file1 to file2. If file2 is available, move file1 to file2 |
ln -s tenfile link | Create symbol link named link to file named tenfile |
touch filecantao | Create or update the filecantao file |
cat > tenfile | Enter from keyboard (standard input) into new tenfile file |
more tenfile | Show contents of file named tenfile |
head tenfile | Show the first 10 lines of the tenfile file |
tail tenfile | Show the last 10 lines of the tenfile file |
tail -f tenfile | Show content of the tenfile file and update continuously in real time |
tail -f -n N tenfile | Show content of the tenfile file and update continuously, limit N lines |
23. Network commands on Linux
Quantrimang.com had a detailed instruction article about commands work with network on Linux, you are interested to consult.
24. Start, stop and list services
These commands allow users to display services as well as start and stop them.
Command | Function | Example |
systemctl | The systemctl command can start, stop, restart and reload services, the admin right is required. |
sudo
systemctl stop apache2.service sudo systemctl restart apache2.service sudo systemctl reload apache2.service |
service | List services and indicate whether they are running. | service –status-all |
25. Determine the operating system version
The table below lists the commands that will show details about the Linux operating system running on the system.
Command | Function | Example |
uname | Show information about the operating system version in a text line. |
uname -a uname -r |
lsb_release | On Debian-based systems, this command displays information about the operating system including codename and distributor ID. | lsb_release -a |
hostnamectl | Display information on the system including server name, chassis type, operating system, kernel and structure. | hostnamectl |
26. Measure system performance
Here are some useful tools to check the system performance.
Command | Function | Example |
top | Display running processes with resource usage and system performance data. Processes can be displayed for a selected user or all users. Processes can be organized according to different criteria (by default, CPU usage level). |
top top jdoe |
atop | Similar to the above command, but direct more to system performance than individual processes.. | atop |
free | Display memory and exchange total memory, used and free space. | free |
df | Display the use of the system disk space of the file. |
df df -h |
27. Manage user and group
Commands to create and delete the user and group account is quite simple.
Command | Function | Example |
useradd | Add new user account to the system. Username is compulsory. Other cases (user description, shell, initial password, etc.) may be specified. The main folder will default to /home /username |
useradd -c
“John Doe” jdoe useradd -c “Jane Doe” -g admin -s /bin/bash jbdoe |
userdel | Delete user account from the system. The -f option is stronger, delete main files and other user files even if the user still log in. |
userdel
jbdoe userdel -f jbdoe |
groupadd | Add a new group of users to the system, update /etc /group. | groupadd developers |
groupdel | Delete the group of user from the system. | groupdel developers |
28. Set up and run scheduled processes
Tasks can be scheduled to run periodically using the commands listed below.
Command | Function | Example |
crontab | Set up and manage scheduled processes. With –l option, periodic jobs are listed. With –e option, periodic jobs can be set to run in selected periods. |
crontab -l crontab -l -u username crontab -e |
anacron | Allow users to only run scheduled daily tasks. If the system is turned off when a job is scheduled to run, it will run when the system starts. | sudo vi /etc/anacrontab |
29. Update, install and list applications
Commands to install and update applications depend on the version of Linux in use, specifically based on Debian or RPM.
Command | Function | Example |
apt update | On Debian-based systems, this command updates the list of available packages and their versions, but does not install or upgrade any packages. | sudo apt update |
apt upgrade | On Debian-based systems, this command installs newer versions of existing packages. | sudo apt upgrade |
apt list | List all packages installed on Debian-based systems. With the –upgradable option, it only displays packages with an upgrade. |
apt list apt list –installed apt list –upgradable |
apt install | On Debian-based systems, this command installs the required package. | sudo apt install apache2 |
yum update | On RPM-based systems, this command updates all or specified packages. |
sudo yum
update yum update mysql |
yum list | On RPM-based systems, this command lists the packages. | sudo yum update mysql |
yum install | On RPM-based systems, this command installs the required package. | sudo yum -y install firefox |
yum list | On RPM-based systems, this command lists known and installed packages. |
sudo yum
list sudo yum list –installed |
30. Turn off and restart
The commands to shut down and restart the Linux system require admin right. Options like +15 are the number of minutes that the command will wait before the shutdown request is made.
Command | Function | Example |
shutdown | Turn off the system at the time of the request. -H option pauses the system, and the -P option will turn off the power. |
sudo
shutdown -H now shutdown -H +15 shutdown -P +5 |
halt | Turn off the system at the time of the request. |
sudo halt sudo halt -p sudo halt –reboot |
poweroff | Disconnect the power from the system at the time of the request. |
sudo
shutdown -H now sudo shutdown -H +15 sudo shutdown -P +5 |
31. Linux
security command
I have synthesized 20+ essential Linux security commands, it is essential for security issues on Linux, you need, let’s consult.