0

Source file : ~/SCRIPTS/script

Destination directory : ~/TMP/LAB

the command should be something like : <command> <option> ~/SCRIPTS/script ~/TMP/LAB

Desired result after the command : ~/TMP/LAB/SCRIPTS/script

There's no point in doing

<command> <option> ~/SCRIPTS/script  ~/TMP/LAB/SCRIPTS/

Because I'll have to edit the command each time I change the source file. Basically I want the command to create the dirs based on the structure it finds in the source, not based on the target I desire to create.

The command should :

  1. create ~/TMP/LAB/SCRIPTS/
  2. copy ~/SCRIPTS/script to it.

cp --parents does that, but only when copying directories

I don't know if install can do that for me? or maybe rsync ?

2
  • cp -r ~/SCRIPTS ~/TMP/LAB?
    – White Owl
    May 26, 2022 at 12:14
  • @WhiteOwl that would copy everything inside ~/SCRIPTS/. It turns out I was wrong about cp --parents, it works when copying files as well.
    – ychaouche
    May 26, 2022 at 12:19

2 Answers 2

1

You should be able to use cp --parents if you have GNU cp.

bash-5.1$ tree
.
|-- SCRIPTS
|   |-- otherfile
|   `-- script
`-- TMP
    `-- LAB

3 directories, 2 files
bash-5.1$ cp --parents SCRIPTS/script TMP/LAB/
bash-5.1$ tree
.
|-- SCRIPTS
|   |-- otherfile
|   `-- script
`-- TMP
    `-- LAB
        `-- SCRIPTS
            `-- script

4 directories, 3 files

You suggested using rsync, which you can do if you use its -R (--relative) option:

bash-5.1$ rm -r TMP/LAB/SCRIPTS/
bash-5.1$ rsync -R SCRIPTS/script TMP/LAB/
bash-5.1$ tree
.
|-- SCRIPTS
|   |-- otherfile
|   `-- script
`-- TMP
    `-- LAB
        `-- SCRIPTS
            `-- script

4 directories, 3 files

Note that in both of these variations, using the absolute path ~/SCRIPTS/script as the source rather than the relative path SCRIPTS/script would create directories corresponding to the absolute path at the destination, and you would end up with something like TMP/LAB/home/myself/SCRIPTS/script.

You would avoid this by first changing directories to the directory containing the SCRIPTS directory.

1
  • Thanks for the rsync -R tip, nice!
    – ychaouche
    Jun 1, 2022 at 12:24
0

It turns out cp --parents create parents even when source is a file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .