Hi everyone, in this blog post I will be talking about some of the most used Linux commands.
What is a Shell?
A shell is a command line interface that will take all my commands as input for example mkdir
for creating a folder or touch
for creating a file or git
which is also a command line tool (touch
and mkdir
are command line tools that we will look into shortly).
But how will my operating system know what command line tools we are using and how are they converting to the instructions? The shell helps us in doing that. For example, when you type git
in your terminal, how does your operating system know what git
is? Is it a file or some internal processing or maybe some magic? (It is a file)
So that is what a shell is. A command line interface that will interpret our commands and tell OS what to do.
Some examples: seashells, bash, zsh.
Ok so as I was saying that the git
, python3
are just some executable files, which are called when we use them in our terminal. But how does our machine know where they are located?
So go ahead and type git --exec-path
in your terminal you will see some path where the git
executable file is located and whenever you type git
in your terminal it will call that file and so work will be carried out. You can also type where git
and it will show you the same thing.
You can also use this path instead of typing git
and it will work. So now you might be thinking that how does our computer know where to look for git
or how does it know the path?
This is something that is known as an "environment variable". So as the name suggests they are just some name values that are used to change how every command and process are executed.
So if you go ahead and type echo $PATH
it will just show you the path environment variable and it shows a bunch of paths that are saperated by a :
.
Now what our computer will do is that whenever we type something like git
or python3
, it will look into each path and try to find the executable file that we are trying to run. If you type some command that doesn't exist then it will check all the paths and it will show an error that the command doesn't exist.
So as you now know what shell is let's jump right into some of the most basic commands that we use. Just a heads up I am using windows powershell and on that, I am using WSL(Windows Subsystem for Linux). Now you might ask what WSL is, but we won't go too deep into this because it's not the topic for today's blog.
But still here is a definition of WSL: The Windows Subsystem for Linux lets developers run a GNU/Linux environment -- including most command-line tools, utilities, and applications -- directly on Windows, unmodified, without the overhead of a traditional virtual machine or dualboot setup. You can read more about WSL here: What is WSL?
Just a heads-up, you don't need to know what WSL is to proceed with this blog. You can use different command line interfaces like bash, zsh, etc.
Most used commands
Before jumping into the commands, let's see what are these things written at the start of our terminal.
what you see on the right is the terminal and on the left is a folder. Here khush@DESKTOP-4BAP0DU
is just the user while /mnt/c/Users/khush/everythingOpenSource/DesktopTry
is just a visual representation of what you see on the right side. If you want to make some changes in your folder somewhere, you have to navigate to that path in your terminal. The $
sign just implies the starting of the command.
mkdir
mkdir
stands for "make directory". By using this command along with the name of the folder, you'll be able to create a folder at the path which is on the terminal.
You can also create a directory inside of an existing directory without going inside it.
You can also add a directory between two folders
The FriendsFolder
won't move. A copy of it will be created inside of middleFolder
So now as we have created a folder in our DesktopTry
folder let's go inside it. The command to do that is cd
.
cd
cd
stands for "change directory". That means it is used to navigate to different folders. Let's say we want go to inside someCoolNameFolder
and make some folders and files inside it.
And now as you can see in the terminal we are in the someCoolNameFolder
. You just type the name of the folder you want to go inside after cd
, if the folder exists then it will go inside that folder. Now this was if we want to go inside some folders, but what if you want to navigate backward?
For that, we use cd .
, cd ..
, cd ~
and cd /
cd .
means go to the current directory. If you are at some directory and if you do cd .
then it won't change anything since you are in the directory.
cd ..
will move you one directory backward. As you can see in the terminal we are now again in the DesktopTry
folder.
Now let's talk about cd ~
and cd /
. So, every computer has a starting point where it loads the terminal. cd ~
will take you to that default starting point. While cd /
will take you to the absolute first point. Let's see how it works.
So as you can see that I am currently in some folder that is out of our scope of discussion but do you see that ~
besides my user name? That means we are at the default starting point(Don't worry what ls -a
is we will cover it shortly). And now if you try to do cd ..
you will be able to go back since it is not the very first folder.
Now as you can see the /
, that means we are at the absolute first folder in our computer and can't go back anymore. So instead of typing cd ..
again and again, we just use cd ~
and cd /
to go to those points directly.
touch
Ok now let's make some files in someCoolNameFolder
. touch
is a Linux command used to make files.
ls
Now let's just say you want to see what files are in a particular folder. In that case, you use ls
(list) command.
You can also add a tag with ls
as well, something like ls -a
which will show all the hidden files in the directory along with all the normal files and folders.
the .
and . .
just denotes the current directory and previous directory respectively.
You can also type ls -l
(list with long details)for more in your terminal
You can also combine both with ls -al
for long detail of hidden files as well.
You can also use ls -R
and it will also show you all the files which are in the sub-directories.
cat
cat
stands for concatenate. With this command, you can see what is written inside a file.
You can also use cat > fileName
to create a file and write in it.
The >
just implies where the output should be. If the file doesn't exist then it will make one.
You can also add files to a new one
You can override what is written in files as well
pwd
pwd
stands for "print working directory". It just shows what directory we are currently in.
cp
cp
stands for copy file.
man
man
stands for user manual. It will show you details of all the command
mv
mv
is used for moving files.
You can also use mv
to rename a file as well. Instead of adding a name of a directory instead, give a name of a file.
In this example rollNumber.txt
is changed to newRollNumber.txt
.
rm
rm
stands for remove file.
CAUTION: If you remove a file this way it won't go to the bin. It will be deleted forever.
rm -R
to remove a directory with all its files. Sometimes it might be possible that the file is open and is not deleting while deleting a directory for that, you can use rm -rf
. f
denotes force flag.
sudo
sudo
stands for "Superuser do". It is used for administrative work. So whenever you will use sudo
it will ask you for a password.
head
head
will print the first 10 lines in a file by default but you can change that.
tail
tail
will print the last 10 lines in a file by default but can change that.
find
You can find files and directories with this command.
You can also find only directories and not any files with the help of find . -type d
.
You can also find only files and not any directories with the help of find . -type f
.
You can also find a particular file in a particular type or maybe different types as well.
*
means "all". Everything can come under it.
df
df
: This lets you check out the system's disk space usage.
diff
diff
: It compares the files line by line and it will output the files that will not match. A use case of this can be, if you want to check the difference between two files you can do so and find what lines are different.
chown
chown
stands for "change owner". You can provide root permissions and not all users can view this particular file.
grep
grep
: Global regular expression print. We have seen how to search for files, but what if you want to search for some particular text in a file, for that we use grep
. Also, it is case-sensitive.
If you want to search for a particular word you can use -w
tag.
You can also change the case sensitivity with the help of -i
tag and you can also combine it with -w
tag.
wget
You can download anything on the web.
wget <url>
wget -o <file name of your choice> <url> (it will create that file)
top
use top
command to find what processes are running what amount of CPU are they taking and so on. Just like task manager on Windows.
But since I am basically running a container it won't know what processes are running on my machine. These things we will cover when we are doing some advance topics like docker and all.
uname
this will show you the information on what system you are using.
hostname
You can also use hostname
to see your hostname. It also provides various tags you can use, one of which is hostname -i
which will show the IP address.
nslookup
If you want to check the IP address of some particular domain then you can do it with the help of nslookup
.
netstat
List down all the ports that are currently listening.
Closing
With this, we are ending this blog post. There are so many flags from each command that we haven't talked in this blog but you can explore them by using man <cmd name>
. There might also be many commands that we haven't talked about which you can explore. But for the most part, everything is covered and you won't miss any command that we haven't covered in your day-to-day tasks.