datestamp-files.sh 581 B

123456789101112131415161718
  1. #!/bin/bash
  2. # for all files in one directory, rename the file adding it's mtime to the filename
  3. # so foo.txt becomes 2023-12-04-foo.txt
  4. DIR=${1:-.}
  5. # Use find to get all files in the directory
  6. find $DIR -maxdepth 1 -type f | while read -r file
  7. do
  8. # Use stat to get the modification time of the file
  9. mtime=$(stat -c %y "$file")
  10. # Format the modification time to YYYY-MM-DD
  11. formatted_mtime=$(date -d "$mtime" "+%Y-%m-%d-%H-%M-%S")
  12. # Rename the file by adding the modification time to the front of the filename
  13. mv "$file" "$DIR/${formatted_mtime}-$(basename "$file")"
  14. done