2

Here's the context :

on a samba server, I have some folders (that we'll call A,B,C,D ) which are supposed to receive files from a network scanner. The scanner renders a PDF file named like this :

YYYYMMDDHHmmss.pdf

(Year, Month, Day, Hour, minute, seconds)

I need those PDFs to be renamed the moment they appear in the folder, or within the minute (I'm thinking about crontab).

the renaming must be Something like

"[prefix_specific_to_the_folder]_YYYY-MM-DD.pdf"

I've seen that "date +%F" does what I want for the timestamp, and I just have to manually set my prefix in the script.

I have the algorithm in mind, it must be Something like

 "-read file.pdf
    -if the name of the file doesn't have [prefix] 
     -then mv file.pdf [prefix]_[date].pdf
    -else nevermind about that file."

It's really hard for me to find the correct syntax for this.

I would prefer to retrieve the system timestamp of file creation and rename the file with it instead of using the filename generated by the scanner.

5

Here is a solution built around the inotifywait utility. (You could use incron too, but you'd still need code similar to this.) Run this at boot time, for example from /etc/rc.local.

#!/bin/bash
#
cd /path/to/samba/folder

# Rename received files to this prefix and suffix
prefix="some_prefix"
suffix="pdf"

inotifywait --event close_write --format "%f" --monitor . |
    while IFS= read -r file
    do
        # Seconds since the epoch
        s=$(stat -c "%Y" "$file")

        # Convert to YYYY-MM-DD
        ymd="$(date --date "@$s" +'%Y-%m-%d')"

        # Rename the file. Mind the assumed extension
        mv -f "$file" "${prefix}_$ymd.$suffix"
    done

I'm not sure what you expect to happen if there are two or more files created on the same day. At the moment the most recently arrived (and processed) will replace any earlier file from the same date.

  • I think i'll keep that script, easier to read and manipulate ! Just one thing i didn't think about, if there are more than one scan per day, it erases the file, so i will set hours, minutes and seconds. Thank you very much ! – Slartibartfast Feb 2 '16 at 16:36
0

I think cron is a good idea! Here some input for your script:

#!/bin/bash
smbdir="/path/to/samba/folder"
smbsubdirs=(A B C D)
smbprefix="YOUR_PREFIX"  # for example

for dirname in ${smbsubdirs[@]}; do
  dir=$smbdir/$dirname && [ -d "$dir" ] || continue
  while read -r file; do
    if [[ "$(basename $file)" =~ ^([0-9]+[.]pdf)$ ]];
    then
      date=$(date +%Y%m%d%S -d $(stat -c%w $file))
      new="$dir/${smbprefix}_$date.pdf"
      echo "mv \"$file\" \"$new\""
      # mv "$file" "$new" # commented for testing
    fi
  done < <(find "$dir" -cnewer "$dir" -type f -iname *.pdf)
  touch $dir
done
exit 0

I would only recommend this solution if you do not have inotify-tools available on your system.

cheers dom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.