Any options with "cp" to create a folder if it doesn't exist.

I've created a backup and restore script and it's working fine so it's all about making it more robust at the moment for me. Let's say the user has a folder in their home directory for word processing files called "wp". I want to copy the files I made a backup of from this directory back into it. We'll say the backup files are in a backup folder in the user's home directory,

I have currently:

cp -i /home/user/backup/wp/* /home/user/wp/

This works assuming the wp directory exists in the user's home directory. My question: is there any way to keep this working still but also in the scenario if, say, the user didn't have the wp directory? Like create it if it wasn't there already, yet still copy files into it if it was already there.

Thanks for any reply,

Ewan.

You could use:

cp -rip /home/user/backup/wp /home/user

Which will copy subdirectories too. The wp directory will be created automatically if it does not yet exist. -p preserves permissions.

Hi

why not insert a if block before you copy the files ?

if [ ! -d <your dir> ] ; then
  mkdir <your dir>
fi

cheers