First I will describe the task I have:
- I have several disks
- I have a list of phrases (words) in a
patterns.txt
file that I use as search patterns - I must to review the disks in search of files containing these patterns
- Copy found files to a separate location on another disk
- Erase the source disk
I came up with such a solution:
Find folders and files by pattern file and save list them to the file:
grep -ril -f /home/user/patterns.txt /media/user/source-disk | tee /home/user/listfiles.txt
Find pdf files (because
grep
are skipping pdfs) by patterns file and attach them to the file:pdfgrep -ril -f /home/user/patterns.txt /media/user/source-disk | tee -a /home/user/listfiles.txt
Copy the files found to the external disk2:
cp -i --preserve=all -v `cat /home/user/listfiles.txt` /home/user/disk2
Use ShredOS to wipe source-disk
Now I encountered a problem:
In the listfiles.txt
results some folders and files contain spaces = that means that cp
reports a mistake (which seems correct)
I found help here and added one more point to work, and now looks like this:
Find folders and files containing space and replace it with underscores:
find /media/user/source-disk -iname "* *" | while read file; do mv "$file" ${file// /_}; done
Find folders and files by pattern file and save list them to the file:
grep -ril -f /home/user/patterns.txt /media/user/source-disk | tee /home/user/listfiles.txt
Find pdf files (because
grep
are skipping pdfs) by patterns file and attach them to the file:pdfgrep -ril -f /home/user/patterns.txt /media/user/source-disk | tee -a /home/user/listfiles.txt
Copy the files found to the external disk2:
cp -i --preserve=all -v `cat /home/user/listfiles.txt` /home/user/disk2
Use ShredOS to wipe source-disk
My questions:
- Does it all look correct?
- Did I forget to do something? (After erasing, it will not be possible to repeat and fix my stupidity)
- Is it better to use the script from here instead of
find -iname "* *" | while...
?
Script:
#!/bin/bash # set -o xtrace # uncomment for debugging declare weirdchars=" &\'" function normalise_and_rename() { declare -a list=("${!1}") for fileordir in "${list[@]}"; do newname="${fileordir//[${weirdchars}]/_}" [[ ! -a "$newname" ]] && \ mv "$fileordir" "$newname" || \ echo "Skipping existing file, $newname." done } declare -a dirs files while IFS= read -r -d '' dir; do dirs+=("$dir") done < <(find -type d -print0 | sort -z) normalise_and_rename dirs[@] while IFS= read -r -d '' file; do files+=("$file") done < <(find -type f -print0 | sort -z) normalise_and_rename files[@]
- advantage: can find other strange characters except spaces
- Disadvantage: I can't make the script work on any selected disk (for example, USB). It works only on my main start disk
- Can anyone help convert this script so that they work on the disk indicated by me?
- Can I do all the work in one command without problems using
&&
?
find /media/user/source-disk -iname "* *" | while read file; do mv "$file" ${file// /_}; done && grep -ril -f /home/user/patterns.txt /media/user/source-disk | tee /home/user/listfiles.txt && pdfgrep -ril -f /home/user/patterns.txt /media/user/source-disk | tee -a /home/user/listfiles.txt && cp -i --preserve=all -v `cat /home/user/listfiles.txt` /home/user/disk2