Copy a file by creating folder structure in destination as in Souce

Hello,

i am having a source directory which consist of multiple sub directories and my destination folder is a empty directory.

if try to copy a file source->test->1.txt from source to destination test2 using the commaind.
cp source/test/1.txt desti/

It will copy the 1.txt under desti directory.

but i need tocreating directory test folder under desti folder then copy the file to desti/test/ folder.

Please advice me how to do this.

thanks in advance.

~Saravana

Is that the real scenario? or do you want to create the complete directory structure?

s_dir=/some/path/source/test
s_file=1.txt
t_dir=/some/path/dest/$(basename $s_dir)

mkdir -p $t_dir
cp $s_dir/$s_file $t_dir/

for the complete directory structure:

change:

t_dir=/some/path/dest/$(basename $s_dir)

to:

t_dir=/some/path/dest/dest/$s_dir

please test first.

1 Like