I have a folder with more than 30 sub directories and I want to get the list of the files which was modified after a specified date(say sep 8 which is the real case) and to be copied with the same tree structure with only the modified files in that folder
I have say 30 dir from that I have the list of the files I need found using last modified date
Find command output
a/a.txt
a/b/b.txt
a/www.txt
etc..
For example I want the folder "a" created and only the a.txt in it…like wise for the other also "a/b" to be created and b.txt inside it…
Best Answer
Assuming you have your desired files in a text file, you could do something like
That will read each line of your list, extract the directory and the file name, create the directory and copy the file. You will need to change
source
andtarget
to the actual parent directories you are using. For example, to copy/foo/a/a.txt
to/bar/a/a.txt
, changesource
tofoo
andtarget
tobar
.I can't tell from your question whether you want to copy all directories and then only specific files or if you just want the directories that will contain files. The solution above will only create the necessary directories. If you want to create all of them, use
That will create the directories. Once those are there, just copy the files:
Update
This little script will move all the files modified after September 8. It assumes the GNU versions of
find
andtouch
. Assuming you're using Linux, that's what you will have.Strictly speaking, you don't need the tmp file. However, this way, the same script will work tomorrow. Otherwise, if you use find's
-mtime
, you would have to calculate the right date every day.Another approach would be to first find the directories, create them in the target and then copy the files:
Create all directories
Find and copy the relevant files
Remove any empty directories