I have to copy a set of files from one folder to another folder and I'm using the following command:
cp -rf `head -n 100 /home/tmp/abc.txt` /home/tmp/test/files
the file contents in abc.txt
is like:
./folder1/example.tar.gz
./folder2/example.tar.gz
./folder3/example.tar.gz
But while executing the above cp
command I'm getting:
cp: will not overwrite just-created `/home/tmp/test/files/example.tar.gz' with `./folder3 /example.tar.gz'
cp: will not overwrite just-created `/home/tmp/test/files/example.tar.gz' with `./folder1/example.tar.gz'
I can understand that because the name of the .gz files are the same that are showing in the error. What I want to do is to have the same folder structure inside /home/tmp/test/files
as listed in abc.txt
, like:
/home/tmp/test/files/folder1/example.tar.gz
/home/tmp/test/files/folder2/example.tar.gz
/home/tmp/test/files/folder3/example.tar.gz
But I'm getting only one example.tar.gz
inside files folder after executing the above cp
command.
So what is the way to get what I mentioned above?
Best Answer
cp won't work
Your example as it stands will not work because copy doesn't copy directory structures, it will only copy the files, hence the error message you're encountering. To do a deep copy such as this you can enlist either the
tar
command and use the constructtar cvf - --files-from=... | (cd /home/tmp/test/files/; tar xvf -)
or you can just usersync
.rsync
If I were you I'd use
rsync
to do this like so:If you only want the 1st 100 lines from file
abc.txt
you can do this:Example
Sample folder data:
Now copy the files:
Confirm they were copied:
tar
If you interested here's how you do it using just
tar
.Confirm that it copied: