Basic commands | Linux Chapters
Welcome Back, This chapter is very Important so, before we go any further lets learn some basic commands which will help us navigating around the terminal to get our work done.
Lets go through the basic and mostly used commands one by one.
Always remember If you stuck anywhere you can use man commands or help as discussed in previous chapter.
- pwd (present working directory) :
This command will show us our current path i.e where we are right now. So, that any file operation if we’ll perform will be on the contents of current directory.
- cd (Change Directory) : This command is used a lot. We cant work all the time in one path as we need to change our location to get or put some files or to do anything else hence,
- By typing cd <directory-name> will go inside the mentioned directory and
- On typing cd .. (notice two dots) will come out of present directory.
It becomes more interesting when these both are nested as to meet our requirements:
Example: consider following directory structure:
As we can see letustweak directory is having 5 directories and each its child are also having some other directories inside them.
Like tweak1 directory is having one file (file1) and two directories (tweak1.1 and tweak 1.2). Suppose we are in tweak1.1 and we want to go tweak4.1 we can use:
here two ../../ will first come out of tweak1.1 and another will make it come out of tweak1 directory so that from there we can go inside /tweak4/tweak4.1
I know this is confusing but read it twice it will help us ahead a lot and you will thank me for this. 🙂
Tip: you can press tab key after typing the few initial of directory or files for auto completion of names. This can save you lot of keystrokes.
- ls: This commands list all the files in specified directory.
As we can see ls <directory-name> will list all files in the specified directory (letustweak directory in this case).
Also if we are inside any directory then typing ls will list all files in the current directory.
Important: I did man ls on my console to know more about ls commands and while scrolling i found:
So, what are these..and how to use them?? Actually by adding these options to our ls command we can make it list contents as per our requirements.
example 1: as simply using ls is not showing the file size or any other details. So, we can use long listing format of files using:
ls -l : means list – <using long listing format>
example 2: if i want to list all files and directories even hidden ones (file name starting with dot). I can use
ls – la : means list – <use long listing><also include listing of all hidden files starting with dot>
example 3: If i am having more than 50 files in my log folder and i want to see the list in reverse chronological order i.e. most recent one at last. I can use
ls -lrt : means list – <use long listing><in reverse order><with time>
So, Now are you getting the idea how we can use the man or help commands as per our needs and why we called as life saviors in previous chapter.
Great..!!! now lets explore next one.
- mkdir : For Creating a directory.
- rmdir : For Deleting a directory.
Did you noticed an extra ‘d’ for tweak directory in permissions column it shows that tweak is a directory not a file. We’ll discuss permission in more details just neglect it for now.
- cat : can be used to read contents of file or for concatenating content of files.
cat <file1> This will read contents of file1.
cat <file1> <file2> This will append the content of file2 to end of file1.
- vi/nano editor:
There are no of ways for editing but in here we will discuss the two popular editors used in linux.
nano is pretty popular but vi on the other hand is more popular it can be said as a legacy editor for linux because it is present from long time.
just type nano <filename> in terminal if the file is not present then it’ll create a new file or else it will open existing file.
After hitting enter you will get nano text editor like this.
Just start modifying the contents of file and you can use below control options to perform save, exit or other operations as highlighted. Example ctrl+o to write contents or ctrl+x to exit.
vi editor on the other hand is quite complex.
To access vi editor type vi <filename> in terminal vi editor will open you can either type:
i to enter insert mode.
esc key to come out of any mode ex insert/append mode they type.
:q to quit.
:wq to save and quit.
Remember you can always type man vi to know more about vi editors.
That’s all folk’s keep practicing and put your comments below 🙂