Move files & folder structure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Move files & folder structure
# 1  
Old 08-26-2010
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/
File1.txt
File2.txt
File3.txt
/dir2/
File4.txt
File5.txt
/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/
/dir2/
/home/temp/work/dir1/
File1.txt
File2.txt
File3.txt
/dir2/
File4.txt
File5.txt
/home/temp/complete/dir1/
File1.txt
File2.txt
File3.txt
/dir2/
File4.txt
File5.txt
I hope that's clear, thanks for any help!Smilie
# 2  
Old 08-26-2010
man cp

Code:
# cp -Rp {olddir} {newdir}

This will copy all files and folders under {olddir}

-R = Recursive
-p = Preserve ownership (you probably want that too)
This User Gave Thanks to Ikon For This Post:
# 3  
Old 08-26-2010
Code:
# 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
This User Gave Thanks to ygemici For This Post:
# 4  
Old 08-26-2010
Quote:
Originally Posted by ygemici
Code:
# 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
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,
Code:
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 Smilie

Last edited by Franklin52; 08-27-2010 at 03:21 AM.. Reason: Please use code tags, thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

Script to move files and Creation of folder structure

We are receiving few zipped files in one location say : apple/oranges/incoming All .zip files are placed here in incoming folder. So few of the files are password encrypted. There are only 10 zipped files, so we are planning to create a script which will pick that zip file from incoming... (1 Reply)
Discussion started by: Sidhant
1 Replies

3. Shell Programming and Scripting

Parallel move keeping folder structure along with files in it

The below will move all the files in the directory dir to the destination using parallel and create a log, however will not keep them in the directory. I have tried mkdir -p but that does not seem to work or at least I can not seem to get it (as it deletes others files when I use it). What is the... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

How to move the files older than x days with similar directory structure?

Hello, I need to move all the files inside /XYZ (has multi-depth sub directories) that are older than 14 days to/ABC directory but with retaining the SAME directory structure. for example: /XYZ/1/2/3/A/b.txt should be moved as /ABC/1/2/3/A/b.txt I know about find /XYZ -type f -mtime +14... (3 Replies)
Discussion started by: prvnrk
3 Replies

5. Shell Programming and Scripting

Move files from Space Folder to other folder

I want to move a folder with spaces from one folder to another. I have two folders like this, 1).RT_032-222 -4444-01/ 2). RT_032-555 -7777-01/ I want to move files from 2 to 1 through shell script.Here I want to assign this like a user defined variable like as Source branch... (2 Replies)
Discussion started by: kannansoft1985
2 Replies

6. Shell Programming and Scripting

Copying files from various folders to similar folder structure in another location

Hi, I need to write a script the has to copy the files from folders and subfolders to the same folder structure located in another location. Ex: mainfolder1 file1,file2,file3 subfolder1(file1,etc) subfolder2(file1,etc) to another folder location of same folder structure. rsync is not... (7 Replies)
Discussion started by: Raji Perumal
7 Replies

7. Linux

Create folder by script & move files in it

Hi Team, I have over 1 lakh pdf files. I want to create folders like Disk-1, Disk-2 ..... & want to move 3000 pdfs per folder. Can i do it by script? Please help me. Thanks & Regards Parag Nehete (4 Replies)
Discussion started by: paragnehete
4 Replies

8. Shell Programming and Scripting

Need to Zip files three years old or longer but leave folder structure intact

Hi all, Hello everyone, my first post here :). I tried to search the forum but I didn't find exactly what I was looking for... I need to zip/tar files across entire filesystem which are more 3+ years old but leave folder structure intact. If the script locates tar/zip files they are more... (1 Reply)
Discussion started by: sashruby
1 Replies

9. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

10. Shell Programming and Scripting

move files and retain subdir structure

hi: I have some files like this folder1/recording1.mp3 folder1/docs/budget.doc folder2/others/misc.mp3 folder3/others/notes.doc all this folders and files are under the mp3 folder. I would like to move just the mp3s to another folder but retain the subdir structure i have. So if... (4 Replies)
Discussion started by: jason7
4 Replies
Login or Register to Ask a Question