Practice Exams:

LPI 010-160 – Directories and Files Part 2

  1. Creating Links

In Linux sometimes it’s handy to refer to a single file using multiple names rather than to create several copies of it. So users can create multiple links to a single file. These are known as hard links or symbolic links and both of these are created using the Ln command or the link command. Let’s first take a look at the hard link. The hard link is a duplicate directory entry and both the entries are going to point to the same file because they both work together by tying together this low level file system data structures. Hard links can only exist on a single file system. In a hard link scenario neither file name is going to hold any sort of priority over the other. Both of them are going to tie directly to the file’s data structures. So to create a link you’re just going to type in Space, the original name space the link name you want to create. This way you’re going to be able to create this hard link. Now the other type of link we have is a symbolic link.

This is also known as a soft link or a SIM link. It’s a file that refers to another file by its name. This means the symbolic link is a file that holds another file’s name and when you tell that program to read or write from a symbolic link file Linux is just going to redirect the access back to the original file.

Because symbolic links work by file name references they can actually cross file system boundaries. So if you type in LNs original name link name this is going to be able to create a symbolic or soft link. Now in Windows there’s really no equivalent to a soft link, a symbolic link or a hard link that we have the way we do in Linux. The closest thing we have to that would be something like a desktop shortcut where the shortcut is pointing to the real file. But it doesn’t really work the way that this symbolic link or hard link does. The reason is these links actually refer you back into the original file. So when you make changes to these symbolic links you’re also going to be changing the original file too. You.

  1. Wildcards and Case Sensitivity

In this lesson let’s talk about Wild Cards. Now unlike other operating systems, Linux is dependent on the case sensitivity of characters. Now when you’re using a Wild card this is just a symbol or a set of symbols that will stand in for other characters that you may want. Wild Cards can be used when referring to files and it’s also sometimes called globbing. Three classes of Wildcards are common in of Linux. The first is the question mark. The question mark stands for a single character. For example if I use B question L this is going to match a couple of words. It could match bowl, ball, bull or any other four letter character file name that’s going to start with a B and end with an L. Now the second kind of Wild card we have is what’s known as an Asterisk and an Asterisk is that little star on your keyboard. An Asterisk will match any character or set of characters including no characters at all.

So if I go back to the BL example and I put B asterisk L, this can match the same words like bowl, ball and bull but it also can match things like BL, BBL and even words like brawl because brawl the raw can be represented by that Asterisk. Now the third type of Wild card we have is what’s known as a bracketed value. This is when characters are enclosed inside of square brackets and they normally are going to match any character inside the letters that are exposed inside that bracket.

So what will this look like? Well, let’s say we have the first letter with B. Then we have a set of brackets including A and O and then another set of brackets with L and W and then our final L. This will match things like ball and bull but it won’t match bull because there was no U in the first set of brackets there was an A or an O. If we look at the second set of brackets we had an L and a W. So you can see how this works. Now Wildcards are implemented inside of your command shell and they’re going to be passed to the command that you’re calling.

So if I’m doing something like LS for list and then B question L, I’m going to find every file name that matches the three files below things like bowl, ball and Bull because those all have a B, some letter, some letter and an L at the end. Now this would be the same thing as typing out something like LS space, bowl, space, ball, space, bull it’s the same thing but we can do it much shorter by using that Wildcard character of using the question mark. Now it’s important to note that if you carelessly use your Wild Cards you can get some unexpected and undesirable consequences when you’re running commands. So you have to be careful and think through what these Wild cards are going to be.

For example, let’s say I was copying two files using Wild cards to a different directory, but I forgot to give a destination directory. And this can actually cause the first file being copied while the second one is overwritten because instead of going to a directory, I’m recording it to the same location. This would be the same thing as deleting that second file. So again, you have to be careful when you’re using these Wildcards. Now, one characteristic of Linux that sets it apart from all of the other operating systems out there is the fact that Linux is case sensitive. This means that file names that are different in case are going to be two different files.

So for example, if I have a directory that holds a bunch of different files like a file, a file and a file, those are three different files because I have all lower case, mixed case and then all uppercase, even the content of each of these files could be the exact same or completely different. The file system is going to treat it as a distinct file. In Windows, you don’t see this happening because in Windows they don’t treat things with case sensitivity. Whether it’s upper case, lower case or mixed case, that’s all the same inside of Windows. But in Linux that is a distinctly different file and it’s important for you to realize that.

Now, one of the things you have to remember is that case sensitivity is actually a function of the Linux file system and not the Linux operating system itself. So if you’re dealing with Linux file systems within a Linux operating system, they are going to be case sensitive. But if I connect a thumb drive that was formatted on a Windows machine using Fat 32 and I connected to my Linux system, well then that case sensitivity will no longer exist. Like I said, this is particularly important when you’re accessing Fat or NTFS volumes because those are Windows file systems which are case insensitive.

But if you’re accessing from a Linux file system, they’re going to carry over that insensitivity as well. Now, there is a twist on this rule that many Linux programs such as the Bash Shell are going to assume case sensitivity even if you’re accessing a case sensitive file system such as a Windows system like Fat or NTFS. So if you’re using commands that are only going to work using case sensitivity and file names, this can be an issue for you. So I like to treat everything as case sensitive when dealing with Linux. And when I’m working on my Windows file systems, I tend to try to keep things in different file names using different letters and not just different cases. This way I get full cross platform capability regardless of whether it was a Linux file system or a Windows file system.

  1. Manipulating Directories

Now you may think of directories as folders because most graphical user interface file managers are going to represent these directories by using a file folder icon. Naturally, Linux provides that textbased command line environment as well, and you can manipulate these directories from within it. These include directoriespecific commands to create and delete these directories, as well as some file manipulation commands that we discussed in our earlier or lessons.

The first one we’re going to talk about here is the make directory command which is mkdir. This is used to create a new directory. You can do this by typing mkdir and then the name of the directory you want to create. For example, if I use mkdir new folder, this will create a new directory called new folder. This is similar to using the right click mouse button and then selecting new folder within your Windows desktop environment. Now, if you want to learn a lot about make directory, you can use the man page for it to see all of the different options that exist.

The next one we have is remove Directory, which is rmdir. Now, this is the opposite of mkdir because it’s going to destroy or delete a directory. To use it, you simply type the command followed by the name of the directory that you want to have deleted. Take note that the file and directory names are going to be case sensitive, as we talked about in the last lesson, so you have to specify and use the correct case to avoid accidental deletion of some other file or folder.

It’s also important to remember that the rmdr command can delete only empty directories. So if a directory contains any files or folders at all, this command is not going to work and it’s going to give you an error. You can use the dash. P option though, and this can be used to delete a set of nested directories as long as none of them hold any non directory files.

So if I have folders containing folders containing folders, I can delete those using rmdr space, p space and the name of the top directory. But if any of those folders have a file in there, that won’t work either. So if you want to delete directories that contain files anywhere within that directory tree, you need to use the RM command, which was for the Remove file command that we talked about before, and then use it with the r option. If you remember before we said RMR is that recursive delete option and it is potentially dangerous. So make sure you’re only using it when you know what’s in those folders and you know what you want to have deleted. This is why you need to make sure you’re using particular caution whenever you’re using something like RMR.

Now, as you may have known, directories are just special files as far as the style system is concerned. These are basically a file that holds other files. Most of the file manipulation tools that we talked about in our other lessons can also be used to manipulate directories. There are a couple of exceptions though. For example, the touch command can only be used to update a directory’s timestamp. It can actually be used to create a directory. If you want to create a directory, you have to use the mkder command instead.

The copy command can be used to copy a directory, but you have to add the option of R for recursive or A to be able to copy the directory and all of its contents. The move command can move with the right options or remove a directory just like it could with a file system as well. And then we have the Ln command, which was that symbolic link command, and we use Ln with the S we can create a symbolic link to a directory just like we could any other file. Now, directories don’t have support for hard links, but they do have support for the SIM links. So remember to use S with the Ln command if you want to be able to create a link to a directory.

  1. Manipulating Files and Directories

In this demonstration, we’re going to play with files and directories. Now let’s do this. First inside the GUI, we’re going to go over and click on the files icon and then we’re going to go up to the documents. And inside here you’re going to see that I created four different text files bail, Ball, bowl, and bowl. I also have folder one and folder two. That’s all we need to do for right now. But if I want to move these files around, it’s pretty simple, right? Let’s say I want to take the ones that had BA as the first two letters and move them into folder one. I can just highlight them, drag them and drop them. If I want to take the other ones, I can bring them over one at a time, or I can bring them over both at the same time. Then if I want to go look in those folders, I just simply double click.

If I want to go up, I go back a folder and then go back down again. And you can see how quickly you can and manipulate files and folders. So let’s go ahead and put this back the way we had it, with all of the files sitting in the main directory of documents. And now I have two folders that are empty and the four files. Next, what I want to look at is the home directory. In Linux, all of your stuff for the user is stored inside of the home directory.

We have our desktop, which is what you see when you first turn on the computer. You have your documents, your downloads, your music, pictures, public information templates, videos and you can create as many other folders as you want inside this home directory because the home directory is something that you own and is fully used for your user only. Now the other thing we need to talk about is these file extensions. You’ll notice it has TXT, but it really doesn’t need to be there in Linux. File extensions are unnecessary. The fact that there is TXT here is just simply something that I have added for clarity. So as we’re talking about this, you know that these are text files, but Bail TXT is exactly the same as just having something named Bail.

It doesn’t matter. It still recognizes that this is a text file and if I double click it, it will open up as a text file. Again. These text files are empty because I just created them using the touch command. But if it was a text file, the text would be sitting here. So let me go ahead and put the TXT back just because I’m going to use it later on in this demonstration. But you can see that it really doesn’t matter what that file extension is. Unlike Windows, which bases a file’s type based on that three digit file extension, linux instead looks at the contents of the file and then using its understanding of the contents of that file. It determines if it’s a movie, a text file, a picture, or whatever else it happens to be. One of the other things I talked about in the lesson was the fact that we can have hidden files.

So let’s go ahead and take Bail here, and we’re going to copy it and paste it, and I’m going to go ahead and rename this into Bail hidden text. But if I wanted to make it hidden, how do I do that? Well, if you remember correctly, we talked about the fact that if you start it with a dot, it becomes a hidden file. And so by me putting a dot there, you can no longer see that file anymore inside the GUI. But we’re going to be able to see it if we go into our terminal. So let’s go ahead and minimize this for now, and we’re going to go into our terminal environment. All right, now that we are in our terminal, we first want to know what directory we’re in. We can do this by looking at the till day there, which tells me I should be in the home Diontraining directory.

Or again, we can use the command PWD to present what the present working directory is. Now, if I want to see what types of files and folders there are, we can go ahead and use the LS command. The LS command is our basic command for listing out everything that’s there. So let’s go ahead and do that. We’re going to do LS. And you can see here that I’m in the home directory. I have my desktop, my documents, my downloads, my music, my pictures, my public, my templates, and my videos. Where did I put all those files? Well, I put them inside documents. So we’re going to go CD documents. Then we’re going to type LS again. And from here we can see four text files and two folders. But I can’t see that hidden file. How can I display the hidden file?

Well, if I use the LS command, I can do that. So if I do LS la, I’m going to do the long listing as shown here, as well as the attributes listing. And so here you can see that Bail hidden TXT is still there inside this directory. You can see it right there. But in the GUI we couldn’t because that dot made it a hidden file. Now, just like I use the GUI, I can move files into folders, but I can’t just drag them and drop them. Instead, I have to use the move command. So just like before, I want to put the BA words into folder one and the bowl and bull into folder two. I’m going to show you how to do this first by using bull and bull and using a single command. So we’re going to use MV for move. We’re going to use bull TXT and move that into folder two. Then we’re going to put the forward slash and the name of the file, bowl TXT. This is an opportunity that if we wanted to change this from bowl to something else, we could do that right now. But in my case, I’m going to leave it named bowl.

Go ahead and hit enter. And then if you type in LS, you should see bowl is no longer listed as one of my four files. I can do the exact same thing with bowl. TXT and I’m going to put that into folder two, bowl TXT. And now that has been moved as well. The next thing I want to do is move the two BA words. Now because I have those two words that are BA, I can actually move them both using a wildcard. I can do that by doing something like move BA questionquestion TXT. And that is going to move everything that starts with BA and then has two other letters after it.

Or because they’re the only two text files here, I could actually move star TXT and that would move anything that has a text file over into this directory of folder one. And so if I put it in folder one, I don’t even have to list out the file names because all of the text files are going to move into folder One using that command. So if I go ahead and hit enter and now if I do a list, you’ll see there are no more text files there. Let’s go ahead and do an LSLA. Now notice the hidden file that does end with star TXT was not moved. Why is that? Well, because it’s hidden. And if we want to move that file, we’re going to have to call it out specifically using that dot. And so if I wanted to move this file, move bail hidden TXT into folder One, I can do that by using that command. And so now you’re going to see there are no more text files inside the Documents folder.

Let’s go ahead and clear our screen which uses the clear command and then hit enter. And then we’re going to use CD folder One. Now inside of folder One, what should we have listed here? We should have three files, the hidden file and the two text files. And you can see right there, we do have all of those files. Now another thing we can do is we can use relative directories when we’re running a command. So right now I’m inside the Documents folder and inside a folder underneath it called Folder One. But there’s also a folder underneath Documents called Folder Two.

And if I wanted to list the contents of folder Two, I can do that a couple of different ways from this position. One is I can use LS and then list out the name of that directory. And I can do that going all the way down to the root and typing out the full path which would be Homediantraining Documents folder Two. That’s one way to do it. And you’ll see I have those two files. Another way would be doing it using a relative link and that would be doing it relative to the home directory. And so this is actually taking out that Tilde is going to substitute for homedioning and I can put Documents folder two and hit Enter. And now I’ve got those same files or I can actually do LF and do it relative from my current position. And if I do that relative from my position, I need to go up a directory first.

So if I dot that says take me to my parent which is Documents, then I put the slash and then go back down into folder two. And again I get the same result. Three different ways to reference the exact same folder using that LS command. Now when you’re moving things around you can do that same function just like I did when I listed out the directory listings.

So if I wanted to move those files, bowl and bowl from folder two into my current working directory, how would I do it? Well I can again use my move command, right, but because I’m sitting in folder one, I need to reference those files over in folder two. So I’m going to use the relative way of doing it by going folder two that’s up and down into folder two. And then I want to find any file that starts with a B as any other two letters, then an L, then TXT, and I want to move those into my present working directory. Now I can reference that a couple of different ways. Again, I can go ahead and do the Documents folder one that would work. Or I can use slashhomediondocuments folder one that would work. Or I can simply use dot because dot is this special directory shown right here and that stands for this directory that I’m sitting in right now. Whatever directory it is that’s the directory dot is. And so I’m just going to put dot there and hit Enter. Now if I do an LS, I should expect to see four different files bail, Ball, Bull and Bull. And so there you go, we have all four of those right there. And if I do the long form of that, you can also see the hidden one. This should help get you the idea of moving up and down directories in and out of directories using these different commands.

Now that’s the way we can move files and we can rename files by using the move command. We can also copy files over using the CP command and it works just like the way move does, except it also leaves a copy where you copied from. Now one more thing I think is important to go over is the wild cards. And we talked about this back in the lecture as well. You can actually grab information by using stars, which is substitute for one or more character. You can use question mark as I’ve been doing, which is a single character, or you can use bracketed values. And so, for instance, if I wanted to list out everything in this directory using the LS la command, but I only wanted to see things that started with a B, had an AO as a second letter, and had a W or L as the third letter and ended in an L TXT.

What should I see? Well, we should get ball and bowl because Bale ends in an E, not an L, and Bull has a U as a second letter, not an A or an O. Let’s see what happens. And there you have it. We have ball and bull. Now, why didn’t we get bail hidden? Well, for one, bail hidden starts with a dot. And for another thing, bail hidden is more than four letters, right? Because we have bail and hidden, and that doesn’t follow the format of what I just looked for. Now, this idea of globing is going to be used over and over again as you go up in your career as a system admin, because you’re going to be looking for certain things, whether it’s a certain user or certain accounts that fit criteria or certain IP addresses or some things like that. And by being able to bracket this information out, you can slice and dice the information in different ways to get what you need.

Now, the last thing we need to worry about here is directory. So we’re going to go ahead and go up one directory, which is Cdspace, and that gets me back to my Documents directory. Here I should have two directories. I have a folder one and a folder two. Well, what do I do if I want to create folder three? Well, I can do that by using make directory and that’s Mkdir, and then I’m going to call it folder three. Now, if I do a listing, you’ll see I have folder one, two and three. Now, how can I rename a directory? Well, maybe I didn’t want folder three to be called folder three anymore. Maybe I want to be called jason. Well, I can do that by renaming it, by simply moving it by using MV and then using folder three and then calling it what I want Jason. And now if I list it out, you’re going to see that I have folder one, folder two, and a folder called Jason. Now, the last thing is I want to remove a directory because I don’t want these directories anymore. Well, I can do that using Rmdr.

Rmdr will remove any empty directory. Jason is empty because we just made him. And so if we go and use Rmdr jason, that should delete the JSON folder. But if I use Rmdr folder one, I’m going to get an error. Why? Because it’s not empty. But I can remove folder two because it is empty. Oops typed it wrong. All right. So if I do Rmdr, I can then type folder two, and that will delete it because that is a directory that was empty, but I still have folder one that has files in it. So here we are with folder one, and we weren’t able to remove it. Why? Because there’s stuff inside of it. And so if we want to be able to remove it, we have to get rid of the stuff that’s inside of it. Now, one of the ways we can do that is by going into the directory folder one, listing out the contents, and we see they’re all text files. So I can simply remove all those files. To do that, I use RM star, TXT, hit enter, and now those files are gone. I can go up a directory, and now I can go ahead and do my remove directory of folder one. Go ahead and hit enter, and it’s still not empty. Huon, why is that? Well, it’s because of that hidden file we had, right? So if I do LSLA and I look in folder one, what are we going to see? That one silly hidden file. Now, that’s kind of a pain to have to go through and delete all these things individually, one by one in the directory. So because we can’t delete this folder because there’s something inside of it, we need to figure out how we’re going to delete anything that’s sitting inside this folder.

And to do that, we’re going to use the remove command, but we’re going to use a special variant of it. Before I go and show you the command, let me show you this thing called man, which we’re going to discuss more in the next lesson. If you type man and a command like RM and hit enter, you’ll get the manual for that command. So in this case, the RM command takes an option and a file, and that’s how it’s going to operate, right? That’s the command syntax, and you can see that up here under synopsis. It’s going to RM space, whatever your options are, space, whatever file you’re going to work on. Now, as we go through here, you can see there’s a couple of different options. We have things like dash F, which is going to force it to do things. You have other options.

If we go ahead and hit the spacebar, we can go down again like dash, which is recursive, it will remove a directory and their contents recursively. So what we want to be able to do is remove this directory and anything inside of it. Well, we can do that with dash. So let me go ahead and hit quit here, and from this directory, we can hit LS, and you see folder one. And so if I do RMR, which says delete this thing and anything below it, and then folder one. If I hit enter. Boom. We now have gotten rid of that folder, including that hidden file that was sitting inside of it. Hopefully, this gives you a good idea of how to use some of these commands. And I do recommend you play with these yourself. Go into your Ubuntu and create a bunch of files and folders. Start moving things around, copying things around, and getting comfortable. Inside this command line we’re going to play.

  1. Man and Info Pages

As you’ve probably seen by now, the Linux shell is very powerful and very useful. However, there are a lot of commands out there. There are virtually thousands and thousands of commands and even more syntax and so it’s impossible for you to remember every single one of them. Fortunately though, there is an easy to use resource that can give you a lot of information when you’re using the command line. That’s what we’re we’re going to learn in this lesson. We’re going to talk about manual pages, or man pages for short. Now Man Pages are a set of pages that explain every command that’s available on the system. This includes information on what they do, the specifics of how to execute them, and what command line arguments and syntax they’re going to accept. The Man Pages are invoked by running a terminal command and typing man, followed by a space and then typing the command that you want information on. It’s also possible to do a keyword search on the man pages.

This can be helpful if the user is not really sure what command they want to use, but they know what they’re trying to achieve. To be effective when using this type of an approach, the user may need to do a few tries as it’s not uncommon to find a particular word in lots of different man pages. To achieve this, on the command line, just type man space, k space, and then the term you’re searching for.

Do note that you have to use proper spaces between the command, the option of K and the search term. Now, while the Mank option is going to search through all of the various man pages, maybe you just want to search through one man page. For example, if I’m looking at the LS commands man page and I want to search through that to find a particular keyword, I can do that too. To do that, you’re going to press the forward slash on your keyboard followed by the term that you want to search for and hit Enter. If the term is going to appear in that man page many times you can cycle through them by pressing the end button to go to the next version of that keyword.

So I think you can see how important these man pages are to find out which command line options can be used to modify the behavior of different commands based on your particular needs. A lot of commands will actually have two versions of them a long hand and a shorthand version depending on your needs. Now let’s say I wanted to list all the directory entries in a certain directory.

I want to do that and include all the hidden files and everything else that’s there. How can I do that? Well, there’s a command called LS. Now, if I wanted to use LS with the Altion, that would show me all the files, but I can also use Lsal for all and that is another option. That’s what I mean by this shorthand and this longhand version. The longhand is really just more human readable and easier for you to know what the all does. It shows you all of the files, whereas the shorthand A is quicker to type.

But you may not remember what that A meant. Either of these can be used because they’re both going to do the same thing. The advantage of using longhand is it’s easier to remember what the command’s doing. But again, the ease of using the shorthand is that you’re going to get multiple options that can be chained together easily, and it’s a lot quicker to type out. Now, in addition to man pages, there is other documentation available in what’s called the Info format. This can be accessed by typing Info space and the name of the command at your command prompt. Info pages are like man pages, but they’re much more detailed. They’re divided into different nodes or pages that can be read with an Info reader, which works much like a web browser. You can use P to go backwards or previous. You can click N to go next and go forward when you’re inside of an Info page. If you press Q, this is going to exit the Info page and bring you back to the command line. There are other keys listed in the Info documentation as well, and you’ll get comfortable using these tools.