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.pdfI'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.
Renaming a file the moment it appears in a folder
files;scripting;rename;date
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 suffixprefix=some_prefixsuffix=pdfinotifywait --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    doneI'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.