In the previous part, we went through a basic understanding of Linux OS. In this part, we will start our journey with Linux commands.
Before jumping into commands, we should know two terms :
1. Command Line or Terminal: It is a text-based interface to the system. Basically, it is an environment that contains a shell.
2. Shell: The part of OS which interacts with Kernel on behalf of applications/utilities. (See the Linux Architecture in the previous part)
There are several types of shell available such as :
Bourne Shell (sh), Bourne-Again Shell (bash), C Shell (csh), Korn Shell (ksh), Z Shell (zsh) etc
but the most popular is bash. We will also use the bash shell in our commands.
Note: -> Linux commands can be easily run on Linux OS-based systems or MAC OS-based systems but if you are a windows user and you want to do a hands-on with Linux commands then you can create an account(for free) on AWS & launch an EC2 instance with Linux system & do the hands-on or you can use an online platform to do the Linux commands hands-on.
One such platform is -> https://killercoda.com/playgrounds/scenario/ubuntu
log in for free & do the hands-on( chose whichever suits you).[WSL can be used too but I used the online platform to do the Linux commands hands-on]
Time to do the hands-on :)
How to find the shell of your Linux system?
In the above screenshot, we have used the command echo with the built-in variable SHELL(in Linux, we use $ sign to identify a variable). echo is used to show the output on the terminal. Here we can see the output "/bin/bash"
In Linux, We can achieve the same result in more than one way. Here we can see that echo & printf both commands gave the same result, the only difference in both the commands is echo prints the output with a newline. If you wish to see the same with printf command then you have to use "\n"
The echo command is more popular than printf & we will also follow the same wherever it will be applicable.
How to find the current date?
you can simply use the command date to find out the current date. If you want the date to show in calendar form you can use the cal command.
How to find the OS-release version of your Linux system?
Here you can see the Linux distribution or version is "Ubuntu" & Release version is 20.04 means Linux stable version which was released on April 2020. Run the same command & find out the version in your system. If it shows 22.04, it means the version was released on April 2022.
Consider the Codename as the nickname of that particular version just like android versions KitKat, Lollipop etc.We can find the find OS version info in this way as well. Here we are looking into the content of the file "os-release" which is under the directory /etc.
We will look into the directory, files, and cat commands shortly.
How to find the kernel name of the OS?
The command uname is giving the result Linux actually LINUX is an OS whose Kernel's name is also Linux.
So if someone asks whether Linux is an OS or Kernel, we can say it's both, the reason we have just seen.
If you want to clear your screen you can either type the command clear or press Ctrl+L to clear the screen.
* Linux is case-sensitive, so Command uname is not similar to Uname.
Let's see how to do the navigation:
Where are we?
pwd is the command which tells us our current or present working directory. The command pwd stands for Print Working Directory. Here we are currently in the ubuntu directory which is a sub-directory of home. We have mentioned in part -1 that home is a directory that contains the user, so here the username is ubuntu.
pwd is a very helpful command, whenever you will fill like you are lost while moving from one directory to another just use this command to figure out your current working directory. You'll find that a lot of commands in Linux are named as an abbreviation of a word or words describing them. This makes it easier to remember them.What's in our current location?
Once you are aware of your current location, you would like to know what's in there. This task is done through the command ls. ls stands for list.
Syntax: ls [options] [location]
The symbol [] represents that it is optional.Here we have used ls without any arguments. This will list all the directories and files inside our current location.
Here we have used the syntax ls [options].
-a is a flag & using it with ls shows the hidden files as well.Here we have used the flag -l with ls which lists out files & directories info in a longer format.
-l has following
1. The first character tells if it is a file(-) or a directory (d)
2. Next 9 characters tell about the permissions for the owner, group & others (We will look into the permission concept later)3. Next field shows number of blocks
4. Next field shows the owner of the file or directory
5. Next field shows the group to which the file or directory belongs.
6. Next field shows the file size.
7. Next field shows the file modification time.
8. The last field shows the name of the file or directory.We can combine the options as well
let's try ls [location]
Here we have provided the path or location /var/log.
We can make ourselves comfortable with the command ls by trying out combinations of different options & paths.
How to move around directories?
This task is done through the command cd. cd stands for Change Directories.
syntax cd [location]
Let's see what we have done in the above example.
1. First we moved to root (/) directory by using the command cd /
2. We checked the present working directory by running the command pwd. The result is showing that present working directory is root(/) directory.
3. We checked what is inside the root directory through the command ls.
(This output gives you the glimpes of Linux File System Hierarchy which we have covered in previous part.)
4. Then we moved to the directory called var.We then listed out the var directory & moved inside the sub-directory called cache & follow the same process one more time.
5. Then we returned back to the previous directory by running the command cd ../ (.. is use to go back to one step back). We confirmed it by running the pwd command. We followed the same process one more time.
6. Then we used the command cd . (. is to stay at current directory & .. is to go back to previous directory). We confirmed it by running the pwd command.
Creation of Files & Directories:
1. We use the command mkdir to create a directory.
Syntax: mkdir [options] <Directoryname>
(the sysmbol <> denotes that we have to replace it with some value)
To create a single directory called messi, we used the command -> mkdir messi
To create multiple directories, we used the command -> mkdir ronaldo neymar
To create directories with numbers, we used the command -> mkdir mydir{1..3}
(It created 3 directories mydir1,mydir2,mydir3)
To create directory path(directory inside a directory), we used the command -> mkdir -p mydir2/gokuldham/jethalal
2. We use the command touch to create an empty file.
Syntax: touch [options] <filename>
similarly to mkdir, we have used the command touch in above example.
We can see all the files are having size 0.
Note -> Linux is extension-less. We have used the file extension .txt for our understanding only.
Removal of a file or a directory :
Removing or deleting a directory or a file is very easy but one thing to notice is we don't have undo option in the Linux command line so be careful while performing deletion activity.
a. Removal of a directory :
Let's try to understand what has been done in the above example.
1. First we created 3 directories ActionMovies ComedyMovies SenselessMovies. Then we tried to remove the directory senselessmovies but we failed as we know Linux is case-sensitive & we used the directory name senselessmovies instead of
SenselessMovies.
2. We removed the empty directories SenselessMovies & ActionMovies using two different commands. The command rmdir can only remove the empty directory but the command rm with the option -r can remove the non-empty directory as well.
3. under the directory ComedyMovies we created one more directory named AndazApnaApna.
4. We tried to remove the directory ComedyMovies using the command rmdir but we failed as ComedyMovies is a non-empty directory.
5. We used the command rm -r ComedyMovies to remove the non-empty directory ComedyMovies.
6. We again created 3 non-empty directories mydir1 mydir2 mydir3 & to remove these 3 non-empty directories we used the command rmdir mydir*. It removed all 3 directories in one go.
7. In step 6 we used * which means 0 or any number of appearances. So even if we have directories mydir1 mydir2 ........ mydir1000, we can remove all the directories in one go by using * with our directory name.
Takeaway -> rm -r dirctoryname will remove both empty & non-empty directories but rmdir dirctoryname will remove only the empty directory.
b. Removal of a file:
To remove any file we use the command rm.
syntax rm [options] <filename>
In the above example, we have created two files named myfiles.txt & examplefile.txt. We used the command rm to remove the files. As we can see in the above example, option -v gives the information about the action taken.
Renaming of a file or a directory:
We use the command mv to rename a file or a directory. Although mv stands for Move but in the Linux command line, it is used for renaming as well as moving a file or a directory. In the above example, we have created a directory mydir & a file myfile.txt. We used the command mv to rename the directory to newdir & similarly renamed the file to newfile.txt by using the command mv.
Moving a file or a directory:
As mentioned earlier, mv is the command to move & rename any file or directory. In the above example, we can see we have moved the file & directory using the command mv.
Copying a file or a directory:
There are many reasons why we may want to duplicate a file or directory. Often before changing something, we may wish to create a duplicate so that if something goes wrong we can easily revert to the original. The command we use for this is cp which stands for copy.
syntax cp [options] <source> <destination>
a. copying a file:
in the above example, we have copied the file jethalal.txt from the directory gokuldham to the directory gadaelectronics.
b. copying a directory:
Continuing with the previous example, we tried to copy the directory gokuldham to a new directory called sodashoap but we failed the first time.
To copy a directory, we have to use the option -r with the command cp then only we will be able to do so.
Note -> cp -r <source> <destination>
1. if the destination exists and if it is a file then it is an invalid operation.
2. if the destination exists and if it is a directory then it copies the file inside the destination directory.
3. if the destination doesn't exist then it will copy the directory to that destination name(It is the same case in our example)
Manual Pages:
Manual pages are set of pages that explain every command available on our system including what they do, how to run them and what command line arguments they accept.
syntax -> man <command to look up>
when we will run the command man ls result of the command will look like below
To come out of the manual page just press q(shortcut for quit).
Whenever we need to find out about any specific command like what that command does, how to use the commands etc then we can take the help of manual pages.
Learning the Linux command line is just like learning to swim. We can't learn to swim by being outside of the water similarly to learn the Linux commands we have to do the hands-on. At first, the Linux command line may feel scary but with some practice, we can be very comfortable with it.
In this part, we have seen a few commands & learn how to use them. We will continue our journey with part-3 where we will go through some advanced concepts & examples of them.
Thank You For Reading!!
#Linux#devops#trainwithshubham