Questions tagged [cp]

cp is a UNIX command used to copy a file

Description

cp copies SOURCE to the destination DEST, or multiple SOURCE(s) to directory DIRECTORY.

Syntax

cp [OPTION]... [-T] SOURCE DEST

cp [OPTION]... SOURCE... DIRECTORY

cp [OPTION]... -t DIRECTORY SOURCE...

Related commands

824 questions
1447
votes
3 answers

How do I create a copy of a directory in Unix/Linux?

I want to recursively create a copy of a directory and all its contents (e.g. files and subdirectories).
user2080656
  • 14,579
  • 3
  • 13
  • 4
856
votes
19 answers

How to force cp to overwrite without confirmation

I'm trying to use the cp command and force an overwrite. I have tried cp -rf /foo/* /bar, but I am still prompted to confirm each overwrite.
Thiyagarajan Varadharaj
  • 10,154
  • 5
  • 21
  • 18
642
votes
20 answers

How to use 'cp' command to exclude a specific directory?

I want to copy all files in a directory except some files in a specific sub-directory. I have noticed that cp command didn't have the --exclude option. So, how can I achieve this?
David Liu
  • 16,374
  • 12
  • 37
  • 38
596
votes
29 answers

Linux: copy and create destination dir if it does not exist

I want a command (or probably an option to cp) that creates the destination directory if it does not exist. Example: cp -? file /path/to/copy/file/to/is/very/deep/there
flybywire
  • 261,858
  • 191
  • 397
  • 503
594
votes
7 answers

How to have the cp command create any necessary folders for copying a file to a destination

When copying a file using cp to a folder that may or may not exist, how do I get cp to create the folder if necessary? Here is what I have tried: [root@file nutch-0.9]# cp -f urls-resume /nosuchdirectory/hi.txt cp: cannot create regular file…
omg
  • 136,412
  • 142
  • 288
  • 348
506
votes
8 answers

Linux how to copy but not overwrite?

I want to cp a directory but I do not want to overwrite any existing files even it they are older than the copied files. And I want to do it completely noninteractive as this will be a part of a Crontab Bash script. Any ideas?
mnowotka
  • 16,430
  • 18
  • 88
  • 134
309
votes
10 answers

Copy files from one directory into an existing directory

In bash I need to do this: take all files in a directory copy them into an existing directory How do I do this? I tried cp -r t1 t2 (both t1 and t2 are existing directories, t1 has files in it) but it created a directory called t1 inside t2, I…
David Chang
  • 3,091
  • 2
  • 15
  • 3
189
votes
7 answers

Copy all files with a certain extension from all subdirectories

Under unix, I want to copy all files with a certain extension (all excel files) from all subdirectories to another directory. I have the following command: cp --parents `find -name \*.xls*` /target_directory/ The problems with this command are: It…
Abdel
  • 5,826
  • 12
  • 56
  • 77
179
votes
10 answers

How to force 'cp' to overwrite directory instead of creating another one inside?

I'm trying to write a Bash script that will overwrite an existing directory. I have a directory foo/ and I am trying to overwrite bar/ with it. But when I do this: cp -Rf foo/ bar/ a new bar/foo/ directory is created. I don't want that. There are…
saketrp
  • 2,323
  • 3
  • 16
  • 18
145
votes
9 answers

BASH copy all files except one

I would like to copy all files out of a dir except for one named Default.png. It seems that there are a number of ways to do this. What seems the most effective to you?
Joe Cannatti
  • 4,989
  • 5
  • 38
  • 56
116
votes
11 answers

Copy or rsync command

The following command is working as expected... cp -ur /home/abc/* /mnt/windowsabc/ Does rsync has any advantage over it? Is there a better way to keep to backup folder in sync every 24 hours?
shantanuo
  • 31,689
  • 78
  • 245
  • 403
106
votes
4 answers

cp -r without hidden files

I have two directories and one is empty. The first directory has many sub directories with hidden files. When I cp -r content from first directory to the second one, the hidden files gets copied too. Any solutions to escape them?
Rahul
  • 5,493
  • 6
  • 18
  • 12
91
votes
6 answers

Getting an error cp: cannot stat when trying to copy files from one folder to another

I have this directory called "mock", which contains 3 directories. I am trying to copy all the items from "mock" directory into the "projweek" directory using the following command: cp /mock/* ~/projweek But I get this error: cp: cannot stat…
user5647516
  • 1,083
  • 1
  • 9
  • 19
68
votes
7 answers

Recursive copy of a specific file type maintaining the file structure in Unix/Linux?

I need to copy all *.jar files from a directory maintaining the folder structure of the parent directories. How can I do it in UNIX/Linux terminal? The command cp -r *.jar /destination_dir is not what I am looking for.
Yury Pogrebnyak
  • 4,093
  • 9
  • 45
  • 78
60
votes
4 answers

pass output as an argument for cp in bash

I'm taking a unix/linux class and we have yet to learn variables or functions. We just learned some basic utilities like the flag and pipeline, output and append to file. On the lab assignment he wants us to find the largest files and copy them to a…
Yamiko
  • 5,303
  • 5
  • 30
  • 52
1
2 3
54 55