Linux

Cloud Essentials

Linux

Those who are starting their cloud journey will eventually come to this stage where we learn Linux. So here we are, I would highly suggest not just to read it as a blog but use it as a hands-on manual. Let's drive straight into it.

Set-up A Virtual Box

I'm not just going to tell you commands and you'll have to run them through and through. That's easy. I want you to struggle a little bit. LOL.

  • If you guys work on Ubuntu. Great. If not or if you have windows then:

  • Set up a virtual box.

Virtual Box is a tool which will enable you to run different operating system virtually on your host operating system.

  • This virtual box will enable us to run Ubuntu operating system virtually on our Windows 10 system.

  • Figure it out is a skill in itself. Your Welcome.

Why bother learning the command line?

The graphical user interface or GUI is what we see and what we generally use. But to be a professional you need to know more, what happens in the background and many other tasks that cannot be fulfilled by the GUI we need CLI or the command line interface.

  • CLI is faster than the GUI.

  • One can go into the details or how and why things work the way they work.

  • Learning Linux is a powerful tool you can utilize on your projects and tasks.

While learning the command line you need to be familiar with two terms:

  1. Shell - The shell is a program which takes command from the keyboard and gives them to the operating system to perform.

  2. Terminal - A tool which can be used to pass the shell cmd. This program opens a

    window and let you interact with the shell.

    A terminal in ubuntu

Ctrl +Alt +t - Shortcut for opening the terminal in Ubuntu.

Commands

Before starting you should know What is a file system?. Like any other operating system, the files in Linux are also arranged in hierarchical directory structure which means arranged in a tree like manner.

  • A directory in Linux is same as the folder in windows.

  • A directory may contain other files also.

  • The most basic directory in Linux is called the Root Directory. Here all the directories and files reside. Also shown by a / forward slash.

Let's begin

pwd : stands for present working directory. It tells us the present directory we're in.

ls command

ls : is called list. It lists all the file and folders of the current working directory.

ls downloads/: would list all the files in the downloads folder. Similarly it can be done with any directory.

ls ~ : will give you the same content that ls will give you.

ls -l: gives you the content in detailed format including the date, user info, the permits etc.

ls -a: shows the hidden files. Hidden files in Linux start with .

ls -al: This combo will give you the detailed info about all the files including the hidden ones.

ls -lS: This will sort the file acc. to their size.

ls *.html: This will list all the file with .html extension. Asterisk stands for all.

ls -lS > out.txt: It will create a new file named out.txt and fill all the content of the listed file into the text file.

ls -d */: It lists out all (*) the directories.

ls -R : Show the directory structure as a tree.

man ls: will give all the options and parameter that can be used with the ls command.

cd command

cd : stands for change in directory. cd / would lead you to the root directory.

cd .. : This command bring the directory to its parent directory.

cd usr/bin/ : If you want to go to a absolute path like bin.

cd document/book/: To move into a relative file like book in a document file.

cat command

The cat command reads files sequentially, displaying their content to the terminal.

cat list.txt : It

he content of the file.

cat list.txt list2.txt : This way it will display content of both the files altogether.

cat -b list.txt : This will add line number to the lines except the blank lines.

cat -n list.txt : This will add line number to all the lines including the blank lines.

cat -s list.txt : This will squeeze big line breaks in the file to one blank line.

cat -E list.txt : This will add dollar sign at the end of every line displaying that it's the end.

man cat : It will list each and every parameter and option that can be used with cat command.

Redirection in Linux

Redirection simply means capturing output from a file, command or program and sending it as an input to another file, command or program.

cat > test.txt : This command will create a text file names test and here we can enter the content manually. The symbol ">" is used for redirection. Ctrl +d to indicate the end of the file.

cat test.txt : This command will display the text inside the file.

create directory

mkdir : creates the directory or a folder.

mkdir image : creates a folder named image.

mkdir image/pics : creates pics file inside already existing folder image.

mkdir -p names/mark : creates both directory and sub-directory at the same time with the help of -p attribute. When done otherwise it won't create a sub-directory named mark if there is not a already existing directory named names.

mkdir -p names/{john,tom,drew} : To create multiple sub-directories inside the names directory ensure the files names into curly braces and name them without spaces.

remove directory

rmdir abc : helps in removing a director abc.

rmdir -p a/b/c/d/e : Removes all the subdirectories and the parent directory altogether.

rmdir -pv a/b/c/d/e : This removes all the directories giving the extended information about the deletion process.

rm -r a : Also removes the whole directory along with the files created inside sub directories. -r stands for recursive.

copy

cp file.txt file2.txt : Suppose there exists a file named file.txt. We have to copy the content of file.txt to another file that is not created yet. So, this command helps copy the contents of file.txt to file2.txt while creating the new file at the same time.

cp file.txt dir1 : copies file.txt to directory dir1.

cp file.txt file2.txt dir1 : copies both files to dir1.

cp -i file.txt file2.txt dir1 : helps overwrite if any of the file name collides.

cp -R dir1 dir3 : To copy contents of dir1 to a new directory dir3.

move

mv file1.txt file2.txt : content of file1 gets transferred to file2.

mv file2.txt dir1 : moving contents of file2 to a directory.

mv dir1 dir2 : This will move the directory dir1 to dir2 and all the other files inside dir1 will also be moved inside dir2.

less

less big.txt : If there is a big text file and after a certain point terminal won't allow scrolling back to the top. If you want to read the text from the starting, less command is used. Down ⬇️ key is used to scroll down to the rest of the content.

If you want to read the content page by page , you can hit space bar to jump to the next page.

If you want to read the page backward, pressing B would take you back to the prev page.

shift + G : This will browse you to the end of the text file.

1 + g : This will browse you to the top the text file.

To search : To search inside the file inside the terminal use /book suppose the word is book.

man less : would give you detailed description about less command.

touch

touch : is the easiest way to create new empty file in Linux. It also changes the time stamp of a file.

touch file1 : It will create a file name file1.

touch file1 : If I enter the file1 once again it will now show the time when this command was hit for the same file.

nano

nano is a small and friendly text editor. Beside text editing nano offers many extra features like interactive search or replace.

nano file.txt : Nano also creates a new file along with opening the file in the terminal.

Ctrl + O : will save the written content in the file.

ctrl + x : will get you out of the nano editor.

sudo

Abbreviation of sudo command is "Super User Do". It gives you extra privileges as an administrator or a root user.

cd /etc : First if we move into a system file one like etc.

mkdir newdir : and try to make a new directory. It will generate error like ' Permission Denied'. All the permission denied error can be solved using sudo cmd.

sudo mkdir newdir : Now if we rewrite the command with sudo. It will ask for the password that was entered by you most probably at the time of login. And the new directory is created.

sudo -s : means you are now a root user mode. Now you can execute any system command.

top

Top command provide the real time view of you running system. To see which program is taking most of your CPU or computer memory then you can use Top command.

top : Simply enter top to see that it presents a list of programs running in real-time which refreshes every 3 sec.

You can perform a set of instruction inside Top view. Simple press q to opt out.

kill

It requires PID inorder to kill a certain process ID.

pidof unity-control-center : This way we get the process ID of a ongoing process.

kill 3286 : This will kill the process.

kill -KILL 3292 : This will kill the command forcefully.

kill -9 3282 : This flag is also used to kill.

ps -ux : This will give you long list of processes if you're not able to find process IDs.

ps -C gnome : Shows you all the list of instances used by gnome.

echo

echo hello world : Echo displays whatever is written after the command like "Hello World".

my var ="Mark" Suppose we assign a variable a value.

echo $my var Will return "Mark" as this was the value assigned to my var. It echoes the content of the variable.

But this value will be valid in the present terminal only.

echo -e 'some \n text : The flag -e enables \n to indicate as next line. So some text will be printed on separate lines.

file permissions

chmod o+x file : Gives permission to others to executable. X stands for executing file.

chmod o+w file : Gives permission to others to write. W stands for writing file.

chmod g+x file : Gives permission to the group to executable.

chmod g-wx file : takes away permission from the group to write and execute. Minus wx.

chmod a-rwx file : takes away the permission from all to read, write and execute a file.

directory permissions

ls -ld dir : to list all the permissions given to the directory.

chmod u-w dir : This will remove the write access from the user in the directory.

touch file1 : Now if we try to create a file manually. It will generate error because we've taken the rights from the user to write a file.

chmod u+w dir : Giving the permission back.

chmod u-r dir : Now taking the read permission from the directory.

ls : Now this command won't be able to list all the files inside the directory coz we've taken the permission from the directory. We cannot read the dir.

So on and so forth.

octal and numerical permissions

chmod 000 file.txt : 0 represents no permission. This command means 0 permission to the user, the group and others to read, write and execute.

chmod 755 file.txt : 7 means all 3 permissions (read write and execute) for the user.

5 means only two permissions which (read and write) for thegroup.

5 for all others also which is read and write.

This is the chart to convert binary to octal.

Next part we'll inculcate most of these basic commands in the bash scripting blog. Stay tuned. Ciao.