nullByteMe
nullByteMe

Reputation: 6391

Is there anyway to copy a file to a directory that does not exist?

I would like to copy a file to a folder that does not exist by having cp create the destination folder and placing a source file into it. Is this possible?

So here is an example, I have a file on my desktop called temp.file. I would like to do this:

cp temp.file ./createThisFolder/

I looked through the man pages and didn't see anything. The reason I want to do this is because I want to run a test on the directory later on. If the directory doesn't exist I know there are no files waiting for me.

Upvotes: 0

Views: 6893

Answers (1)

asf107
asf107

Reputation: 1146

before your copy, you need to perform

mkdir -p ./createThisFolder

If you want to know whether or not there are files in the directory, I'd recommend directly checking whether or not your files exist (instead of checking if the directory exists). For instance:

if [ ! -s ./createThisFolder/temp.file ] ; then 
     echo "file doesn't exist (or it is empty)"
fi 

Upvotes: 3

Related Questions