Page 1 of 1

Need script to copy a file and its parent folder (Solved)

Posted: Thu 16 Jan 2020, 09:49
by nic007
I have a script which finds certain files in the running system and then copies it to another location. My problem is with the copying part. I can copy the actual file but not the file AND its parent directory/sub directory to the new location. This is an excerpt from the script:

Code: Select all

for x in `cat $D/$PKG`; do [ -d $x ] && cd $x || cp -a $x /initrd/mnt/dev_ro2/test; done
Any help will be appreciated. PS: I've tried the --parents attribute but without success.

Posted: Thu 16 Jan 2020, 13:52
by paulh177
Probably we need to see the whole script and a fuller description of the requirement to make appropriate suggestions.

But my first thought is that you might do better to investigate rsync which is built for recursive copying & can certainly create directories on the fly during a copy

Posted: Thu 16 Jan 2020, 14:23
by nic007
I tried rsync with -avR attributes. Same result, only the actual files without parent folders are copied.

I want to use Puppy's remove_builtin packages script but instead of "removing" a particular package from the filesystem, I want to copy the files of the particular package to a designated folder. Attached is the standard remove_builtin script as it is used in Puppy.

Posted: Thu 16 Jan 2020, 14:28
by paulh177
this is literally the first result from googling `rsync parent` :

https://stackoverflow.com/questions/184 ... irectories

Posted: Fri 17 Jan 2020, 18:16
by HerrBert
Since you cd to $x, you cannot use cp --parents. If you remain in / you might be able to copy directory-structure.

First idea that comes to mind:

Code: Select all

cd /
for x in `cat $D/$PKG`; do [ -d $x ] && MYPATH="$x" || cp --parents -a "${MYPATH}/$x" /initrd/mnt/dev_ro2/test; done
But IIRC some files in builtin_files keep full path in filename.
All untested, so no warrenty :wink:

Posted: Sat 18 Jan 2020, 06:18
by nic007
HerrBert wrote:Since you cd to $x, you cannot use cp --parents. If you remain in / you might be able to copy directory-structure.

First idea that comes to mind:

Code: Select all

cd /
for x in `cat $D/$PKG`; do [ -d $x ] && MYPATH="$x" || cp --parents -a "${MYPATH}/$x" /initrd/mnt/dev_ro2/test; done
But IIRC some files in builtin_files keep full path in filename.
All untested, so no warrenty :wink:
That seems to work. Thanks for your input. Marked as solved. :)