How to Copy and Move Files in Linux: The cp and mv Commands

In this blog post, we will discuss Linux's `cp` and `mv` commands. These commands copy and move files, respectively. Both commands are easy to use and take two arguments: the source and destination.

How to Copy and Move Files in Linux: The cp and mv Commands

We will go over the basics of these commands and some more advanced options. Let's get started!

cp command

The cp command copies files. The syntax of the cp command is as follows:

cp source destination

where "source" = the file to copy, and "destination" = the location to copy the file. For example, if we tried to copy a file called "file.txt" from our current directory to a directory called "Documents," we would use the following command:

cp file.txt Documents/

Multiple files

If we wanted to copy multiple files at once, we could do so by separating them with a space:

cp file.txt Documents/ another_file.txt another_directory/

We can also use wildcards when copying files. For example, if we wanted to copy all files in our current directory that end with ".txt," we could use the following command:

cp *.txt Documents/

mv command

The mv command move and renames files. The syntax of the mv command is as follows:

mv source destination

where "source" = the file you want to move, and "destination" is the location where you want to move the file. For example, if we wanted to move a file called "file.txt" from our current directory to a directory called "Documents," we would use the following command:

mv file.txt Documents/ 

Multiple files

If we wanted to move multiple files at once, we could separate them with a space.

mv file.txt Documents/ another_file.txt another_directory/

We can also use wildcards when moving files. For example, if we wanted to move all files in our current directory that end with ".txt," we would use the following command:

mv *.txt Documents/

Renaming files with the mv command

The mv command can also rename files. To do this, you would use the same syntax as above, but with the destination being the file's new name. For example, if we wanted to rename "file.txt" to "new_file.txt," we would use the following command:

mv file.txt new_file.txt 

Advanced options

-r flag

You can also copy or move directories recursively using the -rflag with both cp and mv. This means that all of the files and subdirectories inside a directory will be copied or moved.

cp -r foo/ /destination/directory/

The entire foo directory would be copied to the destination directory, along with all the files and subdirectories inside.

i flag

Finally, you can use the -i flag with both cp and mv to prompt you before overwriting a file. This can be useful if you're not sure if a file already exists in the destination directory.

For example, let's say you have a file called foo.txt in your current directory, and you want to copy it to another directory called bar. But there's already a file named foo.txt in the bar directory. If you run the following command:

cp foo.txt bar/

The file in the current directory would overwrite the file in the bar directory without warning. But if you run the following command:

cp -i foo.txt bar/

You would be prompted before the file is overwritten.

Conclusion

cp and mv are essential commands for anyone who regularly uses Linux. Hopefully, this guide has helped you understand how they work and how to use them effectively. If you have any questions, feel free to leave a comment below.

Thanks for reading!)

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.