My Most-Used Flags and Arguments

Jump Links

The cp command on Linux has some cool options that extend its usage. Most users only do vanilla copying and never bother to explore the flags that come with it. After trying out a few, I realized that there are some flags that I keep using for a better experience.

-r or -R: Recursive Copy

You can use the cp command for copying both files and directories. By default, it only works on files. If you try to use it with directories, you’ll get an error that skips copying the directory.

cp dirA/ dirB/
Trying to copy a directory using the cp command in Linux without using the -r flag.

This is where the -r flag comes into play. By using the -r flag, you can also copy directories and files inside them.

cp -r dirA/ dirB/

Here, we’re copying the whole “dirA/” directory to another directory called “dirB/.” You’ll notice that the “dirA/” directory itself was also copied. If you don’t want that and want to just copy the contents inside the directory, then you need to add a dot (.) after the slash, like this:

cp -r dirA/. dirB/

If you just want to create a copy in the same directory, you can, instead, pass a new name as an argument.

cp -r dirA/ dir-copy # copying the directory and renaming it
Copying a directory using the cp command in Linux using the -r flag.

-u: Copy Only When Source Is Newer

One of the most underrated flags for the cp command is -u. This option tells cp to only copy a file if the source file is newer than the destination or if the destination doesn’t exist yet.

Imagine you’ve got a directory full of project files that you’re backing up to another folder. Running a normal recursive copy every time will overwrite all the files, even the ones that haven’t changed. That’s wasted I/O and, depending on the size of the project, could slow things down considerably.

With -u, you can run the same copy command again and again, and only the updated files will be transferred. Let’s say you’ve got a “reports/” folder that you’re syncing into “backup/”:

cp -ru reports/ backup/
A series of commands to demonstrate the -u flag works with the cp command on Linux.

As seen in the above screenshot, I did a little experiment that you can also follow. The first run will copy everything. The second run will copy only the files you’ve touched or created since the last backup. If nothing has changed, the command will finish almost instantly.

-i: Interactive Mode

If you’re forgetful like me, you’ve probably run the cp command without thinking, hit Enter, and suddenly realize you’ve just overwritten a file you really didn’t want to lose. By default, cp won’t ask for confirmation. It’ll just replace the destination file silently. That’s where the -i flag comes in handy. It tells cp to ask before overwriting a file. If a destination file already exists, cp will prompt you with a simple question like:

cp: overwrite 'file.txt'?

At this point, you can type ‘y’ to overwrite it or ‘n’ to skip it. Here’s an example:

cp -i report1.txt reports/
# cp: overwrite 'reports/report1.txt'? n
cp -i report1.txt reports/
# cp: overwrite 'reports/report1.txt'? y
Copying a file using the cp command with the interactive flag to safeguard overwriting a file.

If “reports/report1.txt” already exists, you’ll get asked before replacing it. Without -i, the file would be overwritten without warning. This is particularly useful when copying into locations like “/etc/” or “/var/,” where a wrong overwrite could cause significant issues. In case you want to copy a bunch of files and only some already exist in the target, -i lets you decide case-by-case.

-v: Verbose Mode

The -v flag is pretty common for Linux commands. It enables verbose mode, which is useful to see what happens in the background when you run a command. For the cp command specifically, the -v flag shows what exactly it’s copying. With -v, every file that gets copied is printed to the terminal in the format:

'source' -> 'destination'

This makes it crystal clear what’s happening behind the scenes. Let’s see an example:

cp -v report1.txt backup/

For multiple files, you can use a wildcard:

cp -v *.txt backup/
Copying multiple files using the cp command in verbose mode to see which ones are being copied.

Now you have confirmation that every file actually went where you expected. When you’re copying files in batches, the verbose flag gives you a running log of progress. It’s also useful to catch those small mistakes in case of a failed copy.

-p: Preserve Attributes

By default, the cp command focuses on getting the content of your files from point A to point B. But sometimes the metadata, things like timestamps, file modes, and ownership, are just as important as the file itself. That’s what the -p flag is all about: preserving attributes.

When you add -p, cp will preserve these attributes. Without -p, copied files get fresh timestamps and default ownership in the destination. With -p, they remain “clones” of the originals in both content and metadata. Let’s see it in action.

cp -p config.cfg backup/
Copying a file with its attributes preserved using the cp command and the -p flag.

In the case of recursive copying:

cp -rp project/ backup/

This will copy the entire project/ directory, ensuring all files keep their original attributes. If you’re archiving files, you don’t want them to all show today’s date. Preserving the original timestamps helps you know when things actually changed.


There are many more flags and arguments for the cp command. You can explore them in the official manpage. Knowing these command options makes Linux file navigation and managing files from the terminal much easier.

Share this post:

Leave a Reply

Your email address will not be published. Required fields are marked *

From the latest gadgets to expert reviews and unbeatable deals — dive into our handpicked content across all things tech.