LPI 102-500 – 103.3: Basic file management
- cd, cp und mv
This topic might even have been at the beginning because it is about how to move around the file system and how to copy move list files and so on. So let’s first look at the CD command. CD stands for change directory. During the last videos of the I have already used CD very often. But to repeat with CD I can move back and forth in the entire directory tree with the option of specifying absolute and relative paths. Let’s see which directory we are in now with PVD print working directory and we are in my personal home drive. The download subfolder is also located in this directory. If I want to change to this directory using the absolute path then I start with the lowest point of the directory tree. So with root up to the downloads directory I would do then CD home manual downloads. Now I am in the corresponding downloads folder with CD period I go one step back in the directory tree. So I am here again in home manual. If I want to switch relatively to the downloads folder then I simply start from my current location and not from the lowest point. So in that case I will just go on with CD downloads because I am now in home manual. So here in this part of the directory tree and I only have to switch to downloads so CD downloads and now I am in the downloads folder.
Note that if you actually want to choose the relative path so CD downloads but make a mistake and choose let me show that go back and choose Slash Downloads. You see that will not work because the system assumes that you are currently starting with roof root here. So the system thinks you are starting with an absolute path and there is no downloads folder in the root file system. Accordingly it also says file not found no such file or directory. Sorry just to show the function you can of course if you for example want to change to folder bin you would like to get there in this way you can express that with the absolute path with CD bin or let me go back to the downloads folder. If you want to use the relative path then you have to use CD. So one step back to manual, then another step back to home and another step back to root and then you can choose bin and now we are located in the bin folder. That is also possible, but I think just CD bin it is a lot easier in this way. With relative and absolute path information we can also copy or move files or directories.
Let’s take a look at the copy command first. Man CP for copy Cpcopy files and directories with Q we can leave the main page. You would use the CP command with CP then corresponding option if there is one. And here the important thing is source and destination so source and target I might switch to the Downloads folder again. Now I’m back in my downloads folder. By the way, the tilt here is an abbrevation and with this tilt I get to my main directory so home manual so CD home manual is the same like CD till it’s absolutely the same, it’s only an abrovation. Okay, so let me switch back to the Downloads folder and we still have the Skype for Linux file from one of the last lessons here we now simply select the copy command specify the source. The source is Skype for Linux and of course we do not have to specify a path since we are now in the same directory as the file. It is sufficient if we simply specify the file so CP and then Skype for Linux 64 DB alternatively you can use CP skypefolinox means that means this is the current directory we are in. We want to copy it to temp. So we add the following here temp sorry TFP so that’s correct.
So the copy command then the source and the target. The source is this file here. We want to copy it to this target to TMP. The file should now be copied so we switch to the temp folder and then I display the content and we see here that the copying worked. Let’s go back to our download folder. For the test I will take part CD minus. The minus is an abbrevation and means that you switch to the last folder you were in before and we switched from the Downloads folder directly to the temp folder. So with CD minus I can go back to the last folder and that was the downloads folder in my home directory. If we now want to copy this file back we could theoretically proceed as follows CP then the source it’s TMP skype for Linux and then the destination, the target and it would be home manual Downloads since we are already in the Home Manual Downloads folder.
So you see here we are already in. Of course we do not have to write out this path but simply use a period instead of the path. So in this way period, this period always indicates the current directory in which we are the command says copy the file skype for Linux 64 d EB which is located in the temp folder into the folder we are currently in and that is the Downloads folder. If you do that now you can see that the file was copied without an error message or confirmation, although the file already existed. But we can see here the new timestamp. It’s the 15th December, it is 10:29 p. m. And you can see it here at the top.
So it’s the right timestamp and you can see that the old Skype for Linux 64 was overwritten. That means we have to be very careful when we copy something because the file will be without query over it of course this can be potentially dangerous if we accidentally override the wrong file. However, we can set it up with a certain option so that we are asked whether we really want to overwritify. This would work now with the I option so CP and the option I TMP Scott for Linux and the period again the I option stands for interactive and means that we are immediately asked if you really want to copy the file override yes or no? And in that case, I will say no. And we will continue to write the time. We have the timestamp at the same time as before and not the actual timestamp.
Another interesting or important option is the option p. This means that the timestamp of the file is taken over. So when we copy a file, the new file always gets the current timestamp and not a new timestamp. With the option p, the timestamp of the source file is taken over. So we would basically copy the file again and test it. So let’s do it CPP and the source is temp skype for Linux. We copy it to the current directory and you see here the time timestamp is 09:26 p. m. . So it should be the same time stamp as the Skype fully looks in the Temp folder. And you see here it’s the same timestamp with the CP command. We can of course also copy complete directories for this we have to use the r option. The R stands for Recursive. We copy for example, the complete Temp folder in our home drive. We will do that with I would say we will copy that into my downloads folder. So we use CP with the option r and use the folder temp. I have forgotten the period.
We have some permission problems here, but that’s no problem. Let’s take a look. We have here the Temp folder and we see that we have some files that were copied and we have some directories that were copied. So it is only possible with the r option. Without the RPT option it would not be possible to copy the whole folder. Maybe we could try that out. Let me create a directory with the name test and now we try it again with CP and then only TFP period. And here we get the hint r not specified omitting directory temp so nothing happened here when we are not using the r option, the move or the move command or MV for short works in the same way. Let’s take a look at the man page man MV, move or rename files. What is the difference to the copy command? When I copy the file the skype file from the Temp directory into my home directory then I have two files called Skype for Linux 64 d EB, one in the Temp folder and one in my home directory.
With the move command the file is cut and pasted or copied. And after the copying process the source file is deleted. So after a move we only have one Skype file, namely hours on the drive as we were moved it, we can move the Skype for Linux 64 d EB from the Temp folder to our download folder. We will do that with MV TMP skype for Linux and here period. Okay, let me move it into my test folder, period. And here we see that the Skype for Linux 64 DB package was moved successfully. So let’s take a look at the Temp folder. And now we see the file is no longer here. So the system copied the file and deleted the file in the source after the copying process. And that is basically the move command. Of course you can also move a whole folder with move and not just individual files. And in contrast to copy, you don’t even have to specify an option.
So it is enough if we choose now maybe the following command let me get back to my test folder and then with pseudo MV temp and we have now the Temp folder here. So if we now look at the root folder, we see that we no longer have a Temp folder here, so that the system is not damaged. We move the folder back quickly because the system also needs the Temp folder.
And let me use the following command pseudo ambry temp folder to root now you see the Temp folder is not here anymore. We switch to root and here we have the Temp folder again as stated in the main page, MV is also used to rename files. Let me go back to my home folder, the test folder and maybe let me create a file with the name file one. We have here the file file and we want to rename the file. We just take MV file one. File one is the source and we want to rename it to file one new. That’s the destination. And we see now we have the new name file and the old name five one doesn’t exist anymore.
- touch, file und ls
In the last video we learned how to move back and forth in the file system and how to copy and move files. Let’s start in this chapter with how to create a file in the first place. There are several ways to do this, but the only important one in this chapter is the file creation with a touch command. Let’s take a quick look at the man page man touch touch change file timestamps so Touch is not intended to create files. The main task is to change the timestamp of a file. Creating a file is easy. We just enter the following command touch file and the file named file has been created under Linux. We don’t have to assign a file extension, but if you want we can do that. And it doesn’t matter what the file extension is. We could use for example, dutch file 40 711 and we see the file has now been created. Of course the two files have no content because we didn’t enter any. I would say let’s fill one of the two files with some text. I use the VI editor for this. We will discuss exactly how it works in a later video. And I will change the file 40 711 and just add a little text here, little text and I save the file. And now we see the size of the first file is zero bytes so nothing is in it.
And here, file 40 711 is 23 bytes. Because we have just added some text with the command file, we are able to find out what kind of file we are dealing with. As I said, we can use any file extension and of course we do not know if the file is not from us whether it is a text file, a binary file or something else. Let’s take a look at the man page. Man file file determine File Type it is sufficient here to find out what kind of file it is. With File, we apply file to the file file. Maybe I choose a bad file title here, but I think you understand it. So File is the command and the second file is the file with the name file. And as an indication we get that the file is empty. Then we look again at the other file with file file 40 711 and here we get the message that this file contains ASCII text. So it is a text file. You can’t actually do more with File. But as I said, there are a few options that you can of course look at to see if there is anything else important. I’ve never used an option personally, privately or professionally. But of course that doesn’t mean that there won’t be a question in the exam. But I wouldn’t expect more detailed questions about file. So let’s get back to the touch command.
We have only used it now to create empty files. The actual function of touch, however, is to change timestamps. Let’s look at the timestamps of our files. Maybe I clear my screen first and then we see this file. The first file has the timestamp 21 36 it’s a German time. In English time it should be 09:36 p. m. And here with the timestamp 21 37 which means that we created the file at this time or that we last edited at this time. If we now simply touch the file, the timestamp changes in principle we don’t do anything with it and just touch the file once. So let me show that touch file and now we see the timestamp of fire has changed to 21 41 the timestamp also changes when we edit a file, but not when we just open it or check it out.
So if I say now cat file 40 711 then of course the timestamp doesn’t change when I just open the fire and I leave it again without editing anything. I’ve made a mistake, I’ve made a little mistake because I haven’t changed anything, I haven’t add anything but before I left the VI with the open file I told VI to save the file. So because of that we have a new timestamp here if I open the file with VI and leave Bi without saving this file then the timestamp will not change. So as you can see here at the top we now have 21 43 so let’s show that again with Bi fire 40 711 so now I will leave the file without storing anything and here you can see the timestamp did not change with touch you can also store any time format for this file which you want. For this we use the T option and the syntax then is as follows touch t so what does that mean? The touch command we are using the T option so we want to use an own timestamp. We choose here the year of the timestamp 2019 we choose the month, the day and the time 1234 and we have a period and then we have the seconds.
Now when you look at the file here, then you see the file is now on the timestamp on April the first at the year 2019. I think that’s all you need to know for the exam with the command touch. Perhaps one or the other has already noticed that I often use the ll command to look at the contents of a directory. Basically Ll is not a Linux command, it is just an alias in principle and abbreviation. Usually you use the correct Linux list command namely LS, which offers various options. You should already know the two best known and most popular ones from the last few videos, namely A and L. So for example LSA also shows the hidden files and that may be a bad example because there is no hidden file here. So let me go back to my home directory so LS and the option A and here you can see the hidden files. They are starting with a period. These are hidden files.
And here these are hidden directories. Again with a period at the beginning and without the A option you cannot see these directories and these files with the option L. You can see a detailed and organized view of your files and folders. You see here are the permissions, the owner, the group owner, the personal owner, the bytes, the timestamp and the name of the file file or the directory. I like the option H means human readable and makes it easier for us to read the most very long file sizes in bytes. Let me check. This file is a little bit bigger, the Lorem Ipsum file. So I could list this with LSL and then Lauren Ipsum. And then I see here the detailed information of Lauren Ipsum and here is the file files. Now I could use lush Lauren Ipsum. And now I see it is 111 KB.
So if you have some files with several hundreds of megabytes or gigabytes then the age option is very nice to have. By the way, the age option doesn’t make sense if you use it without the L option. So just LS H and then Lauren Epson and then you see there is no file size here because the L option is responsible for this data here. And without the L option you don’t get this data. If this is not yet clear. You can of course use two or three options. So LSL for example but you don’t always have to put minus in between. You can simply write them all out together with Lesly for example. But you don’t I’ll always have to put the minus. So I’ll switch back to my home directory with the Tilde and with LSR you can also look at the contents of the directory recursively.
So if we have subdirectories, their contents are also displayed here. You can see that here. Go a little bit up here. In my Downloads folder there is this file and this subdirectory. Here I have Downloads temp folder with these sub files and sub directories and so on. You can also use LS to display the contents of the directory sorters by the last processing time. So that the last process file, for example, always stands on top. We will do that with LSLT and we can see at the top that the test directory is the newest one. It is from today, the test Deer is from yesterday and so on. If you for example want to output the content of a lock directory and you are looking for the file that was last written, then you would have to scroll up again after this command to find the corresponding file.
It may well be that there are a few hundred files in such a lock folder. But if we display this ordered view the other way around, that would be the order LS Ltr for reverse R stands for reverse in this case, so please keep that in mind. The small R is for reverse. The capital R is for the recourse of view, which also shows the contents of the subfolder. So here we can now see that what was last edited is at the bottom. At this time, the Ll that I always use is nothing more than an abbreviation for LSLA or Ll. It’s the same. It looks exactly the same. It’s just an abbreviation form. You can also set up your own aliases. That means I can also say that I want that to be called Le or Lle and that certain options are linked to it. However, aliases will only be dealt with in the second test. So we will not go into it further here. But of course, in the regarding part of the second exam preparation.
- mkdir, rmdir, rm, find
In this lesson we will talk about directories first before going back to files. How can we create a directory? This command is called mkdir, and that is the short form of make directory. We take a look at the main page mkdir make directory directories create the directory or the directories if they do not already exist. So let’s just create a folder called test mkdir test. We go straight to the folder we just created. So we’re now here in Home Manual test. And of course nothing is in it here, because we have just created it and copied nothing in here. So let’s create a few more subfolders. For example with MKD or test one. Test two, test three, test four. So as you can see, we can create several directories at the same time. Of course, we can also create directories and corresponding sub directories at the same time. For this we have to use the option p, which would then for example, proceed as follows with the option p and then let’s create test five. Test six. Test seven. And we see we now have the fold test five here. And we go in and have test six here. And then we have the test seven folder. So we can create several folders and subfolders at the same time. L
et’s go, for example in the folder test two and just create a file here. Let’s maybe name it touch test file. Now we have a test file here. And we are now leaving the folder again. And we now simply delete two folders. The corresponding command is called Rmdr, which stands for remove directory. Let’s look at the man page rmdr remove empty. And that is important. Remove empty directories remove the directory or directories if they are empty. So if we say now rmdr test three, the command was executed without any problems. The folder was deleted. You can see here test three is deleted here you can see test three. Now it’s deleted. And what is if we want to delete the test two folder RMD test two and we get the error message rmdr failed to remove test II directory not empty. That is because we just created the test file in the folder. And as we talked about a few seconds ago, saw it in the man page. Rmdr only removes directories which are empty. But test two is not empty. So Rmdr does not work. This means that as soon as the file is in the folder, we cannot delete the directory. With Rmdr.
This also applies if there is no file. But there are other subfolders. We can test this also with rmdr test five. And here RMD rmdr could not remove test five. Directory is not empty. So there are no files here, but the two other subfolders that we have just created, test six and test seven. As long as the corresponding subfolders are empty, which is what they are here, we can still get RMD to delete this test five folder, and this works with a P option. So rmdr p, test five, test six, test seven. Here we also have to specify all the relevant subfolders and we see that test five is missing and has accordingly been deleted, including its subfolders. How do we get the other folder that contains a file deleted? We have to delete the file first. We cannot use RMD for this because it only deletes directories and not files. Instead we use the RM command, which stands for remove. It’s a bit more extensive than rmdir and you have a few more options. So we change again to the directory with the file test two, and we might create a few more files. Touch file 253545.
So we have a few fights here now, and if we say no RM test file, then the test file has been deleted without confirmation. When we say RM with the option i, file two, I stands for interactive. Then we are asked again whether we are sure that phi two should be removed. In that case I will press no and for no and option I can therefore be a good lifeline. Another important option is V. And as you have already seen with other commands, v often stands for verbose, which means that we get a detailed output of the command. So for example, RMV was removed. Another option is the option F or force, and that means something like the file will be deleted and no questions will be asked under any circumstances. So RMF was removed, RM can also delete directories. The R option for recoursive is often used here. So to say everything that is in this folder, all files and subfolders are also deleted.
When you are using the R option, absolute caution is required with this command because, as we have seen, no questions are asked, but what is said is simply done. The option R is often used with a combination F, because if you delete a folder containing 300 files, you do not want to be asked in between whether you really want to delete one, phi three and 5300. If you make a mistake here with RM and the option R or RM and the option R and F, you can actually delete and paralyze almost the entire system. So if I type now rmrfroot, this command would result in everything being deleted from the root directory on. That means the system would be completely unusable. Luckily, the new Linux systems have some security settings that this command is not allowed. So I think if I would do this now, then nothing would be happened.
But in older versions that you could use, maybe you could delete maybe your entire system. So I don’t want to try it now. And of course we don’t run this command. Instead we just delete the directory in which the appropriate files are located. So we have to remember we have some files here in test two. Only two files left. And now we can delete this folder with RM and the option RF and test two. The test two folder is now deleted and as you have seen, no questions were asked. So we have to be very careful when we are using the RMRF command, especially in connection with RMRF. I recommend getting used the following notation rmrfperiod and then the folder name test one. For example, the period followed by the slash indicates that it only deletes the directory here. Test one in the directory in which we are currently located. So it’s an extra security. As long as you are not constantly somewhere in root or Etsy or VAR or something like that, you can avert even the greatest catastrophes. We see. Test one is deleted and yeah, that’s it. At the end of the video let’s look at the Find command. Clear the screen. Let me take a look at the man page. Man find search for files in a directory hierarchy you can use Find to search for files. There are many users here use cases here you can only search for certain file types for certain file names, files with certain sizes and so on. Usually Find is used with the name option and searches for appropriate names.
The syntax is as follows find the option name and the regarding file name maybe file 40 711 and of course no result is displayed now because this file does not exist on my system. That’s why I just created and execute the command again file option name and the name file 40 711 sorry find of course not file and the file has been found. And as I just said, period. This file is in the folder in which we are currently on our way. We can of course query this with PVD. In that case it is home manual test. The Find command is often used with the asterisk wild card or star wild card. For example with findname lock. That means that it outputs all files with the file extension lock. I know a single file was found here. Why is that? Find name is in this case only searches in the directory we are currently in and we are currently in home Monroe test and there is simply no look file here. So we have to specify that Find should look elsewhere, for example globally or in a specific folder
We can do this as follows pseudo find root VAR for example name block we now also select pseudo here because we are in different directories than Home and need appropriate route permissions for and we can see that of course a lot of log files were output. Often there are folders that still have dozens of sub, sub and subfolders. And to limit the search for find here you can use the max depth option so that for example, only in the subfolders three floors below, for example. So accordingly pseudo point we are searching in root and below. And we are using the option in max depth four. And we are looking for files with the name or with the and lock and we see that we have a maximum depth of four. So one directory, the second, the third and on the fourth place and there is the file. If I would use now five maybe it’s a bad example. There are no other, no more. Let me just show you with two speed up and you’ll see 123-12-3123 and so on. So there are of course countless other possibilities and combinations of how you can use Find. Take a look at the main page and try a bit. But I don’t think the exam asks for anything exotic. Rather you should know how to use Find, how to search in appropriate folders and how how to name or specify files.
- dd
Let’s look at the next command on the list. That would be DD. DD stands for disk dump and is used to copy entire hard disk petitions or just files bit by bit. The syntax looks a little bit different from other syntaxes. What does that mean? In this case, EF stands for input file, and the first petition of the SDA hard disk is used as the source of stands for output file. And the first petition of the SDB hard disk is used as the target. So if I were to send that now and would accordingly have SDB hard disk, then it would create an exact copy of the petition on this second petition. Unfortunately, I don’t have the option of cloning entire hard disk drives or petitions. But the whole thing also works at file level, and we can now test that out. We only have one file here with the name file 40 711. And we want to copy that bit by bit to another file name. Maybe file 40 712. Let me try it here. We have then copied a short summary. Zero plus one records in, zero plus one records out.
And the important thing here is that you know that a file is being copied from left to the right. So let me check if we have a new file. Yes, can see it here. File 40 712. Look at the timestamp 1640. Now it is 1641, so it’s the correct timestamp the file was copied successfully. Of course, DD has a lot of options, so I consider the options BS and Count to be important. BS is the short form of bytes. With the BS option, we can tell DD how big the blocks should be that it reads and writes. If a block is only 16 bytes in size, then of course it takes much longer than if a block is 1 MB in size. The count option defines how many blocks are to be read and written in total. Of course, these options don’t make any sense at all if you are copying an entire partition or hard drive.
But there are scenarios in which it makes sense. DD is, for example, often used to back up the bootloader, including the petition table. What would the command look like? So let me show that. What does this command mean? The first hard disk petition is the source. So the source input file SDA one. The destination is a new file with the name backup bootloader, which will then be created. DD is instructed to read and write 512 bytes, blocks and count equals one says that only a single block will be read and written. Of course, this is the first block in this case. And since the boot loader, including petition tables, occupy the first 512 bytes of a hard drive, the byte size of 512 naturally makes sense. And Count One ensures that only the first 512 bytes are read and written.
So, to repeat, we know the first 512 bytes of the first hard disk is the bootloader, including partition table. And accordingly, we have a great backup of our bootloader. And we see a data record in, a data record out, we have 512 bytes copied. And here we have now also saved the file named backup bootloader, in which our bootloader, including the petition table, is actually saved. And you can see here, 512 in size.