12

How can I do this on the command line?

e.g. copy /dir/another/file to /tmp/ so I have /tmp/dir/another/file

0

3 Answers 3

26
cp --parents /dir/another/file /tmp

will do exactly what you want.

3
  • any ideas for os/x? --parents do not live here ;( Mar 27, 2018 at 2:29
  • tar or rsync? See the other two answers :-) Apr 26, 2018 at 7:24
  • 1
    For macOS you can, via homebrew, install brew install coreutils and use gcp --parents /dir/another/file /tmp Feb 27, 2019 at 13:42
5

rsync can be a good help for this :

rsync -Ravz my/first/dir/file.txt another_dir

will gave as result

another_dir/my/first/dir/file.txt
3

You can use tar to preserve paths while copying files:

tar cf - /dir/another/file | (cd /tmp && tar xf -)

You must log in to answer this question.

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