I already tried to find answer here, but didn't find. I am sorry my bad english, because it is a bit hard for me to speak english. I also have almost none bash scripting skills, so my script may look very bad. It's jus made from examples what I could find from internet, but now I am against wall with this, so I need help from you guys!
What I need from this script is:
- Find all pdf files recursively (Simple Machines Forum hashes attachments, so that is why extension is .dat)
- Identify pdf type from .dat files
- Then I need to use ps2pdf program to optimize all new (last 24h) pdf files
- I also need to keep original timestamp from optimized pdf files
- That "24h-pdf-compress-"
date +"%d-%m-%Y"
".txt" file is only for logging things, so I can check later it is working.
Smf- forum attachments dir structure is following:
Under attachment folder there is folder by year (2020).
Under every year there is folder for month (04 = April).
Under every month are the all attachment files from that month.
Every file (jpg,png,pdf) have the same .dat extension.
/var/www/foorumi/attachments/2020/04/all-files-from-april.dat
My script:
#!/bin/bash
cd /var/www/foorumi/attachments
find . -name '*.dat' -mtime -1 | xargs file -i | grep 'pdf' | cut -d: -f1 > "24h-pdf-compress-"`date +"%d-%m-%Y"`".txt"
find . -name '*.dat' -mtime -1 | xargs file -i | grep 'pdf' | cut -d: -f1 | while read -r file
do
touch -r "$file" "dummy_file"
ps2pdf "$file" "new_$file" # PROBLEM
rm "$file"
mv "new_$file" "$file" # PROBLEM
touch -r "dummy_file" "$file"
rm dummy_file
done
mv "24h-pdf-compress-"`date +"%d-%m-%Y"`".txt" /root/24h_pdf_compress_log
find . -iname '*.dat' -user root -exec chown www-data:www-data {} \;
exit 0
Ok, problem is following.
When I run find . -name '*.dat' -mtime -1 | xargs file -i | grep 'pdf' | cut -d: -f1
it prints files like ./04/somepdfattachment.dat
, so when script runs ps2pdf "$file" "new_$file"
it tries to make new file name like new_./04/somepdfattachment.dat
..it doesn't sound right.
Next error comes when script tries to rename file mv "new_$file" "$file"
, because now it tries to rename new_./04/somepdfattachment.dat
file back it's original filename.
I hope you understand what I try to tell you. I can provide more info if needed.
Thanks in advance!
UPDATE! Like @pLumo suggested, I modified script and it seems to work now. I hope I understood suggestions correctly..
Simple Machines forum attachments are named following way:
403_57066cef00fb1d57137b5613f076d254e89b88bc.dat
"403" = are running number for attachments, next is 404 and next 405 and so on..
"57066cef00..." = is random hash
".dat"= all attachments extensions (jpg,png,pdf) are renamed as .dat after upload.
Updated script:
...
do
touch -r "$file" "dummy_file"
newname="$(dirname "$file")/new_$(basename "$file")"
ps2pdf "$file" "$newname" || continue
rm "$file"
mv "$newname" "$file"
touch -r "dummy_file" "$file"
rm dummy_file
done
...
find
. Are all of the files in/var/www/foorumi/attachments/2020/04/
? If so cd into it and then useprintf '%s\n' *
to return the filenames without the directory appended. Also, add the expected output to your question so that there is a better idea of what you are trying to do. – Nasir Riley Apr 02 '20 at 06:14printf
, file reads multiple files directly:file -i0 *
. btw,find
, unlikels
can be used safely. – pLumo Apr 02 '20 at 07:08find
, either: https://unix.stackexchange.com/questions/321697/why-is-looping-over-finds-output-bad-practice Usingfind -exec
is okay, but not for what HendriXXX is trying to do. – Nasir Riley Apr 02 '20 at 07:35find
done right is totally fine, but you may be talking (and linked) looping. – pLumo Apr 02 '20 at 08:02