Home Terminal Tuts How to copy files and directories in Linux

How to copy files and directories in Linux

We'll cover the essential commands, parameters, and provide examples for better comprehension. So, are you ready to boost your Linux file management skills?

by Divya Kiran Kumar
copy files and directories in linux

It’s always exciting for me to share Linux command line tips. If you’re just getting started with Linux or looking to expand your command-line proficiency, one fundamental skill you must master is copying files and directories. So today, let’s take a deep dive into this topic and unravel the mystery of how Linux moves data around. By the end of this tutorial, I am confident you would be more comfortable working with the command line interface (CLI).

Getting started: The basic commands

The first thing you would need to understand is the basic command used for copying files and directories. The Linux command line provides two significant commands for this purpose, cp and rsync.

The cp command

cp is a short form for “copy”. This command copies files and directories from one location to another. The syntax for the cp command is:

cp [options] source destination

The source is the file or directory that you wish to copy, and the destination is the location where you want to copy it.

Let’s try copying a simple file, say ‘file1.txt’ in our home directory to a new location in a directory called ‘Documents’.

copying a file from home directory to documents using cp command

Copying a file from Home directory to Documents using cp command

cp ~/file1.txt ~/Documents/

In the above command, the tilde (~) represents our home directory. Therefore, ~/file1.txt refers to the ‘file1.txt’ file in our home directory, and ~/Documents/ is the ‘Documents’ directory in our home directory. After running this command, ‘file1.txt’ would be copied to the ‘Documents’ directory.

The rsync command

While cp is quite useful for basic copy operations, rsync (remote sync) is more robust and offers more options. For instance, it is more efficient when copying large files or directories, especially over a network, since it only transfers the changes made rather than copying the entire file again.

The syntax for the rsync command is similar to cp:

rsync [options] source destination

To copy ‘file1.txt’ to the ‘Documents’ directory using rsync, we would run:

rsync ~/file1.txt ~/Documents/
copying a file from home directory to documents using rsync command

Copying a file from Home directory to Documents using rsync command

Working with directories

Working with directories is a bit more complex, but the principles are the same. You can copy directories using both the cp and rsync commands.

Using cp to copy directories

To copy directories, you need to use the -R or -r (recursive) option with the cp command, which tells cp to copy all the files and sub-directories within the specified directory.

Let’s say we have a directory named ‘Backups’ in our home directory and we want to copy it to ‘Documents’. The command would be:

cp -R ~/Backups/ ~/Documents/
copying a directory from home to documents using cp command

Copying a directory from Home to Documents using cp command

Using rsync to copy directories

The rsync command is similar, but you would use the -a (archive) option, which tells rsync to maintain the same file attributes and to copy directories recursively.

rsync -a ~/Backups/ ~/Documents/

Troubleshooting Tips

Despite Linux’s power and flexibility, errors can occur. Here are some common issues you may encounter when copying files and directories and ways to troubleshoot them:

Permission Denied Error
One of the most common issues you might encounter is the ‘Permission Denied’ error. This happens when you don’t have enough privileges to access or modify the file or directory.

To solve this issue, use the sudo command to run your commands with superuser privileges. But remember, use sudo judiciously as it gives you the power to modify system-critical files and directories.

No Such File or Directory Error
If you mistype the path or if the file or directory does not exist, you would get a ‘No such file or directory’ error. Always double-check your paths and file names. You can use the ls command to list the files and directories in your current location.

Rsync is not Installed
Another possible reason for your command not working could be that rsync isn’t installed on your system. You can check whether rsync is installed by using the which command:

which rsync

If rsync is installed, the output will show the path to the rsync executable. If it’s not installed, there will be no output. In this case, you can install rsync using the package manager for your Linux distribution. For example, on Ubuntu, you would use apt:

sudo apt update
sudo apt install rsync

Please ensure to replace the package manager command with the one suitable for your Linux distribution.

Pro Tips

Use the -v (verbose) option: This provides more detailed output about what the cp or rsync command is doing. It’s a great way to learn more about these commands and troubleshoot when things go wrong. Here’s an example of how you can use it with the cp command:

cp -v ~/file1.txt ~/Documents/

The output would display something like this:

'/home/user/file1.txt' -> '/home/user/Documents/file1.txt'

This shows that ‘file1.txt’ has been copied from your home directory to the ‘Documents’ directory.

rsync over cp for large transfers: When copying large files or directories, rsync is more efficient than cp since it only transfers the changes made, not the entire file. For example, imagine you have a directory called ‘BigDirectory’ that you’ve previously copied to a backup location:

rsync -av ~/BigDirectory/ ~/Backup/

After making changes to ‘BigDirectory’, you can use the same command again to sync these changes. rsync will only transfer the changes instead of copying the entire directory again.

Use wildcards (*): This allows you to copy multiple files at once. For example, suppose you have multiple text files in your ‘Documents’ directory that you want to backup. You can do this in one command like this:

cp ~/Documents/*.txt ~/Backup/

This command will copy all the text files from the ‘Documents’ directory to the ‘Backup’ directory.

Make use of Tab Completion: Pressing the ‘Tab’ key while typing a command will auto-complete file and directory names, saving time and reducing errors from typos. Here’s how it works:

Imagine you have a file called ‘verylongfilename.txt’ and you want to copy it to another directory. Instead of typing the entire filename, you can just type the first few characters and then press the ‘Tab’ key:

cp ~/ver (press Tab)

If ‘verylongfilename.txt’ is the only file that starts with ‘ver’, the shell will automatically complete the filename:

cp ~/verylongfilename.txt

Then, you can just type the destination and press Enter to execute the command.

Wrapping up

Linux command line can seem intimidating, but once you learn to master the basics, it can offer an unprecedented level of control and efficiency. Copying files and directories is just the start, and there’s a lot more to learn. While I’m more partial to rsync for its advanced options and efficiency, cp also holds a special place for its simplicity and widespread use. It’s the same sense of satisfaction that comes with finding the perfect tool for a task.

Remember, practice is key to mastering the Linux CLI. So, go ahead and experiment with these commands and options, and don’t worry if you mess up. It’s all part of the learning process. As for me, I’ll be here, ready to help you navigate the fascinating world of Linux. Happy learning!

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.