Move files & folder structure

Hey, this might be a really basic question, but I'm new to Unix scripting.

I'm trying to find a command to replicate a file structure from one location to another & move the actual files contained in the base directories, i.e. I have this structure -

home/temp/test/dir1/[INDENT] File1.txt
[/INDENT][INDENT] File2.txt
[/INDENT][INDENT] File3.txt
[/INDENT] /dir2/

[INDENT] File4.txt
[/INDENT][INDENT] File5.txt
[/INDENT]/home/temp/work/
/home/temp/complete/

I want to replicate the file structure under 'home/temp/test/' in 'home/temp/work/' & 'home/temp/complete/', then move the files into the new file structure, so after running the script I want to have this structure -

home/temp/test/dir1/[INDENT] /dir2/
[/INDENT]/home/temp/work/dir1/[INDENT][INDENT] File1.txt
[/INDENT][INDENT] File2.txt
File3.txt
[/INDENT][/INDENT][INDENT] /dir2/

[INDENT] File4.txt
File5.txt
[/INDENT][/INDENT]/home/temp/complete/dir1/[INDENT][INDENT] File1.txt
File2.txt
File3.txt
[/INDENT][/INDENT][INDENT] /dir2/
[/INDENT][INDENT][INDENT] File4.txt
File5.txt
[/INDENT][/INDENT]I hope that's clear, thanks for any help!:smiley:

man cp

# cp -Rp {olddir} {newdir}

This will copy all files and folders under {olddir}

-R = Recursive
-p = Preserve ownership (you probably want that too)

1 Like
# cp -a /home/temp/test/ /home/temp/work/ /home/temp/complete/

a --archive mode so means dpR

d-> not follow symbolic links
p-> preserve (mode,ownership,timestamps)
R-> copy directories recursively

1 Like

I don't think I was very clear, this works fine, in that the entire file structure is copied form /temp/test to /temp/work, but then how do I only delete the files in /temp/test without deleting the folder structure. There may be additional files copied into /temp/test before the copy is completed, that's why I would like to move & not copy the files, but I want the folder structure to remain in /temp/test.

---------- Post updated at 03:47 PM ---------- Previous update was at 02:15 PM ----------

I found a solution to my problem,

ls /home/temp/test | xargs -I {} -t mkdir -p /home/temp/work/{}

TEMP_LIST=/home/temp/test.lst
find /test/* -type f > ${TEMP_LIST}

      if [ -r $TEMP_LIST ]; then
         while read org_file; do
            mv /home/temp/test/$org_file /home/temp/work/$org_file
         done < $TEMP_LIST 
      fi
      
cp -r /home/temp/work/ /home/temp/complete/

Thanks for the help :slight_smile: