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

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/

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/

Copy a file in all folders of a directory [duplicate]

This question already has answers here:
Copy files from one directory into an existing directory
(9 answers)
Closed 5 years ago.
Given source file path and destination path. How to write a shell script to copy source file in all folders of destination directory?
You don't need a script. Use cp -R for recursive copy:
cp -R source_path dest_path
To do a recursive copy while preserving file attributes like last modified time etc., use the -p option as well:
cp -Rp source_path dest_path
From man cp:
-R
If source_file designates a directory, cp copies the directory and the entire subtree connected at that point. If the
source_file ends in a /, the contents of the directory are copied
rather than the directory itself. This option also causes symbolic
links to be copied, rather than indirected through, and for cp to
create special files rather than copying them as normal files.
Created directories have the same mode as the corresponding source
directory, unmodified by the process' umask.
It sounds like you want to copy a single file into all the subdirectories under the target directory (and into the target directory itself). If that is correct, then:
find $targetdir -type d -exec cp $sourcefile {} \;

Linux Copy a directory but with a different name?

I have a directory that I want to copy all of it, but to a directory with a different name.
Example:
/Home/user/DirA-Web
copy its contents to (but it needs to be created)
/Home/user/version1/DirB-Img
/Home/user/version2/DirB-Img
I could always copy it and the rename it, I suppose.
Edit: I currently rsync the directories to the desired location and them mv in a for loop to rename them. I am looking for something cleaner.
If the directory
/Home/user/version1/
exists, a simple cp will do:
cp -r /Home/user/DirA-Web /Home/user/version1/DirB-Img
If not, you need to use mkdir beforehand, because cp has no option to
create your target directory recursively:
mkdir -p /Home/user/version1/DirB-Img && cp -r /Home/user/DirA-Web /Home/user/version1/DirB-Img

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

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

Resources