Basic Linux commands list
Now we will discuss about some basic linux commands with examples, you are almost always going to need those commands, so better to remember them. However from my experience, it’s much easier to remember if you write them with pen on paper, rather than just typing on terminal.
This command prints the location of your current working directory. It’s important to know actually where you’re before going to a parent or sub directories.
ls is one of the most used basic linux commands, used to print contents of a directory, by default it lists contents of current working directory(pwd).
Example, use ls /var to list contents of the /var folder.
After knowing your pwd and getting an overview with the ls, it’s time to move around with cd command. Clarification, assume you’re on your Home directory, you need to go to the lib/apt/lists/ directory, use cd lib/apt/lists/
There’s three shortcut, if you need to move one directory up, use cd .. and go straight to your Home folder with cd , and use cd – to go back to your last working directory.
It’s used to print the contents of a file to the screen(stdout more precisely), really useful when you want to have a quick look on contents of a file. As example, use cat a_text_file to get the inside contents of that file in your screen.
cp , You can copy files and directories with this command. Typical usage is like cp file_a file_1_copy or cp directory_a dir_a_copy Also don’t forget to use proper path when you’re coping something to different location.
The mv command is used to move or rename directories and files. To rename a file use mv old_name new_name.
7. rm command
The rm command is used to remove directory or files. Like use rm -r /tmp/backup to remove everything that folder. Of course you have to be careful before removing anything.
mkdir, it’s used to make a new directory in linux. Example, use mkdir my_new_dir to make a new directory named my_new_directory. The -p argument is useful, when you don’t want to make parent directories manually.
rmdir, if you need to remove a directory, use this command. As example, use rmdir my_dir to remove that specific directory. More details about the rmdir command here.
touch, It’s the equivalent command of mkdir for files. You can create a blank file with touch command. As example, use touch ~/home/ubuntu/index.html to create a blank index.html file under the /home/ubuntu directory.
This command is used to make link between files and directories. As example, you need to make a symbolic link of the /var/www directory to the /tmp directory.
ln -s /var/www/ /tmp/
To un-link that symlink, use
You’ve to be extra careful with complete path and trailing slashes while linking and un-linking.
sudo , that’s an essential yet potentially dangerous command. Whenever you’re getting a Permission denied, Authorization failed or something like that use sudo.
As example, the /var/www directory is not writable by the normal user. So to create a blank index.html file under the /var/www directory use sudo touch /var/www/index.html
If you need to print first few lines of a file then you can use head command. A nice practical example would be
head -20 /var/log/syslog
This will print the first 20 lines of the syslog log to the stdout. By default head command prints first 10 lines.
It’s similar to the head command, but the function is opposite, prints last 10 lines of any file by default. Here’s an example, how to print last 30 lines of the kernel log.
tail -30 /var/log/kern.log
It’s also a very important command, used to change file and directory permission. As the chmod command is a very long topic.
Explanation in Numerical:
Representations used for Ownerships
u – User
g – Group
o – Others
Representations used for File permission
r &ndash Read (value is 4)
w &ndash Write (value is 2)
x &ndash Execute (value is 1)
7 = 4+2+1 (read+write+execute)
6 = 4+2 (read+write)
5 = 4+1 (read+execute)
4 = 4 (read)
3 = 2+1 (write+execute)
2 = 2 (write)
1 = 1 (execute)
Basically there’s three type of permission, read, write and execute. Each of them denoted by a number.
4 for read permission
2 for write permission
1 for execute permission
So if you need to set universal read/write permission to a file, you can use
chmod 666 my_file_name
Assume you need to make a script executable, you can use
chmod +x my_script_name
There’ll be a full chmod tutorial very soon, to explain you in detail.
Use the chown command to change file owner and group information. Use the chmod command to change file access permissions such as read, write, and access.
chown command changes the user and/or group ownership of for given file. The syntax is:
chown owner-user file
chown owner-user:owner-group file
chown owner-user:owner-group directory
chown options owner-user:owner-group file
The basic command to find files and directories in Linux. As it’s a database driven command, so for the first time you need to build the database, run sudo update-db and wait for few minutes.
A typical example to locate something could be like below.
locate -i *chromium*
It supports wildcards, and use the -i option to ignore upper/lower case.
This command is used to check disk space usage on a linux system. The most common usage is like below, used along with the -h flag.
If you need to quickly check disk space usage of a file or directory, the du command is here.
For a single file, a nice example could be like below,
ubuntu@DESKTOP-SCLCDHJ:~$ du -sh /home/ubuntu/login
Or could be like below for a entire directory and it’s contents.
ubuntu@DESKTOP-SCLCDHJ:~$ du -sh /home/ubuntu/node/
The -s flag is used to suppress unnecessary clutter and -h flag is to make the output more human readable.
The free command is used to display amount of free and used RAM in the system, also prints the swap space stats.
Again, the -h flag is used to make the output easier to read by humans.You can read more here, check
No doubt you often need to create and extract zip archives, here’s the zip and unzip commands for that.
Most probably these commands are not pre-installed, install them with apt in Ubuntu.
sudo apt-get install zip unzip
The syntax to create a zip archive,
zip -9r my_archive.zip file_1 file_2 folder_1 folder_2 folder_3
When the -9 option is used, zip attempts maximum compression on all files and -r option is for recursive archiving.
The unzip command extracts archives to the current working directory(pwd) by default. So if you need to extract the contents to a specific folder, then use
unzip my_archive.zip -d /path/to/my_directory
You might want to read about another archiving tool here, 7zip linux command examples.
ifconfig stands for interface configuration, and it can do many networking related things, literally.
Some basic use for beginners could be like checking which network interfaces are connected and their respective IP address.
Or you can find out how much data passed through a specific interface, all could be done just by running the ifconfig command.
This command prints some basic information about the system, like OS name, kernel version, host name, system time, OS architecture and so on.
As the name suggests, history command prints a list of previously typed commands. Very useful when you’re trying to find what you’ve done wrong before.
You can also quickly find previously typed commands by pressing the Ctrl + R key combo.
To Clear history use this command: history -c
The command to find details about other commands.
Almost every command has their respective man pages, useful to get a quick overview of an unknown command, use it like man any_command .
Extra – Shutdown Linux machines with command line
We’re not done yet! How you can shut down and reboot your system through command line?
shutdown -h now to power off immediately.
shutdown -h +10 to shutdown after 10 minutes.
reboot to reboot the machine immediately.You may need to use sudo with the command above, depending on your current user id. Another thing important note is the modern shutdown command is a symbolic link to the systemd init daemon.