Working with files is an important task when you are using the command line. You might have to copy or move files or folders, rename them or maybe even delete some files and folders that you no longer need. Thankfully, the bash command line offers the basic functionality to work with files and folders built into the shell.
Moving files with the mv command
You can move files using the command line with the mv command. The mv command has the following syntax:
mv [options] <source> <destination>
As you can see, you can move files and add options for more control. If we want to move a file from the Downloads directory to the Documents directory, we could use the following command:
mv ~/Downloads/myfile.pdf ~/Documents
If we run the command from within the Downloads directory, we can even skip the first part of the command and just run:
mv myfile.pdf ~/Documents
We can also rename a file with the mv command. For example, if we have the myfile.pdf in the current directory, we can rename it like this:
mv myfile.pdf cheatsheet.pdf
In the same way, we can move several files, for example, an entire folder. If we want to move a folder named books from Downloads into our Documents folder, we could run:
mv ~/Downloads/books/ ~/Documents/
This would move the entire folder named books with all its contents from Downloads to its new directory. Of course, you could also change its name, if needed.
Copying files with the cp command
The cp command is very similar to the above-mentioned mv command. The one big major difference is that cp copies a file or a directory, while the mv command moves them. It means that you will eventually end up with two instances of a file or a directory. For example, if you run something like this:
cp ~/Downloads/myfile.pdf ~/Documents/myfile.pdf
You would create a new copy of the file myfile.pdf in your Documents directory. The original file in the Downloads directory would still be there untouched. This comes in pretty handy if you want to make changes to a file, while still keeping the original intact. Like with the mv command, you can also give a new name to the copy:
cp myfile.pdf myfile.old.pdf
The command would create a copy of the file myfile.pdf, named myfile.old.pdf in the same directory. You can then make changes to the original file without losing the original backup if needed. Moreover, you can copy whole folders using the -r or --recursive option:
cp -r ~/Downloads/books/ ~/Documents/books
The original folder and its content, located in the Downloads directory, will remain untouched.
Deleting files using the rm command
Using the bash command line, you can also delete files, which are no longer needed. To do so, you can use the rm command. For example, if you don't need the file myfile.pdf anymore, you can do the following:
rm myfile.pdf
The file will be deleted from your computer right away. As with the cp command, you can also add a flag to delete folders and files recursively:
rm -r ~/Downloads/books
The above command will delete the folder books and all its contents from your computer. If you try to apply the rm command on a folder without the recursive flag, you will get an error. Instead of rm you could also use rmdir to remove a directory:
rmdir ~/Downloads/books
The difference between the rm -r and rmdir commands is that the rmdir can only delete empty directories, so if you want to delete the folder with all its contents use the rm -r. Another option that is commonly used with rm is the -f flag to force deletion. With the force flag active no checks are run and no confirmation will be given.
rm command may cause harm to the work of your operating system if you delete system files with it. There is no trash bin, so there's no way to recover any files deleted by this command.
Pros and cons of using the command line to copy and delete files
There are some pros and cons to using the command line to work with files. The pros are that you can copy, move, and remove any files in the command line and create scripts for repetitive actions. This is pretty useful, especially when working in a server environment with multiple machines or users.
The downside is that there is no confirmation for moving, copying, or deleting files, unless you set it specifically in the options before running the command. That means that once the command is run, it is executed and there is nothing you can do to stop it. Regarding the rm and rmdir commands, a huge downside is that there is no trash bin where the files will be placed temporarily. Whatever you remove this way is gone forever.
Conclusion
With this, you are at the end of our introduction for mv, cp and rm. You've learned the following:
- You know how to copy and move files and directories.
- You know how to change the names of files and directories while moving or copying them.
- You know the difference between copying and moving files.
- You have learned how to delete files and directories with
rmandrmdir.