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

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 {} \;

Related

Create directory based on date/time and copy files to it?

I'm attempting to create a script that will create a folder based on the current time and date. I then need the script to copy the files from a source folder to the newly created folder. I then need it to copy folders from a second source folder to the original source folder, overwriting everything that's in there.
Below is what I've tried, and it's failing in quite an epic fashion.
#!/bin/bash
d="/home/$(date +%d-%m-%y")"
mkdir "$d"
cp /home/test "$d"
cp /home/test2 /home/test
I'm aware that I don't have to define the variable, as the time between copies should be seconds and not lapse a day, but I wanted to make sure and honestly, I'm interested in learning to use variables in scripting.
There is one too many double quote here:
d="/home/$(date +%d-%m-%y")"
Actually no quoting is necessary here at all, write like this:
d=/home/$(date +%d-%m-%y)
In the rest of the script, if you want to copy directories, you will need to use cp -r instead of simply cp.
Finally, note that when you do cp -r dir1 dir2 when dir2 already exists, then dir1 will be copied inside dir2, rather than overwriting its content. That is, it will create dir2/dir1. If dir1 doesn't contain hidden files, then you can write like this to overwrite the content of dir2:
cp -r dir1/* dir2/

Copy files from multiple folders to another multiple folders Linux

I have a huge list of files from different folder and i need to copy them to another folder.
I have already created those folders which I will copy.
I have tried the code:
for file in $(cat /home/pdf/report/folder/files.txt | sed $'s/\r//'); do cp "$file" /home/pdf/report/folder/dest.txt; done
Is there any solutions in Linux?
I would do it this way:
while read file
do
cp "$file" /home/pdf/report/folder/
done < /home/pdf/report/folder/files.txt
The important point is you're copying into the folder without specifying the filename, just the folder, meaning the copy will have the same name as the original

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/

mv: cannot overwrite directory with non-directory

Is it possible to get around this problem?
I have a situation where I need to move some files to 1 directory below.
/a/b/c/d/e/f/g
problem is that the filename inside g/ directory is the same as the directory name
and I receive the following error:
mv: cannot overwrite directory `../297534' with non-directory
Example:
/home/user/data/doc/version/3766/297534 is a directory, inside there is a also a file named 297534
so I need to move this file to be inside /home/user/data/doc/version/3766
Command
This is what I am running: (in a for loop)
cd /home/user/data/doc/version/3766/297534
mv * ../
You can't force mv to overwrite a directory with a file with the same name. You'll need to remove that file before you use your mv command.
Add one more layer in your loop.
Replace mv * ../ with
for f in `ls`; do rm -rf ../$f; mv $f ..; done
This will ensure that any conflict will be deleted first, assuming that you don't care about the directory you're overwriting.
Note that this will blow up if you happen to have a file inside the current directory which matches the current directory's name. For example, if you're in /home/user/data/doc/version/3766/297534 and you're trying to move a directory called 297534 up. One workaround to this is to add a long suffix to every file, so there's little chance of a match
for f in `ls`; do mv $f ../${f}_abcdefg; done

Copy all files in a directory to a local subdirectory in linux

I have a directory with the following structure:
file_1
file_2
dir_1
dir_2
# etc.
new_subdir
I'd like to make a copy of all the existing files and directories located in this directory in new_subdir. How can I accomplish this via the linux terminal?
This is an old question, but none of the answers seem to work (they cause the destination folder to be copied recursively into itself), so I figured I'd offer up some working examples:
Copy via find -exec:
find . ! -regex '.*/new_subdir' ! -regex '.' -exec cp -r '{}' new_subdir \;
This code uses regex to find all files and directories (in the current directory) which are not new_subdir and copies them into new_subdir. The ! -regex '.' bit is in there to keep the current directory itself from being included. Using find is the most powerful technique I know, but it's long-winded and a bit confusing at times.
Copy with extglob:
cp -r !(new_subdir) new_subdir
If you have extglob enabled for your bash terminal (which is probably the case), then you can use ! to copy all things in the current directory which are not new_subdir into new_subdir.
Copy without extglob:
mv * new_subdir ; cp -r new_subdir/* .
If you don't have extglob and find doesn't appeal to you and you really want to do something hacky, you can move all of the files into the subdirectory, then recursively copy them back to the original directory. Unlike cp which copies the destination folder into itself, mv just throws an error when it tries to move the destination folder inside of itself. (But it successfully moves every other file and folder.)
You mean like
cp -R * new_subdir
?
cp take -R as argument which means recursive (so, copy also directories), * means all files (and directories).
Although * includes new_subdir itself, but cp detects this case and ignores new_subdir (so it doesn't copy it into itself!)
Try something like:
cp -R * /path_to_new_dir/

Resources