So in a previous video we saw that a kernel is an essential part of an operating system. When you boot up your computer the first thing that kicks into action is the kernel. Think of the kernel as an ultimate traffic controller managing how your systems resources like CPU, memory and storage interact with applications. If you look at the whole
setup at the very core you have got the kernel the main component of the OS then comes the OS itself which builds upon a kernel and often comes with a layer of graphical user interface. This is what we see and interact with by clicking buttons and navigating through the menus. GUI involves comparatively more steps to send a request and receive a response from the kernel. Meanwhile, a shell allows you to directly interact with the kernel. It's an interface between you and the kernel. Let's see an
example of viewing the contents of a small text file to understand both the workflows. In a most simplified overview of the GUI workflow to view the contents of a text file, you would first double click it to open the file. The graphical server like Wayand or X server detects this click event and forwards it to the file manager. The file manager then uses APIs like GTK requesting the kernel to open the text file with a default text editor application. Whereas in the CLI workflow, if you know the command, you just type it into the shell which makes a system call to the kernel to execute the command and displays the file contents. As you can see, the CLI way is
almost a direct instruction to the kernel. So shell is your direct line to talk to the kernel allowing you to execute commands, manage files and control your system often without needing the GUI. In this series of videos, we will start with a few basic commands to manage the files and directories followed by text manipulation and redirection. By the end, we will write simple bash scripts and see a few real world examples. Right, let's get started.
The default shell in most of the Linux distros is born again shell or simply bash. We can access it using terminal emulators. In Fedora the default terminal is tyis. Earlier versions of Federa and DES like Cinnamon use gnome terminal whereas KDE plasma uses console. We can check the default shell
by typing echo dollar sign zero. This prints out the location of bash or just says bash. Let me remove the OM theme so that we can have the default look. If you have a Gnome terminal, you can open it by pressing Ctrl AltP. If Tyis is not opening with that shortcut, you may have to configure the keyboard shortcuts in the settings.
Along with the terminal window, I will keep a file manager window open on one side so that we can see what's happening while using the terminal. Before starting, let's have a look at the directory structure in Linux. At the top level, we have root. We denote it with forward/ character.
All other directories are jail directories of root and most of these store system files, application binaries, log files, etc. We may need root privileges to access some of these directories. I explained about root privileges and how sudo allows us to get root access in an earlier video. We may not touch these directories in this video unless necessary. One of the subdirectories of
root is home. This is where all the user data is stored under a separate subdirectory with the user's name. If there are multiple user accounts, each will have a separate subdirectory in home. I have two user accounts on my system. So there are two subdirectories. When we enter our
accounts directory, we can see many subdirectories. This is our account's actual home directory. Whenever we open a file manager, we are always inside this directory. Now let's check the terminal window. When we open a terminal window, we have a few things displayed by default. The part before the
colon displays the username followed by the host name. We can modify the host name from the settings. Whereas the part after the colon character displays the path of the current working directory. Here we have till day which is a single character representation of the user's home directory. That means whenever we open the terminal, we are in our home directory similar to our file manager. Then comes the dollar symbol which is a prompt. Whenever you see a prompt, you
would also see a blinking cursor which means the terminal is ready to take commands from the user. Now we know that we are in our home directory. To see the full path of this current directory in the file manager, we can click on the path bar and see it. So the path starts with forward
slash which means root and then the home directory and finally the user's name. Note that each subdirectory is also separated by a slash. So in this video whenever we are representing the root directory we will refer to that as forward slash or root. Whereas for the separators we will just say slash. Now to see the path of the current directory or current working directory in the terminal we can use the command pwd which is short for print working directory. We can see that home directory has many subdirectories. In the terminal if we
want to list out the contents of the current directory we can use a command called ls. Let me make the colors a bit brighter. If we want to see additional details regarding the contents of the directory instead of just listing their names, we can add parameters or flags or in simple terms options to the ls command. So to see details of the contents, we have to add -l to the ls command. Note the gap between the command and the flag. Now it displays more information about the contents. We
will see what these details are a bit later. So what flags can be used with ls? To know that we can use the man command short for manual. Type man ls to see the available flags or options. We can also use the double-helpp flag with the ls command. Note that these flags are different for each command. As we can see, we have a
long list of options for ls. What about our first command pwd? It has only two options. Coming back to the ls flags, we can see that some of these have two variations. The single dash variation is the short version whereas the double dash variation is the long version. If available, you can use either of these variations. So adding the single- a or
double dash all flag means that ls will not ignore the entries starting with a dot. What does this mean? What are these dot entries? Let's try adding the double dash flag to ls. So along with our earlier items, we can also see additional entries starting with a dot.
Why are these not visible in our file manager? Because these are hidden items. Click on the file manager window and press Ctrl H to view or hide the hidden files. Now we can see the dot items. Either it's double dash all or
single- a both give the same output. Note these single and double dot entries. We will come to this in a minute.
We have seen how a flag enhances the ls commands capabilities. We have used two flags - L and - a. Can we combine these flags? Certainly. We can use multiple flags at a time. So if we want to include hidden items along with the long list, we can use both the - a and -l flags. We can also add both the flags with just one dash like ls dash al. Now we are
seeing all the contents in our current directory including hidden files in the long list format. By the way, you can navigate through the previous commands using the up and down arrow keys. In the help output, we can also see how the command can be used. Here the options and file are
enclosed in square brackets which means these are not mandatory. We have already seen that ls works as it is and doesn't require any additional options or file names. So by default it lists the contents in the current directory. We can give a directories
path to ls to see its contents. In bash a single dot represents the current directory. So if we type ls dot the output is same as earlier. Now in my downloads directory I have a few items. To view its contents I can type ls downloads or ls dot / downloads both give the same output. If there are no contents in a directory like my music folder then it won't return anything.
So now we are in this directory and if we want to look at the contents of its parent directory we can either type the full path if we know it which is forward slash home or we can use double dots. So double dots represent the parent directory of whichever directory we are currently in. Till now we have encountered four representations for directories. forward slash till day single dot and double dots. So ls dot lists the contents of
the current directory. ls double dot lists the contents of the parent directory. ls tillday lists the contents of the user's home directory as of now it is the same directory as we are in and ls forward slash lists out the contents of root directory. Further in
the help output we can see three dots after options and file which means we can provide multiple options and paths to ls. Just like giving multiple options to the ls command we can list out the contents of multiple directories at a time. So if we type ls downloads space documents we can see the contents of both downloads and documents directories. But be careful while using spaces. If we type lsd downloads slash, these are the contents of the downloads directory alone. But if we type a space before slash by mistake, then we are dealing with two different directories, it prints out the contents of downloads and root. Right? Another useful command to
see the actual directory structure is tree. Without any flags, the tree command prints out all the files and subdirectories of the current directory recursively. We can add flags like dash d to show only the directories or dash uppercase l to restrict the levels of subdirectories like tree dash uppercase L1 which displays the first level contents and is similar to ls but with a tree structure. Note that uppercase L flag takes an argument or a value. If the value is not provided, it throws an error. There are
many flags for the tree command. You can explore its help output. Now let's have a look at the details are the metadata of our listed contents. In these columns of data, the first character represents the type of that item. Here we are seeing D, which means it is a directory. The next nine
characters represent the items permissions for the current user, the users group and others. Groups are useful when we have multiple users on the system. We can categorize them into groups so that they can have specific privileges over others. For example, if we have five users and we want only two of the users to access a directory, we can create a group and add these two users to the group. Then set the same
group to the directory. By default, a group gets created with the same name as that of the user. So here the first three characters say that the current user that is your account has read, write and execute permissions for this directory. The next three characters indicate that the current users group has read and execute permissions whereas others have read and execute permissions as indicated by the final three characters. Then we have a dot character for some of the items.
This is specific to the Linux distros using SE Linux security. The dot means that the item is SE Linux secured. If you are using Ubuntu, you may not see this character unless you have installed and enabled SE Linux. The next column
indicates the number of hard links that the item has. There are two types of links in Linux, hard and soft links. It is a bit out of scope to explain these in this video. If you are interested, you can go through the link resource. Then comes the username and group name.
As I mentioned, by default, both have the same names. After that we have the size of that item. Then we have the date and time columns. It is the date and time that the item was last modified. Well, you know the last column, it is the name of that item. We can see that all visible items in our home directory are directories. If we add both L and A
flags, we can see a little bit of variation since we have files as well. The files have dash as the first character. If you look at the size column, it displays everything in bytes. So if we have large files, obviously the size will be large and may not be readable. What we can do is we can add an extra flag h to make it human readable. Now we have a converted form
of the size column. Let's take a look at the root directory. Now we can find additional types like a soft link or sim link items having more than one hard link varied permissions. Also a new type of permission T which represents sticky bit again out of scope for this video. Also
note that we have a different color for each item type. Right? That's about the metadata. Till now we have always been in the home directory and have never moved away from it. So the next command is used to navigate to the file system which is cd short for change directory. It takes a
destination path and changes the current directory to the given destination. So if we want to move to the documents directory, we can type cd documents. Note the change in the current directories path. Now to go back to the home
directory which is a parent directory to our current directory. We can simply type cd double dots. We can also move to suble directories in one go. Let's create a
directory in the documents directory and a few subdirectories inside it. We will see how to create directories using the terminal after this section. Check the structure by typing tree in the terminal. So to navigate into one of these subdirectories, we can start typing the full path like cd documents / docub / sub 1. Now in whichever directory we are if we want to navigate back to the home directory we can either use the cd tillday where till day represents the home directory we already know that or just type cd typing just cd always takes us to home directory. There is another
shortcut that can be used to switch back to our previous directory. So we were previously in the newly created sub one directory. Instead of retyping the whole path, we can use cd dash to toggle between your previous and current directories. Let's go back to our home directory and look at another useful shortcut. While typing the paths of directories, hitting the tab key tries to autocomplete the name of the directory or file if it exists. Now to navigate to the music directory we can start typing cdm m and then hit tap and it will autocomplete the directory name for us. Note that this worked because
there is only one directory which is starting with the uppercase cm. Let's say we want to navigate to the downloads directory. If we type cd capital D and then press tab once, this will not autocomplete because we have three directories starting with the letter D. But if we hit the tab twice, it will display all three directories indicating that we have to choose one of those.
Then when we type O, it still won't autocomplete as we have two directories starting with DO. So order complete works after typing cd d o w and then pressing the tab key. What if we type cd uppercase a and hit tab once or twice? It does nothing as we have no file or directory starting with a. The tab key is really helpful when we are exploring the sub levels. So instead of typing the full path, we can start hitting tab.
When there is only one file or directory, hitting tab will automatically fill that for us without typing anything. cd doc tab tab and then tab 1. We have been moving downwards into the sub levels. Now let's look at the other way around. If we want to move
back to the documents directory, which is two levels up from here, we can type cd double dots/ double dots. You can keep adding the double dots and move upwards. However, it may become messy as we are not using any directory names. And you may also reach the top of the tree and can't go any further.
Let's go back to the documents directory. Now from here, if we want to navigate to the downloads directory, we can type cd double dots /d downloads. Try going back to one of those subdirectories in the documents directory right from here. So we can
type whatever the paths we have been using till now with the cd command are called relative path because those were relative to the current directory. In relative path always the current directory is the starting point. On the other hand we have absolute path. Absolute paths always start with forward slash that is root. So from the current directory if we want to go to the downloads directory we can type cdouble do/d downloads which is a relative path or type cdward slashome/ username/d downloads which is an absolute path. The pwd command always outputs the complete path which is an absolute path.
Earlier we created new directories by using the rightclick menu in the file manager. Now let's see how to create directories using the cla. To create a new directory we can use the command mkdir followed by the name of the new directory.
Unlike ls and cd mkdir should be provided with the directory name. If we don't give the name it will throw an error. However, we can give multiple names to create multiple directories at a time. We can also make subdirectories
within a directory from the current location. We can use all the relative and absolute path knowledge to create directories at any location. Note that we are making subdirectories within existing directories. We have come back to our home. Now let's try to create a new
directory and a subdirectory within it at the same time. Will it work? No, it throws an error. To make this work, we have to use the P flag. It creates a
subdirectory and all its parent directories. Well, we have made so many empty directories which are of no use. So, let's remove them. To remove empty directories, we can use rmdir followed by the directory name. Note that it can't remove directories with contents. It works just like mkdir but in the opposite way. So to remove the
new directory, we can type rmdir and the directories name. We know that all the newly created directories are empty. Can we delete all of those? Let's try. This deletes only test 4 because the other directories are not empty. They have subdirectories. So let's delete the
empty directories along with their parent directories using the p flag just like mkdir. At this stage let me introduce you to the wild card character asterisk. Let us create a few test directories like earlier. mkdir test one test two
test three. Now these do not have any subdirectories. So if we want to delete these three directories at a time, we would usually do rmdir test one test two test three. But we have common characters in our directory names and only the last character is different. So
we can use the wild card character and delete all three directories by just typing rmdir test asterisk. So a wild card represents any character as well as any number of characters. We have two directories with main underscore as the starting part.
Let's create one more nested directory. If we check the structure of these main directories, we can see that one of the directories has two suble whereas the other two have one suble. If we use rmdir -p main asterisk / asterisk, we can delete those with one suble and rmdir-p_aststerisk/ asterisk/ asterisk to remove the other one. If we use just the asterisk, it would match all the characters. So if we had multiple
subdirectories in the main diir it would delete all subdirectories. So be careful while using just the wild card. We can also use a wild card at any position in the name. We just have to find the common characters. Now we have two directories which have a common underscore character. We can type rmdir asterisk asterisk to remove these two directories at a time. Okay, that's
about creating and removing directories through the command line. Now let's deal with files. We will do that in a new directory called test trir. Let's create that and navigate into it. To create new files, we can use the touch command. The actual usage of this
command is to update the access and modification times of an existing file. However, if there is no existing file with the name we have provided, it creates a new file. We will also look at creating new files while using standard streams. Now let's make a copy of our text file. We can do that using the CP command. The CP command needs two
things. The item which we are copying and where we are copying it to. that is the source and destination. Let me create a new directory called copied content and then make a copy of the test file in it. Now we can see a copy of that file in the new directory. By default that cp command
won't display any output unless there is an error. So if we try to copy a non-existing file, it will throw an error. Further, we could also give a different name while copying. We can add the V flag to make it more verbose. Right, we copied files. But what about the directories? Can we copy those as well? Let me make a new directory named new diir. Now if we type cp copied contents new diir, it will warn us to use the r flag and it won't copy anything. So as
it says to copy directories and its content we have to use the recursive flag. Note that by default CP overrides files and directories which have the same names. That's about copying files and directories. Now let's see how to move files and directories from one place to another. Just like the copy command, we
can move files by providing the source and destination to the mv command. Let us create a new directory called move content and then move the test file into it by typing mv source file and the destination. Let's move one of the test files from the copied contents directory to the current directory. We can move the directories as well with the move command. But unlike the copy command, we don't need the recursive flag. The move command can also be used to rename files and directories.
Now let's delete the files and directories. We know that rmdir removes the empty directories and it won't work on files and directories with contents. So the command we can use to delete the files and non-mpt directories is rm. We will first delete the text file from the current directory. Let's delete one more file from the copied contents directory. Now the copied contents directory is empty. Since this is a
directory, we cannot delete it with rm as such. Instead, we have to use the recursive flag to delete directories with or without contents. We can also remove empty directories with rm using the d flag. So
basically rmdir is rm with d flag. Now let's delete all files from the mode and rename it directory. Since these are only files we can type rm mode andreamed / asterisk. Now let's delete all files and directories from the current folder. We'll also add the verbose flag. That's it. Whatever we have
created till now is gone permanently. Note that the content deleted using RM cannot be recovered. So be careful. In the next video, we will
see how to manipulate text files and also look at the concept of redirection. Thank you.
2025-05-01 13:28