brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: cp --parents copies a file and rebuilds its folder path at the destination

Linux
~/tips/tip-linux-cp-parents

mkdir -p ~/config-export

Copies files into a target folder while keeping the directory path they came from.

Grabbing a handful of config files from all over a server usually ends with a flat pile of files called config.yml. I prefer cp --parents, which keeps the path each file came from.

With --parents, cp takes the source path as you typed it and appends it to the target directory, creating any folders in between. The manual’s own example: cp --parents a/b/c existing_dir copies the file to existing_dir/a/b/c. A small config grab looks like this:

mkdir -p ~/config-export
cd /mnt/user/appdata
cp --parents sonarr/config.xml radarr/config.xml ~/config-export/

Afterwards ~/config-export has sonarr/config.xml and radarr/config.xml in their own folders, so two files with the same name don’t overwrite each other and you can see where each one belongs. It also combines well with find: find . -name '*.conf' -exec cp --parents {} /backup/conf/ \; copies every match and keeps the tree.

The caveat is that the path is taken literally. Run it with an absolute path like /etc/ssh/sshd_config and you get backup/etc/ssh/sshd_config, which may be more depth than you wanted. That’s why I’d cd to the right folder first and use relative paths. The last argument must also be an existing directory, and --parents is a GNU option, so don’t expect it in every cp.

Further reading: cp(1) manual page, GNU coreutils: cp invocation

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link