Linux Administration Bootcamp
Installing and Connecting to a Linux System
Intermediate Linux Skills
Linux Boot Process and System Logging
Disk Management in Linux
LVM - The Logical Volume Manager
User Management in Linux
Networking With Linux
Shell Scripting with Linux
Command Line Kung Fu
In this course, you will learn how to install a Linux system and connect to it, whether that be on Mac or Windows.
This lesson will cover how to delete, copy, move, rename and compress files. Eventually, you'll get tired of all the files you have laying around and want to delete some of them. To do that, use the RM command. RM space file name is the simplest form of this command. If you want to remove a directory and its contents recursively, use RM space dash R, and the name of the directory. If you want to force removal, use RM dash F.
Search patterns can be used to delete multiple files at once. It's a good idea to double check what you're going to delete with the LS command before you actually run an RM command against it. In this example, you can see that rm dot star will not move dot and dot dot, which represent this directory and the parent directory.
To copy files, use the CP command. If you want to create a copy of a file, you just run CP source file destination file. You can also copy a file or a series of files to a directory by using CP and then listing the file or files and ending the line with a directory name. To run CP in interactive mode, use CP space dash I. If the destination file exists CP will prompt you before it overwrites the file. If you use CP space dash R source directory and destination, CP will recursively copy source directory into the destination directory.
If the destination directory doesn't exist it will create the destination directory with the contents of the source directory. Let's copy a file with a CP command we'll copy file one to file two. And you can see that there are no differences between the files. Let's create a directory. Let's copy these files into that directory. So we can list as many files as we want. And when we end the command with a directory it will copy all those files that we listed into the directory. And see that the files are in the directory.
If you use CP with a dash I, it's interactive mode, and it'll prompt you if the file exists, the destination file exists. Do we want to overwrite file two? Let's do a recursive copy on DIR to DIR two. DIR two doesn't exist. So CP creates DIR two and places the contents of DIR into DIR two. If we try to copy DIR into DIR three, DIR three does not exist. We'll see what happens. And we see that copies skips directories. Copy will only work on directories if you give it a dash R for recursive. So let's create DIR three.
Let's recursively copy DIR and DIR two into DIR three. And you can see that the two directories are in DIR three. To move or rename files, use the MV command. If you were to run move, space source, space destination, move will move the source into the destination. Otherwise source will be renamed destination like CP, MV has an interactive mode.
So if you were to run MV dash I source destination and destination existed MV will ask you if you want to override the destination. Let's use the move command to rename a directory. You can see that the IR has been renamed to first DIR. Let's do the same thing with a file, rename file one to file one dot renamed. And let's look at how move will handle moving a file into a directory. File one dot renamed into first DIR. And you can see that the file is now in first DIR. It's contents file one. This is the contents of file two.
If we run, move file one, file two. That will rename file one to file two. It will overwrite file two. You can see file one is gone because it's been renamed and overwrote to file two. If you want to do this in interactive mode, use dash I And in this case, it says, Hey, there's a file that already exists. Do you want override it or not? You've already used the sort command a couple of times in this course, but as a reminder, the sort command at its most basic level sorts, text alphabetically that appears in a file. You can use sort with a dash K option and supply a field. So you can sort by a different field than the first field.
Dash R sorts in reverse order and dash U removes duplicate lines and only provides unique results. Let's look at the contents of more secrets. If we sort this file, it will sort on the first column. You can see that P comes before S which comes before T et cetera. You'll also notice that there are two duplicate lines. So to remove those, we can run sort with a dash U for unique, and we see that we only get one tags line.
If we want to reverse the order, dash R. And if we want to sort by the second column we can use dash K two for the second field and we can even combine options use the dash U for unique. Now you can see the second field A comes before B before C and before F If you want to bundle a group of files or directories together in an archive, use the tar command.
You may want to create a copy or backup of a group of files. You may also have several files that you want to transfer at once or transfer as a set. In these situations tar can help. You'll notice that tar does not require a hyphen to proceed it's arguments. The hyphen or the dash is in brackets. Traditionally, the hyphen is excluded but tar still works with it. So if you see tar space, CF file dot tar it's the same as tar space dash CF file dot tar.
Some of the most commonly used tar options include C to create a tar archive, X to extract files from the archive, T to display a table of contents, V to be verbose, Z to use compression and F to specify the file that you'll be working with. Let's create a tar archive of TPS reports directory. So tar C for create F for the file we want to use. We'll use TPS dot tar, and we'll supply the directory that we're going to archive. Can use T for list and it shows the directory and two files.
So let's move into the tmp directory and extract the archive extract F for the file. We'll give it the file name, and you can see that it extracted the contents into TPS reports. If you want to get a verbose listing of a tars doing you can use a V option so we could have ran tar XVF home Jason TPS dot tar, and it will list the file set it's extracting.
To compress files and save space, you can use the gzip command. To uncompress the file you use gunzip. And if you want to view the contents of a zip archive you can use gzcat or zcat. You can use the DU command to display how much disc space is used by a file. So DU dash K display sizes in kilobytes, DU dash H display sizes in a human readable format. So if the file is five megabytes, it will say five M or 3.7 gigabytes, 3.7 G.
Let's use the DU command to see how much space that this data file is using. It's using 80 K. You can compress it with gzip When you compresses a file with gzip, it compresses it and adds the dot GZ extension to the file name. Let's see if that saved us any space. And it did 4k 80 uncompressed, 76 K compressed. And can uncompress it with Gunzip. We can see it's back to its original state. If you want to create a tar archive that is compressed you can use the Z option for compression.
We'll do C for create, F will name the file, TPS dot TGZ. This is a naming convention. You don't have to name files this way. Sometimes you'll see it TPS dot tar dot GZ, but TGZ is short for a tar that's been compressed with gzip. We'll compress the TPS reports directory. And if you want to work on a compressed file, you need to use Z. And it lists the files that are in that archive.
In this lesson, we covered the RM command to delete files, CP command to copy files, the MV command to move and rename files and directories. Gzip to compress and tar to create archives.
QA: A world-leading tech and digital skills organisation
We help many of the world’s leading companies to build their tech and digital capabilities via our range of world class training courses, reskilling bootcamps, work-based learning programmes and Apprenticeships. We also create bespoke solutions, blending elements to meet specific client needs.