In this tutorial, we will learn 10+ Important Linux commands with examples. Linux is used almost everywhere. All the cloud providers also supports Linux instances. It works on older hardware as well. We can easily customize it as it is open source. Linux community support is quite strong. It has plenty of pros which bounds us to use Linux. We have discussed some of the pros of using Linux later in this tutorial.
What is Linux
Linux is one of the strongest and most usable operating system. An operating system is a computer software that manages system’s hardware and software resources. In simple word, communication between the system’s hardware and software is done by operating system. You can not run any software in your computer if there is no operating system. Linux is an open source operating system which means it is free to use kernel.
You can download and use the Linux without any license. Hence, many of the organizations have moved their software to Linux in order to save the operational cost. There are many distributions of Linux operating system available in the market. Linux is suitable for any type of server applications. You can download and install Linux in your Windows machine. There are other operating systems as well like Windows, macOS.
Why Use Linux
There are plenty of reasons to use Linux and justify why Linux is better that any other operating systems. Some of the pros of using Linux are listed below.
Open source
Unlike proprietary software, Linux is free and open source which means developer’s community can easily access the source code and do any form of operation to it. They can analyze, modify, enhance or even delete.
Multiple distros
There are more that 100 distributions of Linux available in the market. Based on the requirement, we can use any distro like Fedora, CentOS, Ubuntu, Slackware, Kali etc.
GUI support
As we know, Linux is CLI (Command Line Interface) based operating system, most of it’s distribution now comes with GUI (graphical User Interface ) installation which makes it easy for the user to work with the OS, just like Windows OS.
Compatibility
Linux supports almost all file formats. Hence, we can work with any file format in Linux.
Security
When it comes to security, Linux is very less prone to vulnerability. Security is one of reason which has made Linux as one of the most favorite OS of developers. To run any application in Linux, admin authorization is needed. Each user only gets the access to their owned files and folders.
10+ Important Linux Commands with Examples
Also read: Python logging module
1. ls command
ls command in Linux is used to list down the files and directories in the system. It also lists the hidden (.) files using flags. If ls command is executed without any flags, it will list down all the files and directories present in current working directory. Let’s explore few helpful and daily use ls commands.
Syntax
ls [Option]…[File]…
To list all hidden files and directories, use flag -a with ls as shown below.
[root@linuxnasa~]# ls -la dr-xr-x---. 9 root root 4096 Mar 30 20:42 . dr-xr-xr-x. 20 root root 4096 Mar 13 12:08 .. -rw-r--r--. 1 root root 75 Mar 30 20:33 .bash_profile -rw-r--r--. 1 root root 75 Jul 28 2022 .bashrc drwx------. 2 root root 4096 Mar 30 20:41 demo drwx------. 2 root root 4096 Mar 13 10:40 .ssh -rw-------. 1 root root 0 Mar 30 20:41 file1.txt
To list all files and directories under /tmp path, use command as shown below.
[root@linuxnasa~]# ls -lrt /tmp drwx------. 3 root root 60 Mar 30 11:47 dir1 -rw-------. 1 root root 0 Mar 30 20:41 test.sh
2. cat command
cat command is used to lists, combines and writes content of a file to the standard output. Concatenate or cat command is one of the most usable command in Linux. Some of the common cat command is shown below.
Syntax
cat [Options] [File]
To see the content of file file1.txt use below command.
[root@linuxnasa~]# cat file1.txt This is file1 Welcome to this tutorial
To see the content of all .txt files, use below command. Since there are only two .txt file in present working directory, it will cat those two files.
[root@linuxnasa~]# cat *.txt This is file1 Welcome to this tutorial This is file2 In this tutorial read about Linux commands
To write the content of multiple files in a new file, use below command.
[root@linuxnasa~]# cat file1.txt file2.txt> file3.txt
To create a new file and add content to it, use below command.
[root@linuxnasa~]# cat > newfile.txt <<EOF > This is a new file created from cat command > Thank you > EOF
3. cp command
copy or cp command in Linux is used to copy files and directories along with their content is desired location.
Syntax
cp [Options] source destination
To copy file file1.txt from current working directory to /tmp path, use below command.
[root@linuxnasa~]# cp file1.txt /tmp/
To recursively copy the directory demo from current working directory to /tmp path, use below command.
[root@linuxnasa~]# cp -rf demo/ /tmp/
To copy all .txt files from current working directory to /tmp path, use below command.
[root@linuxnasa~]# cp *.txt /tmp
4. mv command
move or mv command in Linux is used to move files and directories from one path to another.
Syntax
mv [Options] Source Destination
To move file file1.txt from current working directory to /tmp path and rename to tmp_file, use below command.
[root@linuxnasa~]# mv file1.txt /tmp/tmp_file
5. mkdir command
make directory or mkdir command in Linux is used to create new directories. We can also create directories recursively using flags.
Syntax
mkdir [Option]… [Directory]…
To create a new directory demo, use below command.
[root@linuxnasa~]# mkdir demo
To create directory recursively, use below command.
[root@linuxnasa~]# mkdir -p demo/sub-dir1/sub-dir2
6. touch command
touch command in Linux is used to create a new file. It is also used to change and modify the timestamp of a file.
Syntax
touch [Option]… [File]…
To create a new file newfile.txt, use below command.
[root@linuxnasa~]# touch newfile.txt
To change the modification time of file file2.txt, use below command.
[root@linuxnasa~]# touch -m file2.txt
7. rm command
remove or rm command in Linux is used to delete a files, directories, symbolic links and other objects.
Syntax
rm [Options] [File]…
To remove file file2.txt, use below command.
[root@linuxnasa~]# rm file2.txt
To delete directory demo recursively, use below command.
[root@linuxnasa~]# rm -rf demo/
8. rmdir command
remove directory or rmdir command in Linux is used to delete only empty directory.
Syntax
rmdir [Option]… [Directory]…
To delete empty directory sub-dir2 and it’s parents sub-dir1 and demo, use below command.
[root@linuxnasa~]# rmdir -p demo/sub-dir1/sub-dir2/
9. locate command
locate command in Linux searches the database system to find a file.
Syntax
locate [Options] pattern
To locate first 2 files which ends with .txt, use below command.
[root@linuxnasa~]# locate "*.txt" -n 2 /home/linuxnasa/fdl-1.3.txt /home/linuxnasa/LICENSE.txt
10. which command
which command in Linux finds the binary of the given command along with it’s absolute path in PATH environment variable.
Syntax
which [Options][filename1] [filename2] …
To find the binary of shell, use below command.
[root@linuxnasa~]# which sh /bin/sh
To find all matching executables of a given command python, use below command.
[root@linuxnasa~]# which -a python /bin/python /usr/bin/python
11. wc command
word count or wc command in Linux is used to find out number of lines, words, byte and character count in the file.
Syntax
wc [Option]… [File]…
To find out number of lines, words and characters in files file2.txt and file3.txt, use below command.
[root@linuxnasa~]# wc file2.txt file3.txt 2 10 58 file2.txt 5 22 135 file3.txt 7 32 193 total
To find out only the number of words in file file2.txt, use below command.
[root@linuxnasa~]# wc -w file2.txt 10 file2.txt
Conclusion
In this tutorial, we read about 10 basic and commonly used Linux commands with examples. There are plenty of other commands in Linux which comes handy when working with Linux. Explore command like grep, cut, chmod, chown etc. to get more comfortable in Linux.