1

I need a automatic way of adding a created date and time stamp on all .pdf files within some directory like this; testfile.pdf needs to be testfile-YYY.MM.DD_HH.MM.SS.pdf Im using OSX terminal.

I have this code:

    cd "/Users/***/Documents/testmapa"
old_filename=""testfile.pdf"
new_filename=${old_filename%.*}$(stat -f %SB -t "%Y%m%d_%H%M%S" "$old_filename").${old_filename##*.}
mv "$old_filename" "$new_filename"

And this do the job but on one file, how do I apply this on all pdf files in some directory. Basically I need a file that will "on click" run some code and add this created date to a file name on all .pdf files that are in directory where the file was clicked. And a question, can that code run on subfolders? I have perl installed but don't know how to use it. I only know that it has rename function and ctime stat, but I don't know how to write that code.

2

Something like this work ok for me:

for files in /home/gv/Desktop/PythonTests/*.sh; do
old_filename=$files
old_filename_stripped=$(basename -a $files)
echo "filename full : $old_filename - file name stripped: $old_filename_stripped"
done

output:

filename full : /home/gv/Desktop/PythonTests/advfind2.sh - file name stripped: advfind2.sh
filename full : /home/gv/Desktop/PythonTests/advfind.sh - file name stripped: advfind.sh
filename full : /home/gv/Desktop/PythonTests/appslist(draft).sh - file name stripped: appslist(draft).sh
filename full : /home/gv/Desktop/PythonTests/appslist.sh - file name stripped: appslist.sh

Improved Code to include spaces and subdirs:

IFS=$'\n'
for files in $(find /home/gv/Desktop/PythonTests/ -name "*.txt" ); do
old_filename="$files"
old_filename_stripped=$(basename -a "$files")
echo "filename full : $old_filename - file name stripped: $old_filename_stripped"
done
unset IFS

Output:

filename full : /home/gv/Desktop/PythonTests/b.txt - file name stripped: b.txt
filename full : /home/gv/Desktop/PythonTests/c.txt - file name stripped: c.txt
filename full : /home/gv/Desktop/PythonTests/a ver 1.txt - file name stripped: a ver 1.txt
filename full : /home/gv/Desktop/PythonTests/d.txt - file name stripped: d.txt
filename full : /home/gv/Desktop/PythonTests/a.txt - file name stripped: a.txt
filename full : /home/gv/Desktop/PythonTests/appsfiles/a.txt - file name stripped: a.txt

PS: See the last line, it is a subdirectory.

  • Thanks for fast replay, but im getting stat: No such file or directory – ikiK Nov 24 '16 at 12:21
  • Maybe you have write the directory wrong (or maybe you are using a different shell ? I'm using bash gnu). – George Vasiliou Nov 24 '16 at 12:40
  • I will update / correct my answer. I will also include the real output from the script. – George Vasiliou Nov 24 '16 at 12:45
  • Thanks for replay! That actually is working, but looks like file names cannot have spaces in them. I will have to rename them, or is it possible to make it work with spaces. It is working when I remove them from name. Also is it possible to do the same on subfolders? – ikiK Nov 24 '16 at 15:00
  • Let me know if the improved code that i added in my answer does the job you want. Should handle spaces and subdirs correctly. – George Vasiliou Nov 24 '16 at 16:02

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.