Linux is an open-source operating system that powers a majority of web servers and thousands of personal computers. Built around the Linux Kernel, the operating system comes in different distributions for both desktops and servers, including Ubuntu, CentOS, CoreOs, Arch Linux, Fedora Debian among others.
Although the distributions may differ in some ways, there are common Linux commands that work in all of them. In this guide, we will focus on how you can utilize the power of these basic Linux commands to manage your system better.
- A VPS plan running any Linux distribution>
- Shell access to your server
# 1: Linux cd command
In Linux cd is an acronym for ‘change directory’. This command is useful if you want to change to a different working directory. For instance, to move to the ‘var’ directory, type the command below:
$ cd /var
The command is also useful if you want to navigate one directory up. To do this, just type the cd command followed by two dots:
$ cd ..
# 2: mkdir
Linux ‘mkdir’ stands for ‘make directory’. The command allows users to make new directories using the syntax below:
$ mkdir <directory name>
$ mkdir sample
# 3: pwd
Another great command is pwd which means ‘print working directory’. This command shows the current directory that you are working from. To use the command just type the command below on your terminal window:
# 4: cp
cp command is used to copy a file using the syntax below:
$ cp <source> <destination>
For instance, to copy the file ‘sample.txt’ to ‘sample2.txt’ on the same directory, we can use the command below:
$ cp sample.txt sample2.txt
Please note you must have the right permissions to copy files to a directory.
# 5: mv
When you are moving files in Linux, you can use the mv command by typing the command below:
$ mv <source> <destination>
For instance, to move the file ‘sample.txt’ from the working directory to the downloads directory, we can use the command below
$ sudo mv sample.txt /downloads/sample.txt
# 6: ls
The ls command is used in Linux to list the contents of a directory. For instance, to list the contents of our ‘/var’ directory, we can type the command below:
$ ls /var
backups crash local log opt snap tmp cache lib lock mail run spool www
# 7: cp -a
You can use the cp -a to copy a directory. For instance, to create a backup of our mail directory, we can use the syntax below where ‘mail.bk’ is then the name of our new directory:
$ sudo cp -a /var/mail /var/mail.bk
# 8: chmod
chmod is used to change the permissions of a directory. For instance, we can alter the permissions of ‘var/www’ directory by typing the command below:
$ chmod 755 /var/www
In the above example, full access is maintained for the current user while other people are kept from modifying the files in that directory.
A permission of 755 means that the owner of the file will have read, write and execute permissions while the group and other users on the system will only have read and execute permissions only
# 9: chown
You can change the ownership of a file or directory in Linux by using the ‘chown’ command.
$ chown [options] <new_user> <file_or_directory>
For example, to change the ownership of ‘/var/www/’ folder to a user named ‘francis’, we can use:
$ chown -R francis /var/www/
Please note we have used -R command line option in order to change ownership of all files and directories inside our target directory recursively.
You can also use chown command to change both the owner and group of the file using the below syntax:
$ chown new_user:new_group <file_or_directory>
$ chown james:www-data /var/www/
# 10: free
You can check the total used and available memory on Linux by using the free command:
total used free shared buff/cache available Mem: 595512 394600 71196 17144 129716 90124 Swap: 0 0 0
# 11: df
Use the df command to get a report about the disk space on your Linux server.
If you want to get the information in a human readable format, use the -h option
$ df -h
Filesystem Size Used Avail Use% Mounted on … /dev/sda1 9.6G 1.8G 7.8G 19% / ...
# 12: rm
Use rm command to completely delete a file from the system
$ rm <filename>
$ rm sample.txt
You can also delete a directory using the the rm command with a -r option
$ rm -r /tmp/sample
Ensure you have the right permission to delete the file or use sudo to do the task.
To remove a directory use the rm -f command as shown below:
$ rm -f <directory name>
$ rm -f mail.bk
# 13: date
To get the current date and time of your Linux server, use the date command as follows:
Thu Aug 9 12:52:59 UTC 2018
# 14: reboot and shutdown
To restart your Linux VPS server, type:
$ sudo reboot
To shut down the server, type the command below:
$ sudo shutdown -h
# 15: touch
In Linux, the touch command is used to create new files. For instance to create a new file on your current folder, run the command below
$ touch sample.txt
# 16: cat
You can display the content of files in Linux by using the cat command.
$ cat sample.txt
Or to combine the content of multiple files run the command below:
$ cat sample1.txt sample2.txt
You can also append the content of the concatenated files to a different file e.g. sample3.txt using the command below:
$ cat sample1.txt sample2.txt >> sample3.txt
# 17: more
Use the more command to read the content of a file one screen at a time:
$ more sample.txt
# 18: grep
If you want to search for a certain text in a file, you can use the linux grep command.
The basic syntax is:
$ grep '<keyword(s)>' <file_name>
For instance to look for the phrase chemist in a file named sample.txt, type the command below:
$ grep 'chemist' sample.txt
Our chemist was filled with water during the floods
Please note, if your keyword is found, it will be highlighted in red.
# 19: history
This command will display a list of previously used commands:
1 free 2 df -h 3 date ... 12 grep chemist sample.txt 14 history ...
To reuse a previously used command type the ! character followed by the numeric key associated with the command.
# 20: clear
Sometimes the screen may get filled with lots of commands and output. To clear these, run the clear command:
# 21: traceroute
You can track the route followed by packets from your computer to a given host.
$ traceroute 127.0.0.1
traceroute to 127.0.0.1 (127.0.0.1), 30 hops max, 60 byte packets 1 localhost (127.0.0.1) 0.052 ms 0.004 ms 0.003 ms
# 22: ifconfig
ifconfig stands for Interface Configuration use this command to query network interface parameters
ens4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1460 inet 10.142.0.2 netmask 255.255.255.255 broadcast 0.0.0.0 inet6 fe80::4001:aff:fe8e:2 prefixlen 64 scopeid 0x20<link> ether 42:01:0a:8e:00:02 txqueuelen 1000 (Ethernet) ...
# 23: ssh
Use Linux ssh command to connect to another remote server:
$ ssh <server's IP/Domain_name>
$ ssh 127.0.0.1
# 24: wget
You can download files in a Linux machine using the wget command using the below syntax
$ wget <URL>
$ wget http://www.example.com
I hope you have enjoyed reading and testing the basic Linux command that works on all distributions. Although this is not a conclusive list of all commands available on Linux, they can give you a better foundation of managing your Linux server as a beginner.