How to have the cp command create any necessary folders for copying a file to a destination [duplicate] - linux

This question already has answers here:
Linux: copy and create destination dir if it does not exist
(27 answers)
Closed 7 years ago.
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 `/nosuchdirectory/hi.txt': No such file or directory

To expand upon Christian's answer, the only reliable way to do this would be to combine mkdir and cp:
mkdir -p /foo/bar && cp myfile "$_"
As an aside, when you only need to create a single directory in an existing hierarchy, rsync can do it in one operation. I'm quite a fan of rsync as a much more versatile cp replacement, in fact:
rsync -a myfile /foo/bar/ # works if /foo exists but /foo/bar doesn't. bar is created.

I didn't know you could do that with cp.
You can do it with mkdir ..
mkdir -p /var/path/to/your/dir
EDIT
See lhunath's answer for incorporating cp.

One can also use the command find:
find ./ -depth -print | cpio -pvd newdirpathname

mkdir -p `dirname /nosuchdirectory/hi.txt` && cp -r urls-resume /nosuchdirectory/hi.txt

There is no such option. What you can do is to run mkdir -p before copying the file
I made a very cool script you can use to copy files in locations that doesn't exist
#!/bin/bash
if [ ! -d "$2" ]; then
mkdir -p "$2"
fi
cp -R "$1" "$2"
Now just save it, give it permissions and run it using
./cp-improved SOURCE DEST
I put -R option but it's just a draft, I know it can be and you will improve it in many ways. Hope it helps you

rsync is work!
#file:
rsync -aqz _vimrc ~/.vimrc
#directory:
rsync -aqz _vim/ ~/.vim

cp -Rvn /source/path/* /destination/path/
cp: /destination/path/any.zip: No such file or directory
It will create no existing paths in destination, if path have a source file inside.
This dont create empty directories.
A moment ago i've seen xxxxxxxx: No such file or directory, because i run out of free space. without error message.
with ditto:
ditto -V /source/path/* /destination/path
ditto: /destination/path/any.zip: No space left on device
once freed space cp -Rvn /source/path/* /destination/path/ works as expected

Related

Copy a directory structure and only touch the copied files

I want to mimic copying a directory structure recursively (as in cp -r or rsync -a), but only touch the copied files, i.e. make all the copied files empty.
The specific use case is for a Snakemake pipeline; Snakemake looks for existing files in order to decide whether to re-run a pipeline step, and I want to make it believe the steps have already been run while avoiding fully downloading all the files.
This is a little kludgy, but you could pipe the output of find or rsync -nv into a little bash loop with mkdir -p and touch:
find /some/dir -type f | while read FILE; do
mkdir -p $(dirname $FILE)
touch $FILE
done

Makefile: using cp command while reading from a file

In makefile, I am doing something like this -
#while read -r file; do \
if [ ! -z "$$file" ]; then \
cp -R path/to/someplace/$$file path/to/someplace/else/$$file \
fi \
done <filename.txt
filename.txt contains files and folders like -
abc/*.txt
folder1/
folder2/
textfile.txt
I am able to copy files and folders but in the case of abc/*.txt it shows an error:
cp: target `/path/to/someplace/else/*.txt' is not a directory.
Is there some way possible to copy these files with wildcard characters?
Actually the problem is not in the pattern expansion. That bit would appear to work as expected. The error is actually due to the resulting cp command. You end up with for instance:
cp abc/a.txt abc/b.txt other/path/abc/*.txt
However, for multiple source files cp expect destination to be a directory, which other/place/abc/* is not (it could be if you created it, but is unlikely what you wanted).
That said. You could for instance create the target directory by calling mkdir -p other/path/`dirname $$file` (escaped $ for make) and with cp use `dirname $$file` as well for destination.
There are few caveats. For instance if absolute paths were to be encountered or globing characters where also used on directories leading up to the files themselves (dir?/*.txt, some/*/file). It would perhaps be safer to use for instance tar or cpio:
tar cf - -C /source/path src/*.txt | tar xf - -C /target/path/

How to copy a file and all the directories that comform his path

Is there an easy way to copy an specific file nested in an already nested directory creating an structure of directories nested in the same way its file path (in linux)?
for instance;
copy_command A/B/C/a.txt OTHER_DIR
would create
OTHER_DIR/A/B/C/a.txt
creating the directory structure A/B/C into OTHER_DIR and copying the file a.txt on his corresponding dir.
With GNU cp
cp --parents -- A/B/C/a.txt OTHER_DIR
The ${var_name%pattern} syntax removes pattern from the variable's value. With that in mind:
file="A/B/C/a.txt"
mkdir -p "OTHER_DIR/${file%/*}"
cp "$file" "OTHER_DIR/${file%/*}/"
Which is equivalent to:
mkdir -p OTHER_DIR/A/B/C
cp A/B/C/a.txt OTHER_DIR/A/B/C/

CentOS: Copy directory to another directory

I'm working with a CentOS server. I have a folder named test located in /home/server/folder/test. I need to copy the directory test to /home/server/. How can I do it?
cp -r /home/server/folder/test /home/server/
To copy all files, including hidden files use:
cp -r /home/server/folder/test/. /home/server/
As I understand, you want to recursively copy test directory into /home/server/ path...
This can be done as:
-cp -rf /home/server/folder/test/* /home/server/
Hope this helps
This works for me.
cp -r /home/server/folder/test/. /home/server
For copy directory use following command
cp -r source Destination
For example
cp -r /home/hasan /opt
For copy file use command without -r
cp /home/file /home/hasan/

Copy and overwrite a file in shell script

I want to copy a certain file to a location, irrespective of that file already exists in the destination or not. I'm trying to copy through shell script.But the file is not getting copied. I'm using the following command
/bin/cp -rf /source/file /destination
but that doesn't work.
Use
cp -fr /source/file /destination
this should probably solve the problem.
This question has been already discussed, however you can write a little script like this:
#!/bin/bash
if [ ! -d "$2" ]; then
mkdir -p "$2"
fi
cp -R "$1" "$2"
Explaining this script a little bit
#!/bin/bash: tells your computer to use the bash interpreter.
if [ ! -d "$2" ]; then: If the second variable you supplied does not already exist...
mkdir -p "$2": make that directory, including any parent directories supplied in the path.
Running mkdir -p one/two/three will make:
$ mkdir -p one/two/three
$ tree one
one/
└── two
└── three
If you don't supply the -p tag then you'll get an error if directories one and two don't exist:
$ mkdir one/two/three
mkdir: cannot create directory ‘one/two/three’: No such file or directory
fi: Closes the if statement.
cp -R "$1" "$2": copies files from the first variable you supplied to the directory of the second variable you supplied.
So if you ran script.sh mars pluto, mars would be the first variable ($1) and pluto would be the second variable ($2).
The -R flag means it does this recursively, so the cp command will go through all the files and folders from your first variable, and copy them to the directory of your second variable.
Your problem might be caused by an alias for cp command created in your system by default (you can see al your aliases by typing "alias").
For example, my system has the following alis by default: alias cp='cp -i', where -i overrides -f option, i.e. cp will always prompt for overwriting confirmation.
What you need in such case (that'll actually work even if you don't have an alias) is to feed "yes" to that confirmation. To do that simply modify your cp command to look like this:
yes | cp /source/file /destination
/bin/cp -rf src dst
or
/usr/bin/env cp -rf

Resources