Making a copy of all files (including subdirectories)...

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
tharpa
Level 1
Level 1
Posts: 33
Joined: Sat Mar 24, 2012 3:18 pm
Location: North America
Contact:

Making a copy of all files (including subdirectories)...

Post by tharpa »

I would like to write a command to copy all of files in a directory and its subdirectories that have been modified in the last month. How do I do this?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
bigj231

Re: Making a copy of all files (including subdirectories)...

Post by bigj231 »

Not sure of a way to only copy files modified in the past month, but using cp with the -rfu flag should copy all files in all subfolders that are newer than the ones in the target directory.

I'm sure there is a way to only copy files modified in the past month, but it will most likely require a few lines of code instead of a single command.

Here's one of the top results of a google search using the query "how to copy files modified recently linux"
[url]http://www.unix.com/shell-programming-scripting/34582-trying-copy-files-changed-recently.html[/url]
When scripting, google is your friend.
tharpa
Level 1
Level 1
Posts: 33
Joined: Sat Mar 24, 2012 3:18 pm
Location: North America
Contact:

Re: Making a copy of all files (including subdirectories)...

Post by tharpa »

Thanks, big. What is this Google you speak of? Cuz, ya know, if you don't know the answer, then it's never the case that the OP has already tried Google (and/or other search engines) before posting.

If anyone else has helpful info, please let me know.
Habitual

Re: Making a copy of all files (including subdirectories)...

Post by Habitual »

Code: Select all

man find
...
       -mtime n
              File's  data  was  last  modified  n*24 hours ago.  See the comments for -atime to understand how
              rounding affects the interpretation of file modification times.
tharpa
Level 1
Level 1
Posts: 33
Joined: Sat Mar 24, 2012 3:18 pm
Location: North America
Contact:

Re: Making a copy of all files (including subdirectories)...

Post by tharpa »

The Ubuntu forum was actually helpful.

Code: Select all

cd /src
rsync -aR0vn --files-from=<( find . -mtime -30 -type f -print0 ) . /dest

n = dry run 
[url]http://ubuntuforums.org/showthread.php?t=2174664[/url]
Habitual

Re: Making a copy of all files (including subdirectories)...

Post by Habitual »

Yeah, Vaphell is a code-ninja. ;)
Locked

Return to “Scripts & Bash”