Navigating Around Bash

The directories in Ubuntu are in the following order:

By default when you open Ubuntu, you'll land on /home/<username>
which in my case is /home/saatweek



To confirm this, type in 
pwd
pwd stands for present working directory, and will show the absolute path of whatever directory you happen to be in (as seen above).

To go to the previous directory, type
cd ..
Since we're currenly at /home/saatweek/, typing that once would take me to /home/ and typing it again would take me to the root, i.e., /
Check it out for yourself and type in pwd to see the present working directory at each step

Remember, / means you're in the root directory


To list all the directories and files that are in the present working directory, type in 
ls

It's not all though, ls may take in 3 types of arguments as well -a, -l, and -la
Ubuntu has some hidden files, that are by default, not shown, or listed by the ls command.
To show all the hidden files, type in 
ls -a

Notice the additional . and .. files that were not shown earlier


If you want a detailed list view of all the files and directories, then type
ls -l



And if you want to combine both of these functionalities (i.e., get a detailed list of all the items, including the hidden files), then type in 
ls -al 
or 
ls -la
(same thing, the order doesn't matter)

Notice that the previously hidden directories ( . and .. ) are also included this time

Alright!
Now let's go back to where we started from!
To change the directory, we type cd, followed by the directory name which lies inside the present working directory, or cd followed by the absolute path of the file (if you know exactly where the file lies in the system)
So, if I have to enter the folder home, we'll  type
cd home

So here, I changed the directory to home and typed pwd to confirm that we are, indeed in the home directory


Let's check what other files/directories we have inside home 
ls



To get into the saatweek folder, type
cd saatweek


Ok!
Here's a short cut!
Since I already knew the location of saatweek, I could've simple entered the absolute path of saatweek  (while I was at root directory) and get there directly from the root
See the example below


There's also a shorter cut to this short cut!!
If you type cd without any arguments, you'll reach to your default working directory (which is /home/saatweek in my case)



Now that we can move in and out of different directories, let's see how to create  our own directories!

simply use mkdir followed by whatever name you want to give the directory
for example, type

mkdir sample_folder

A new folder got created after typing the command

To create multiple new folders, simply type in multiple names divided by a space
For example, if I have to create sample_folder1, sample_folder2 and sample_folder3, then I'll write
mkdir sample_folder1 sample_folder2 sample_folder3


And now that we have seen how to make new directories, let's see how to delete them as well!

To delete an empty directory, type rmdir followed by the directory name. So if I have to delete sample_folder2, I'll write

rmdir sample_folder2

But remember, rmdir can only be used to delete empty directories.
Directories with any sort of content in them will prompt an error message

If you want to delete all the empty folders, type (this is usually used to get rid of the clutter)

rmdir * 

That's basically it.

All that's left for us, is to cover one special case.

What if the file names have Spaces in between them?

Let's check out all the directories I have in my current path


Now if I try to get in 'My Folder' directory, it gives me this error:


To avoid this error, type in \ before the spaces, this will tell the system to avoid the space following the \
So your command will be

cd My\ Folder

Same rule goes for creating Directories with spaces in between their letters
For Example, type

mkdir My\ Folder2


If you were to write mkdir My Folder2 (without the \), then this would've created 2 different folders My and Folder2

End of the tutorial! 

Now that we know how to navigate through directories and make and remove directories, let's clear up all the clutter that we've made so far, i.e., let's just delete all those empty files (guess the command?)

Notice how 3 folders couldn't be deleted, it's because they have files in them


Bonus Shortcut if you made it this far!

You don't always have to type in the entire file name all the time, simply press TAB after writing first few words and the system will autocomplete the rest

If there are multiple files starting with the same few words that you just types, then pressing TAB twice will give you a list of all the files that start with those words

THE END

Don't forget to practice all the codes in your PC
Happy Coding!


Previous Post : Setting up Ubuntu and GCC
Next Post : 

Comments

Popular posts from this blog

Solving Sudoku

Plotly

Computing Expressions