File Management in Linux
Red Hat Enterprise Linux (RHEL) is a rock solid commercial grade Linux operating system. If you're interested in learning RHEL from a system admins perspective then this course is for you!
The "Red Hat Enterprise Linux Technical Overview" course walks you through many of the basic system admin tasks and concepts required to administer RHEL effectively.
This course will provide you with insights to:
- Working with the Terminal
- Understanding the Kernel and User Spaces
- Graphical User Interface
- File management and the File System Hierarchy
- Editing Files using Vim
- Organizing Users and Groups
- File Permissions
- Managing Software
- Configuring Networking
- Controlling System Startup Processes
- Introduction to Containers
- Overview of Cockpit
Hey guys, right now we are going to have a look at File Management from the Command Line.
And pretty much everything is a file in Linux, we have normal files, we have directories, we have storage devices, we have links and so much more.
Now looking inside the current directory, I'm going to do a directory listing right now, and I've got a couple of files over here.
Everything is a file in Linux.
So you can see that there are some obvious colorization differences.
So looking at fileA over here or file1 over here and then compare it to file2 you can see that they are very different looking. Similarly you could see that compared to myprogram.1 and myprogram.2 they are very different and dirA and dirB are very different. And if you want to get more information about a file you could use ls -l and valuable clue over here when you do a long listing is the value of the first bit. So if it's a d, it tells me that it's a directory and you can see that even though it's called dirB, it's a normal file because when you have the value of the first bit over there's, it's just a dash ( - ). It means that it's a normal file.
So, you can see that file1 is indeed a normal file unlike file2 which is a directory.
Now moving on over here. We have my program1 in myprogram1 again it's a normal file and at the bottom over here myprogram2 is a link file. A link is a special kind of file that points to another file.
So I can access my program1 under two names, myprogram1 and also myprogram2 as you can see over here, points to my program1 other than that we also have block files, block files are special files that represent real physical storage devices.
So let me go and show you an example of a block file.
So I'm going to do a directory listing against dev/vda. Look at the value of the first bits over here, it says B this tells me again the type of file. Apart from that guys, we have the file command that could help you determine the file type.
So in Linux, extensions don't matter. So if you come across a file in your Linux File System that ends in .exe your system doesn't care about the fact that it ends in.exe.
The file content determines the file type. So to help us in that case we have the file command.
So I'm going to run the file command against the file1, you can see it's an ASCII text file, let's go and do it against file2, and over here it tells us that it is in fact a directory. Let's go and run it against myprogram.1 and you can see right now it's a shell script, it's an ASCII text executable and compared that to file2 over here, you can see, that it is a symbolic link to program.1.
Now some of them are basic file management commands are as follows just some of them and there are so many mkdir, you use this to create a directory.
So, if you're wondering how I created the directory dirA and file2 which we know now is the directory, you can make use of the mkdir command.
So I'm going to go and organize my work I'm going to create a Documents directory and you can see that I did make use of an uppercase D and that is because filenames in Linux are case sensitive. So if I try to type in CD against documents, right now you'll see that it won't be able to find it because it's not called documents with a lowercase d, and this is where I find that tab-completion really helps, because you don't have to go and type out every single character, and also tab completion for me allows me to avoid typos, to avoid spelling mistakes.
So what I could do is that one more directory that I'm going to create is Data with a capital D. Now if I type in cd to change directory and I'm going to use a capital D over here, and I hit tab nothing's going to happen, because, we don't have sufficient uniqueness. I'm going to make this more unique right now by putting in an o afterwards. Now I'm way too lazy to type out the remaining characters.
So all that I'm going to do at this stage is I'm going to work more efficiently.
I'm in a tab completed, because we have enough uniqueness.
So you can see that it has tab completed to the Documents directory. So let's go into that directory right now. So what I would like to do at this stage guys is that
I want to copy a file into this directory.
So, in Linux, we have the cp command.
So I'm going to say cp and now you have to specify what you want to copy and where you want to copy it to. So I'm going to copy a file called etc/fstab and I'm going to copy that to my current directory and again, I've got a . over here a period represents your current directory. So I am saying let's go and copy the fstab file that is inside of the etc directory and where we're going to copy it to is right here inside of this directory. So let's go and hit enter over there and now we can do a directory listing and you can see that I've got the fstab file copied.
If you wanted to delete a file you could make use of the rm command.
So simply rm and then the name of the file and there you go, it's deleted.
It's a non-interactive deletion, it's not going to prompt to you whether you are sure and there's also no recycle bin or trash bin for you to go and recover your files. So when something has been deleted, it's gone.
Let's go back to my home directory, the directory that's I'm originally placed in when I logged in is the home directory. So let's go and type in cd right now, and if you want to find out the directory that you're working in type in pwd which is for print working directory. So a directory listing one more is what we are going to be doing and you can see that we have the Data directory.
Now, let's go and use the rm command to delete that directory.
So I'll say rm Data and you can see over here that it says can't remove data it is a directory and we have another command called rmdir, which is used to delete directories.
However it can only be used if a directory is empty. So let's go and use rmdir against Data. And again we're going to tab complete this, and you can see transaction was successful, the job is done because in the absence of any kind of feedback assume that the command completed successfully. So this tells guys the Data directory has been deleted. Now we're going to try and do the same thing right now with the Documents directory. So rmdir, I'm going to try and delete Documents right now, and it says ok well that is fine because again it was empty.
If you have a directory with content and this can populate a directory right now.
So we're going to copy myprogram.1 and we're going to copy that into a directory called file2. Remember file2 is a directory.
So let's go and verify the contents of the file2 directory, we could see that myprogram.1 is inside of that. So let's go and use the rmdir command right now to go and delete file2. And you can see that it says can't do that. Why not? Because the directory is not empty.
So are you telling me that what I have to do is go into the directory clear out its contents and then go ahead and delete the directory well pretty much that's what it's telling you.
However you can make use of another command you could make use of the rm command that particular case. Now remember I told you that options influenced how the command is executed. Now in this case, we have a very common option that a lot of commands make use of, it's called r. r stands for recursive, which means that we're going to apply the instruction to an object and all of the objects below it. In other words we can apply that to a directory and all of the files below that directory. So let's go and try and delete file2 in this way.
So guys what's going to happen right now, is that it's going to delete file2.
However file2 has got contents, all of the contents are going to be deleted as well.
Now it's only fair that I show you how I created this link over here the special file, where the value of the first which is an l.
And that is the ln command, the ln command is used to create links,
So what I'm going to do is create a link that points to myprogram.1 and the link has been called my program3. So again just to speed things up and to avoid any typos
I made use of tab completion over there.
So if you think that I typed out every single character ohh you're surely mistaken.
So let's going to have a look at the contents of that directory and you can see that in this case I created a link. Now, how I know it's a link is, because I know that the link count has been increased, because we have two types of links, the link that I just created right now is called a hard link the other kind of link that I could create is a symbolic link, also commonly known as a soft link.
Now the cool thing about these symbolic links is that they could point to files on other file systems, they can also point to directories. And very often we could use that just, to sort of truncate the path, to make it a lot easier to access certain files and certain directories.
Instead of having these long paths. So let's go and create a symbolic link right now and we're going to say ln -s and we are going to be creating a symbolic link that points to myprogram.1 and it's going to be called my program3 or my program.4 rather. So now when we do a directory listing you can see that it does indeed myprogram forward, does indeed have an l as the value of the first bit as opposed to my program.3. Now both are links and I know how to tell the difference.
Let me just go and give you a bit of a clue over there.
Yeah, that number as opposed to the one at the bottom which is myprogram.4 which is a symbolic link and again you can see the value of the first bit.
And there's I mean there's a strong visual indicator over here that myprogram.4 points to myprogram.1 as opposed to myprogram.3. No strong visual indicator there.
So guys there are lots of commands that I could show you right now, as to how we would go about doing File Management. It's way beyond the scope of this particular video.
However we have lots of learning material available for you.
So, I would encourage you to explore just that.
Jeremy is a Content Lead Architect and DevOps SME here at Cloud Academy where he specializes in developing DevOps technical training documentation.
He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 25+ years. In recent times, Jeremy has been focused on DevOps, Cloud (AWS, GCP, Azure), Security, Kubernetes, and Machine Learning.
Jeremy holds professional certifications for AWS, GCP, and Kubernetes (CKA, CKAD, CKS).