Basic Linux Commands That Every User Should Know
Are you just getting started with Linux and want to learn some important Linux commands. Then today you are going to learn some basic Linux commands that will make your life a lot easier.
Linux is one of the most popular operating systems. Many Linux distros are available over the internet like Linux Mint, Ubuntu, Kali Linux and more. There are different distros for different user needs.
For newbies, some Linux distros are recommended over others for a smooth transition from Windows to Linux environment.
But one thing new Linux user get little intimidated with is Linux Terminal or Linux command line.
Linux terminal is a simple yet potent tool. It is just like Windows command prompt but is more powerful. You can do whatever you want with your system using the Linux terminal.
There are some dangerous commands too, and that requires separate discussion.
But for today, we will be looking at some essential Linux command that every user should know.
So, let's dive right in.
How to Open Terminal in Linux?
To open a Linux terminal, press Ctrl + Alt + T
or press Alt + F2
, and search by typing gnome-terminal or terminal and press enter. You can also use GUI to open Linux terminal.
Basic Linux Commands
Now you have your Linux terminal open, and let's get started by learning some basic commands.I would recommend you to read about the command and then try to run that particular Linux commands in the terminal.
This will help you better understand and learn Linux terminal commands quickly.
1. pwd
pwd
pwd command prints the current working directory path. It prints the absolute path to the current directory starting from the root directory.
The root directory is the base of the Linux system. What absolute path does is that it displays the complete path to locate a file or folder/directory.
To see pwd in action, open your terminal and type pwd command and press enter.
It will display the directory you are currently inside. The slashes represent the different level of directories.
2. ls
Syntax: ls [options]
ls commands displays all the files and folders inside the directory. The ls commands do takes different arguments.
- Without argument, ls lists files and directories of the current directory
ls
- If you provide directory as an argument, then it lists the content of that directory.
ls [directory-name]
- If you want to display detailed information about the directory, then -l as an argument. The entries with "-" represents files, whereas "d" represents directories.
ls -l
You will see the following results after using the command. Let me dissect it for you.
- ls with -a command displays hidden files in the directory.
ls -a
3. cd
cd path/directory/..
cd is known as change directory command. You can change the current working directory using the cd command.
In simple words, it helps in navigating the file system using the terminal.
E.g., you want to change your directory then entre the commands followed by name or path of the directory.
cd with different arguments:
cd directory
Eg: cd Documents. Then this will change your current working directory to Documents.
cd path
Eg: cd /usr/local this will change your current working directory to local. Note: path must start with /
If you use only cd and press enter, then it will take you to the home directory.
cd ..
This command will take you one directory up. For, E.g., now our current working directory is /Documents/Courses.
Now by using cd .. command, our current working directory will be /Documents.
Also, if you want to create a directory name having space, for, e.g., Web Development, then merely typing cd Web Development will not work.
It would help if you used to make it work — e.g., cd Web Development.
Quick Tip: While entering the name of the directory just press tab to autocomplete it. Moreover, one more thing, please note that there is a space between cd and two dots.
4. mkdir
Syntax: mkdir directory-name
mkdir command let you create an empty directory/folder. You need to enter mkdir followed by directory name that you want to create.
For example, let's create a directory named Web. Enter mkdir Commands into the terminal. Now use ls to view the created directory.
Again, if you want to create a directory with space in the name, then, yes you guessed it right, you need a symbol, for example for creating directory command named Web Development. You need to use the command in the following way:
mkdir Web Development
5. rmdir
rmdir directory-name
You have learned how to create a directory, now its time to learn to delete a directory. Enter rmdir followed by the folder you want to delete. Please note that you can't delete files using this command. For deleting files head over to next command.
Note that you can only delete empty folder/directory using this command.
For, e.g., for removing directory name Android, we will use the following command:
rmdir Android
If it still not deleted then, the directory might be empty. And you wll get the following error:
For deleting a files and directory containing files, head over to next command, i.e., rm with -r option.
6. rm
rm directory-name
Simple rm command without any option work same as rmdir but without any confirmation. That is it also lets you delete empty directory but without confirmation.
To remove an empty Android directory, use rm Android will delete the Android directory if it is empty.
However, rm with -r can delete the directory containing files.
rm -r
For, e.g., to remove non-empty Web Development directory, run rm -r Web Development. This command will remove all the files, subdirectories inside that directory.
7. mv
mv [filename/path] [filename/path\]
You can move files in the file system using the Linux command line. It takes two arguments. You can also rename the file using this command.
mv command performs two different functions depending on the type of the arguments.
If the argument is a path, then it performs move function, and if the arguments are filenames, then, it does rename function.
For using mv command for renaming the file, the first argument must be the file that you want to rename, and the second argument should be the new file name.
For, e.g., to rename index.js to home.js, we will use the following command.
mv index.js home.js
Also, for using it for moving, use rm filename/path, followed by the path where you want to move the file.
E.g., for moving home.js to ~/Document/Commands, we will use the following command. (~
represents home directory)
mv home.js ~/Documents/Commands
8. cat
cat filename
The cat command is used for copying, combining, viewing and creating files. Use ctrl + d
to save and close the file.
For creating a new file, you can use the following command
cat > filename
Eg:
cat > hello.txt
For viewing a file, type the cat command followed by the file name.
cat filename
Eg:
cat hello.txt
Now, to combine or concatenate two files use the command as follow:
cat file1 file2 > new-file
Eg:
cat first.txt second.txt > combine.txt
Now view the file using cat combine.txt
9. touch
touch filename
touch command lets you create a file. You can create any file, and it can be a simple text file or a zip file.
For example, if you want to create index.js, then use:
touch index.js
This will create index.js in the current working directory.
10. cp
cp source destination
cp command is used to copy files or directories. cp uses two arguments, the first is the location of the file to be copied, and the second is where to copy.
E.g., to copy hello.text to ~/Documents/Commands, the command will be:
cp hello.text ~/Documents/Commands
11. man & --help
man command --help
man is for manual. If you want to know more about any command and how to use it, you can use man followed by the command.
E.g., to know more about ls command, use
man ls
Result:
Moreover, the --help
command also does the same thing what man does.
E.g., to know about cd commands, you would type
ls --help
12. clear
Syntax: clear
clear as the name suggests clear the terminal window.
13. locate
locate [options] filename
This command helps in locating the file in the Linux system using the terminal. To search any file type locate followed by filename you want to location.
If successful, the result will be the file path. locate with -i option often comes handy because it helps to ignore the case (uppercase or lowercase). e.g.,
locate -i hello
It will ignore the case and locate the file with the upper case as well "HELLO."
14. history
history
Using history command in the terminal, you can view the previously executed commands.
So, If you ever want to see what commands you used earlier, then run history command.
15. ping
ping address
ping command helps in checking network connectivity between the host and host/server.
Whenever we ping any server then if we received some packets in response ping is successful, and it means that your internet is working.
For example, we try to ping google.com using below command:
ping google.com
Press ctrl + c
to stop the ping process. If ping to google.com is successful, then this confirms that your internet is active.
Read: 12 Best Free VPN For PC
Some Intermediate But Important Linux Commands
1. sudo
sudo command_to_execute
sudo is one of the most used Linux commands. sudo stands for superUser do. If you want to execute something with administrative or root privileges, then you can use the sudo command.
It is used as a prefix for some commands that only superuser can execute.
However, when you execute something with sudo command, then it will ask for a password. Though you don't need to enter the root password, you can enter your password.
You need to enter root password only if the command involves files or directories not owned by the user.
By default, sudo password is retained for 15 minutes per terminal. After that, you need to provide the password again.
For, e.g.,
sudo shutdown -r now
The above command restarts your system and as you can see requires sudo for execution.
2. echo
echo string/text
The echo command is used to display text or string that are passed as an argument. e.g.
echo "Hello World."
It is a handy command because it can show environment variable values.
For example, $USER
, $HOME
, and $PATH
environment variables. To get the value use the environment variable after echo command. e.g., The below command display the name of the user
echo \$USER
3. apt-get
sudo apt-get [options] [command]
apt-get is a Linux command that is used to handle packages in Linux. It is one of the most important Linux commands that everyone should know.
No matter if you are starting with Linux or have been using Linux for years, this command is a must. This command is used to install software packages.
Also, with apt-get, you can update your system and all the installed packages. apt-get does require admin privileges, so you need to use sudo with it.
It is always recommended to update your repository before installing any new package using:
sudo apt-get update
After that you can install any package for, e.g., vlc media player using:
sudo apt-get install vlc
4. chmod
Syntax: chmod [options] permission filename
With chmod, you can change the permission associated with any file. chmod stands for "change mode."
For example, to change the permission of the file named hello.java to 777, you can do this using
chmod 777 hello.java
There are lots of flags associated with the chmod command. You can find more information about chmod here.
5. df
df
df command is used to display information related to the Linux file system like total space and available space.
You can also see the mounted partitions.
By default, it displays information in KBs, but you can use df command with -m flag to get information in MBs.
df -m
6. du
du filename
The du command is used to determine the disk usage of a file. Use du followed by the filename whose disk usage you want to know.
For example, if you want to know how much space image.jpg is taking, then you will use the following command:
du image.jpg
7. hostname
hostname -[options]
You can obtain the DNS name and set your hostname using this command. However, what is a hostname?
Let me explain...
A Hostname is a name that is given to a network-attached computer to identify it over a network uniquely.
If you execute hostname command without any option, then it will display the current hostname of your Linux system.
hostname
To get your IP address, use the hostname Linux command with -i
flag:
hostname -i
For setting a new hostname use the following command sudo hostname new-hostname. For, eg.
sudo hostname TechBrackets
8. zip & unzip
Zipping
zip [options] zipfile.zip filename
Unzipping
unzip zipfile.zip
You can use zip and unzip command to compress files and uncompress file respectively. For compressing, use zip command followed by zip file name followed by file you want to compress.
E.g., for compressing image.jpg file into a zip file, you can use the following command:
zip file.zip image.jpg
Moreover, for decompressing the newly created file.zip, you need to use:
unzip file.zip
9. uname
Syntax: uname -[options]
The uname command displays information about the system. It shows which Linux distro you are using.
You can use uname with -a flag to get detailed system information like the kernel release date, version, processor type, etc.
uname -a
10. passwd
Syntax: passwd
The passwd command lets you change your password. It requires administrative privileges, so you need to execute it with sudo. eg:
sudo passwd
11. shutdown/ halt/ reboot
Syntax: sudo shutdown/halt/reboot
These three Linux commands do the same thing. Also, all require sudo prefix for execution.
halt instructs the hardware to stop all the CPU functions and reboot the system.
sudo halt
reboot as the name suggests, it reboots the system.
sudo reboot
shutdown command is used to shut down the system in a safer way. You can also provide the different arguments to shutdown command for, eg:
sudo shutdown
shutdown commands take various arguments:
sudo shutdown now
shuts down the system immediately.
sudo shutdown now
sudo shutdown minutes
automatically shuts down the system after specified minutes. For eg, the below command will shutdown system after 15 minutes.
sudo shutdown +15
sudo shutdown time
automatically shuts down the system at the specified time. For eg, the below command will shutdown system at 4:00 AM.
sudo shutdown 04:00
To cancel shutdown, use the following command:
shutdown -c
Read More: How To Install Android Studio on Ubuntu Step by Step
Final Words: Master Linux Command Line
That's it. No doubt, there are many more Linux commands. But you will use these basic Linux commands frequently and more importantly, you will gain hands-on experience with these commands.
Moreover, this will help you better understand the terminal and Linux as a whole. Don't forget to download free cheat sheet for quick reference.
Quick Tip: To see the previously executed command, use up and down arrow keys. And use tab for autocompletion.
I hope you will find this guide helpful, if in case you need any help, please do let me know in the comment section.
Do share this guide with your friends.