Skip to main content
Commonmark migration
Source Link

###Update

Update

###Update

Update

added 408 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

Another approach would be to first find the directories, create them in the target and then copy the files:

  1. Create all directories

     find ./ -type d -exec mkdir -p ../bar/{} \; 
  2. Find and copy the relevant files

     find ./ -type f -newer "$tmp" -exec cp {} /path/to/target/bar/{} \; 
  3. Remove any empty directories

     find ../bar/ -type d -exec rmdir {} \; 

Another approach would be to first find the directories, create them in the target and then copy the files:

  1. Create all directories

     find ./ -type d -exec mkdir -p ../bar/{} \; 
  2. Find and copy the relevant files

     find ./ -type f -newer "$tmp" -exec cp {} /path/to/target/bar/{} \; 
  3. Remove any empty directories

     find ../bar/ -type d -exec rmdir {} \; 
Post Undeleted by terdon
added 953 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

Assuming you have your desired files in a text file, you could do something like

while IFS= read -r file; do echo mkdir -p ${file%/*}; cp /source/"$file" /target/${file%/*}/${file##*/}; done < files.txt 

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 and target to the actual parent directories you are using. For example, to copy /foo/a/a.txt to /bar/a/a.txt, change source to foo and target to bar.


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

find /source -type d -exec mkdir -p {} /target 

That will create the directories. Once those are there, just copy the files:

while IFS= read -r file; do cp /source/"$file" /target/"$file" done 

###Update

This little script will move all the files modified after September 8. It assumes the GNU versions of find and touch. Assuming you're using Linux, that's what you will have.

#!/usr/bin/env bash ## Create a file to compare against. tmp=$(mktemp) touch -d "September 8" "$tmp" ## Define the source and target parent directories source=/path/to/source target=/path/to/target ## move to the source directory cd "$source" ## Find the files that were modified more recently than $tmp and copy them find "$source"./ -type f -newer "$tmp" -printf "%h %p\0" | while IFS= read -rd''rd '' path file; do mkdir -p "$target"/"$path" cp "$file" "$target"/$path""$path" done 

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.

Assuming you have your desired files in a text file, you could do something like

while IFS= read -r file; do echo mkdir -p ${file%/*}; cp /source/"$file" /target/${file%/*}/${file##*/}; done < files.txt 

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 and target to the actual parent directories you are using. For example, to copy /foo/a/a.txt to /bar/a/a.txt, change source to foo and target to bar.


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

find /source -type d -exec mkdir -p {} /target 

That will create the directories. Once those are there, just copy the files:

while IFS= read -r file; do cp /source/"$file" /target/"$file" done 

###Update

This little script will move all the files modified after September 8. It assumes the GNU versions of find and touch. Assuming you're using Linux, that's what you will have.

#!/usr/bin/env bash ## Create a file to compare against. tmp=$(mktemp) touch -d "September 8" "$tmp" ## Define the source and target parent directories source=/path/to/source target=/path/to/target ## Find the files that were modified more recently than $tmp and copy them find "$source" -type f -newer "$tmp" -printf "%h %p\0" | while IFS= read -rd'' path file; do mkdir -p "$target"/"$path" cp "$file" "$target"/$path" done 

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.

Assuming you have your desired files in a text file, you could do something like

while IFS= read -r file; do echo mkdir -p ${file%/*}; cp /source/"$file" /target/${file%/*}/${file##*/}; done < files.txt 

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 and target to the actual parent directories you are using. For example, to copy /foo/a/a.txt to /bar/a/a.txt, change source to foo and target to bar.


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

find /source -type d -exec mkdir -p {} /target 

That will create the directories. Once those are there, just copy the files:

while IFS= read -r file; do cp /source/"$file" /target/"$file" done 

###Update

This little script will move all the files modified after September 8. It assumes the GNU versions of find and touch. Assuming you're using Linux, that's what you will have.

#!/usr/bin/env bash ## Create a file to compare against. tmp=$(mktemp) touch -d "September 8" "$tmp" ## Define the source and target parent directories source=/path/to/source target=/path/to/target ## move to the source directory cd "$source" ## Find the files that were modified more recently than $tmp and copy them find ./ -type f -newer "$tmp" -printf "%h %p\0" | while IFS= read -rd '' path file; do mkdir -p "$target"/"$path" cp "$file" "$target"/"$path" done 

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.

Post Deleted by terdon
added 953 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719
Loading
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719
Loading