Join Scott Simpson for an in-depth discussion in this video Copy, move, and delete files and folders, part of Learning Linux Command Line.
- [Voiceover] It's pretty common to need to move, copy, and delete files at the Command Line. In fact, a lot of experienced Command Line users prefer the Command Line for file management because it can be a lot faster than dragging files around with the mouse on-screen. The first command I want to introduce to you here, is cp for copy. Let's make a duplicate copy of our poems.txt file. I'll write cp poems.txt, space, poems2.txt. The first file name is the file you want to copy, and the second file name is where you want to copy it to.
I'll press Enter, and then I'll take a look at the contents of this folder with LS. And I can see, there's my poems2.txt. You can also copy a file to a different path, for example we can copy our simple_data.txt file to our Employee Info subfolder inside our VHR folder. To do that, I'll write cp simple_data.txt, space, departments/hr/employee\ info/ Remember, we need to escape that space in employee info.
And if I list that folder, I can see that the file's been copied there. Let's take a look at moving a file rather than copying it. Move has two uses. Of course, you can move files between folders, but you can also use it to rename files. The command for move is mv, so I'll type that, and I'll move our poems2.txt file over to the departments/marketing folder. And I can check that the file is in that folder. Alright, ls departments/marketing/ there it is.
And then I can see that it is no longer in our original folder with ls. I mentioned that we can rename files with the mv command too, so let's do that. I'll type mv departments/marketing/poems2.txt space, departments/marketing/literature/txt and if I list that folder again, I can see that the file's been renamed. Earlier we saw the two dots that represented the parent folder. There's another shortcut you should know about, which is the single dot.
It represents the current folder. We can use that when working with files. I'll use the dot to move our literature file back from the marketing department folder, to the Exercise Files folder, which is the current working folder. I'll write mv departments/marketing/literature.txt space, and then period. Let's take a look at this folder, and there it is. I'll clear the screen. Of course, you can move, copy, and delete more than one thing at a time, and one of the ways we do that is with what are called Wildcards.
Wildcards are characters that stand for patterns. We'll see more about patterns in text later, so I want to introduce you to the two most common Wildcards here briefly. There's star, or asterix, and question mark. Star stands for any number of characters, and question mark stands for one character. Let's take a look at how these work with the move command. Let's say I want to move all of the .txt files to the marketing folder. Let's take a look at the files here, And I've got dupes, literature, poems, and simple_data.txt Instead of typing out all these names, I can write mv *.txt meaning anything followed by .txt to departments/marketing/ and then, listing the folder, I can see I have four files in there.
Let's move them back. I know that the only thing in the marketing folder are my text files, so let's use the Wildcard a little bit differently. If I don't specify a file extension, or any other part of a name, and only use * by itself, it represents all of the files in the given folder. I'll write mv departments/marketing/* space, and then period, to represent the current working folder. I'll list the folder, and I can see that my files are back. Finally, I want to show you how to delete, or remove files using the rm command.
First, let's get rid of this literature.txt file, since it's a duplicate of the poems file. To do that, I'll write rm literature.txt, and if I list my folder again, I can see that the literature file has been deleted. It's important to keep in mind that, unlike in most graphical file browsers, there's not a trashcan or recycle bin for deleted files here at the Command Line. That file, is gone. I've cleared the screen, and I've still haven't shown you how to use the question mark Wildcard. So let's make a few more copies of the poems file.
I'll write cp poems.txt to poems3.txt and the same to poems4.txt using the up arrow to bring back my previous command. I'll list the folder, and there they are. Now, we'll use that question mark Wildcard to delete them. I'll write rm poems?.txt. Of course, this doesn't mean delete poems? Like we're not quite sure, but it means delete files called poems with only one character afterward, which in our case, is poems3 and poems4.
I'll list the folder again, and see that we've deleted two files with just one command. I'll clear the screen again. rm has some options too, and one of them makes rm act recursively. Remember recursion? It goes into a structure of folders and walks through it, preforming an action on each folder before it moves up the tree. Let's try to delete the customer service folder structure from our departments folder. If I write rm departments/customerservice/ I get an error about the folder not being empty.
Of course, I could go in and use r+enter or rm on each folder, but it's a lot easier to delete them all at once, recursively. To do that, I'll write rm -r departments/customerservice/ and rm takes care of everything. I encourage you to continue exploring and practicing with these commands, but remember to be careful. There's no safety net for removing files. So keep your exploration inside the Exercise Files folder, or another safe place for now.
This course will establish the foundation for more advanced Linux topics. Find other Linux training courses here.
- What is the Linux command line?
- Writing Linux commands at the prompt
- Finding help for Linux commands
- Editing files and folders
- Configuring user roles and file permissions
- Using pipes to connect commands
- Peeking at files
- Searching and editing text
- Finding disk and system information
- Installing and updating software