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

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
nic007
Posts: 3408
Joined: Sun 13 Nov 2011, 12:31
Location: Cradle of Humankind

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

#1 Post 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.
Last edited by nic007 on Sat 18 Jan 2020, 06:20, edited 1 time in total.

User avatar
paulh177
Posts: 975
Joined: Tue 22 Aug 2006, 20:41

#2 Post 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

User avatar
nic007
Posts: 3408
Joined: Sun 13 Nov 2011, 12:31
Location: Cradle of Humankind

#3 Post 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.
Attachments
remove_builtin.zip
(1.69 KiB) Downloaded 72 times
Last edited by nic007 on Thu 16 Jan 2020, 14:40, edited 1 time in total.

User avatar
paulh177
Posts: 975
Joined: Tue 22 Aug 2006, 20:41

#4 Post by paulh177 »

this is literally the first result from googling `rsync parent` :

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

HerrBert
Posts: 152
Joined: Thu 03 Nov 2016, 15:11
Location: NRW, Germany

#5 Post 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:

User avatar
nic007
Posts: 3408
Joined: Sun 13 Nov 2011, 12:31
Location: Cradle of Humankind

#6 Post 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. :)

Post Reply