Folder to Folder move


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Folder to Folder move
# 1  
Old 07-23-2010
Folder to Folder move

Hi,

I need a little help with this one; Let's say I have two directories

/dir1/dir2/dir3old and /dir1/dir2/dir3new

Within dir3old and dir3new are many folders, each containing some files. How can I search all the sub folders in dir3old for files of a specific date and move the files to the corresponding folders in dir3new?
# 2  
Old 07-24-2010
Doing this portably is more difficult than it sounds.. though anything is possible.

The "find" command excels at finding files that are newer than a target time. So, if the goal is finding files within a tree that are newer than a specified time, that can be done with "find". If this isn't Linux or a platform with GNU find, then the -newer argument of find can be used with a file argument where that file's mtime is used as the target time mentioned above. You can use the touch command to create the file with the timestamp for which you want "find" to find files newer than.

I realize that tons of people are using Linux now or have a good "find" (e.g. GNU find), but I like to show people the "portable" way.

Consider:

touch -t 201007230000 reftime.stamp

(you'll have to do a "man" on "touch" to see if your accepts a -t option or expects the time string, possibly in a different format, simply as an argument)

Then you could do:

find /dir1/dir2/dir3old -type f -newer /fullpathto/reftime.stamp

To get a list of the files that are newer than the time stamp.

Now it would be easy to do a copy at this point using something like cpio to copy the files over... but that isn't what you asked for. You want them to move.. and I don't blame you... just an inode remove/add to a directory entry (very fast).

So you want something like:

mv /dir/dir2/dir3old/p1/p2/p3/file1 /dir1/dir2/dir3new/p1/p2/p3/file1

As long as the destination file1 doesn't exist, which you make it sound like it does not, and the directories already exist on the destination side, then we can filter our list of files into the corresponding "mv" commands to feed to the shell.

Note: the following makes an assumption that the filenames do not contain special characters, but spaces are allowed for.

(this is a one liner)

find /dir1/dir2/dir3old -type f -newer /fullpathto/reftime.stamp |
sed 's,^/dir1/dir2/dir3old/\(.*\),mv "/dir1/dir2/dir3old/\1" "/dir1/dir2/dir3new/\1",'

The nice thing about using a filter like the above is we can run and and safely see the output and see if it makes sense before piping that to a shell.

As I said, it makes assumptions based on what you said... not sure if it's true though. Also, sometimes it helps to know EXACTLY what you are wanting to do this for and on what OS... then it's VERY likely that a more elegant and correct solution can be presented.
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. UNIX for Beginners Questions & Answers

Best way to move the contents of a folder to another one

what is the best way to move the contents of a folder to another one without deleting the structure of the first one. the contents could include subfolder too. both folder, the source-folder and the target-folder are on the same host. any idea is appreciated . (7 Replies)
Discussion started by: andy2000
7 Replies

3. 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

4. Shell Programming and Scripting

Script to move one folder to multiple folder...

Hi All, I have to requirement to write a shell script to move file from one folder (A) to another five folder (B,C,D,E,F) and destination folder should be blank. In not blank just skip. This script will run as a scheduler every 2 minutes. It will check number of files in folder A and move 1 to... (9 Replies)
Discussion started by: peekuabc
9 Replies

5. Shell Programming and Scripting

want to move set of file from one folder to another folder

Hi all, let me explain my requirments i am having 5 folder with different name for eg) abc , cdf , efd, rtg, ead each 5 folders contain 15 files i want to move 10 files to some other folder, remain 5 files should be there in the same folder. give me some suggestion on this. (6 Replies)
Discussion started by: natraj005
6 Replies

6. Shell Programming and Scripting

Move only files to a folder

I want to move all the files inside a directory to another directory which is inside that directory. Please help. Note: I want to move only the files. Note the folders. Thanks in advance. (4 Replies)
Discussion started by: brnl_basu
4 Replies

7. Shell Programming and Scripting

Move to different folder

Hi, I have a requirement to move to diff directories based on the input file Project|SourceFolder|SourceFile|TargetFolder|TargetFile the above is my Record in a file I have to read the record and move to the Target folder to copy some records var_TargetSystem==`cat "$var_SourceFile" |... (3 Replies)
Discussion started by: magesh_bala
3 Replies

8. 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

9. UNIX for Dummies Questions & Answers

Move the folder which I right click

Hi! I don't use to program much with unix but it would be useful to create a script to move a folder to a default directory. For example I right click on a directory and the script moves it to the home folder At the moment I've this but doesn't work: and I also tryed this and... (0 Replies)
Discussion started by: ruben.rodrigues
0 Replies

10. Shell Programming and Scripting

Move the file from one folder to another folder

Hi, I have a requirement to move a file from one folder(a) to another folder(b) only when folder (b) have a write permission. Folder permission is 755 If the permission is otherthan 755 we need to come out of the loop I will appreciate your help Thanks Soll (1 Reply)
Discussion started by: sollins
1 Replies
Login or Register to Ask a Question