{"id":421,"date":"2023-07-05T12:52:36","date_gmt":"2023-07-05T10:52:36","guid":{"rendered":"https:\/\/www.netlabor.it\/?p=421"},"modified":"2026-11-24T02:53:57","modified_gmt":"2026-11-24T01:53:57","slug":"riordinare-foto-e-video-da-terminale-con-uno-script-bash-ed-exiftool","status":"publish","type":"post","link":"https:\/\/www.netlabor.it\/en\/riordinare-foto-e-video-da-terminale-con-uno-script-bash-ed-exiftool\/","title":{"rendered":"Organizing Photos and Videos from the Terminal with a Bash Script and ExifTool"},"content":{"rendered":"\n<div class=\"wp-block-bogo-language-switcher\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">When exporting photos and videos from smartphones, cameras, or older backups, you almost always end up with a mix of inconsistent filenames (<code>IMG_20200612.jpg<\/code>, <code>DSC04512.ARW<\/code>, <code>VID-20230410-WA0002.mp4<\/code>) that make clean chronological sorting difficult.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Relying on filesystem modification times (<code>mtime<\/code>) is risky: simply copying files to an external drive, downloading them from cloud storage, or moving them between folders can alter their timestamps. The only reliable source is the <strong>embedded metadata<\/strong> (EXIF and QuickTime\/MP4) written when the photo was taken or the video was recorded.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This Bash script automatically renames photos and videos to the <strong><code>YYYYMMDD_HHMMSS.ext<\/code><\/strong> format (for example, <code>20200612_205732.jpg<\/code>), preserving the original file extension and protecting files against accidental overwrites.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What the Script Does<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Controlled recursion (<code>-r<\/code> \/ <code>--recursive<\/code>):<\/strong> Without <code>-r<\/code>, it only processes the top level of each target directory (<code>-maxdepth 1<\/code>); with <code>-r<\/code>, it descends into subdirectories. If no paths are provided, it defaults to the current directory.<\/li>\n\n\n\n<li><strong>Timestamp tag chain:<\/strong> Queries <code>exiftool<\/code> in order for <code>DateTimeOriginal<\/code>, <code>CreateDate<\/code>, and <code>MediaCreateDate<\/code>, picking the first valid value (helpful for both still photos and many video formats).<\/li>\n\n\n\n<li><strong>Strict format validation:<\/strong> Accepts only timestamps matching <code>YYYYMMDD_HHMMSS<\/code> (<code>^[0-9]{8}_[0-9]{6}$<\/code>), ignoring files without metadata or with invalid\/zeroed values, and skips files that already follow the target naming scheme.<\/li>\n\n\n\n<li><strong>Automatic collision handling:<\/strong> If another file already has the same timestamp within the same second (such as burst shots), it appends a sequential suffix (<code>20200612_205732_1.jpg<\/code>, <code>_2.jpg<\/code>, and so on) and uses <code>mv -n<\/code> to avoid overwriting existing files.<\/li>\n\n\n\n<li><strong>Safe preview mode (<code>-d<\/code> \/ <code>--dry-run<\/code>):<\/strong> Shows what would be renamed without modifying any files on disk.<\/li>\n\n\n\n<li><strong>Italian or English messages (<code>-l IT|EN<\/code>):<\/strong> Detects the system locale (<code>LC_ALL<\/code>, <code>LC_MESSAGES<\/code>, <code>LANG<\/code>) and lets you force the language for both <code>--help<\/code> and status\/error messages.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Installation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The only external dependency required is <strong>ExifTool<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Debian \/ Ubuntu \/ derivatives:<\/strong>Bash<code>sudo apt install libimage-exiftool-perl<\/code><\/li>\n\n\n\n<li><strong>macOS (Homebrew):<\/strong>Bash<code>brew install exiftool<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Save the script\u2014for instance at <code>~\/.local\/bin\/exif-rename<\/code>\u2014and make it executable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod +x ~\/.local\/bin\/exif-rename\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">The Complete Script<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env bash\nset -u\n\nusage() {\n  if &#091; \"$MSG_LANG\" = \"it\" ]; then\n    cat &lt;&lt;EOF\nUso: $0 &#091;OPZIONI] &#091;PERCORSO...]\n\nRinomina foto e video in base alla data di scatto (metadati EXIF).\nFormato: AAAAMMGG_HHMMSS.ext (es. 20200612_205732.jpg).\nI file senza metadati utili vengono saltati.\n\nOpzioni:\n  -r, --recursive    cerca ricorsivamente nelle directory\n  -d, --dry-run      mostra cosa verrebbe fatto senza rinominare\n  -l, --lang IT|EN   scegli la lingua dei messaggi (italiano o inglese); di default usa la lingua di sistema\n  -h, --help         mostra questo aiuto\n\nSe non indichi percorsi, usa la directory corrente.\nEOF\n  else\n    cat &lt;&lt;EOF\nUsage: $0 &#091;OPTIONS] &#091;PATH...]\n\nRename photo and video files according to capture date (EXIF).\nFormat: YYYYMMDD_HHMMSS.ext (e.g., 20200612_205732.jpg).\nFiles without useful metadata are skipped.\n\nOptions:\n  -r, --recursive    recurse into subdirectories\n  -d, --dry-run      show what would be renamed without actually renaming\n  -l, --lang IT|EN   choose language for messages (Italian or English); defaults to system locale\n  -h, --help         display this help\n\nIf no paths given, current directory is used.\nEOF\n  fi\n}\n\nlang=\"${LC_ALL:-${LC_MESSAGES:-${LANG:-}}}\"\ncase \"$lang\" in\n  it*|IT*) MSG_LANG=it ;;\n  en*|EN*) MSG_LANG=en ;;\n  *)       MSG_LANG=auto ;;\nesac\n\nhelp_requested=0\nrecursive=0\ndry_run=0\nlang_opt=\"\"\npaths=()\n\nwhile &#091; $# -gt 0 ]; do\n  case \"$1\" in\n    -h|--help) help_requested=1; shift ;;\n    -r|--recursive) recursive=1; shift ;;\n    -d|--dry-run) dry_run=1; shift ;;\n    -l|--lang)\n      if &#091; $# -lt 2 ]; then\n        echo \"error: -l requires an argument (IT or EN)\" &gt;&amp;2\n        exit 2\n      fi\n      lang_opt=\"$2\"; shift 2\n      ;;\n    --) shift; paths=(\"$@\"); break ;;\n    -*) echo \"error: unknown option: $1\" &gt;&amp;2; exit 2 ;;\n    *) paths+=(\"$1\"); shift ;;\n  esac\ndone\n\nif &#091; -n \"$lang_opt\" ]; then\n  case \"$lang_opt\" in\n    it|IT) MSG_LANG=it ;;\n    en|EN) MSG_LANG=en ;;\n    *) echo \"error: invalid language: $lang_opt (use IT or EN)\" &gt;&amp;2; exit 2 ;;\n  esac\nfi\n\nif &#091; \"$MSG_LANG\" = \"it\" ]; then\n  MSG_EXIFTOOL=\"errore: exiftool non trovato. Installalo (es. sudo apt install libimage-exiftool-perl)\"\n  MSG_SKIP_NO_META=\"saltato (nessun metadato)\"\n  MSG_RENAMED=\"rinominato\"\n  MSG_WOULD_RENAME=\"rinominerei\"\nelse\n  MSG_EXIFTOOL=\"error: exiftool not found. Install it (e.g. sudo apt install libimage-exiftool-perl)\"\n  MSG_SKIP_NO_META=\"skipped (no metadata)\"\n  MSG_RENAMED=\"renamed\"\n  MSG_WOULD_RENAME=\"would rename\"\nfi\n\nif &#091; \"${help_requested:-0}\" -eq 1 ]; then\n  usage\n  exit 0\nfi\n\nif ! command -v exiftool &gt;\/dev\/null 2&gt;&amp;1; then\n  echo \"$MSG_EXIFTOOL\" &gt;&amp;2\n  exit 1\nfi\n\nfind_depth=()\nif &#091; \"$recursive\" -eq 0 ]; then\n  find_depth=(-maxdepth 1)\nfi\n\nsearch_roots=(\".\")\nif &#091; ${#paths&#091;@]} -gt 0 ]; then\n  search_roots=(\"${paths&#091;@]}\")\nfi\n\nfiles=()\nfor p in \"${search_roots&#091;@]}\"; do\n  while IFS= read -r -d '' f; do\n    files+=(\"$f\")\n  done &lt; &lt;(find \"$p\" \"${find_depth&#091;@]}\" -type f ! -name '.*' -print0)\ndone\n\nfor f in \"${files&#091;@]}\"; do\n  base=$(basename -- \"$f\")\n  case \"$base\" in\n    *.*) ext=\"${base##*.}\" ;;\n    *) continue ;;\n  esac\n\n  dt=$(exiftool -d \"%Y%m%d_%H%M%S\" -s3 -DateTimeOriginal -CreateDate -MediaCreateDate \"$f\" 2&gt;\/dev\/null | head -n1) || true\n\n  # Validate date: must be YYYYMMDD_HHMMSS (8 digits + _ + 6 digits)\n  if &#091;&#091; ! \"$dt\" =~ ^&#091;0-9]{8}_&#091;0-9]{6}$ ]]; then\n    echo \"$MSG_SKIP_NO_META: $f\" &gt;&amp;2\n    continue\n  fi\n\n  # Skip if already correctly named\n  if &#091; \"$base\" = \"${dt}.${ext}\" ]; then\n    continue\n  fi\n\n  dir=$(dirname -- \"$f\")\n  new=\"$dir\/${dt}.${ext}\"\n\n  if &#091; -e \"$new\" ]; then\n    j=1\n    while &#091; -e \"$dir\/${dt}_${j}.${ext}\" ]; do\n      j=$((j + 1))\n    done\n    new=\"$dir\/${dt}_${j}.${ext}\"\n  fi\n\n  if &#091; \"$dry_run\" -eq 1 ]; then\n    echo \"$MSG_WOULD_RENAME: $f -&gt; $new\"\n  else\n    mv -n -- \"$f\" \"$new\"\n    echo \"$MSG_RENAMED: $f -&gt; $new\"\n  fi\ndone\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage Examples<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Preview what would be renamed in the current directory without making any changes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exif-rename --dry-run\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Recursively rename files across one or more paths:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exif-rename -r ~\/Pictures\/Archive ~\/Backups\/Phone\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Show help in English or force messages into a specific language:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exif-rename -l EN --help\nexif-rename -l IT -d .\/photos\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When exporting photos and videos from smartphones, cameras, or older backups, you almost always end up with a mix of inconsistent filenames (IMG_20200612.jpg, DSC04512.ARW, VID-20230410-WA0002.mp4) that make clean chronological sorting difficult. Relying on filesystem modification times (mtime) is risky: simply copying files to an external drive, downloading them from cloud storage, or moving them between [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"full-width-page-template.php","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/www.netlabor.it\/?p=419","footnotes":""},"categories":[6,7],"tags":[8,9],"class_list":["post-421","post","type-post","status-publish","format-standard","hentry","category-informatica","category-programmazione","tag-informatica","tag-programmazione","en-US","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/posts\/421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/comments?post=421"}],"version-history":[{"count":1,"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/posts\/421\/revisions"}],"predecessor-version":[{"id":422,"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/posts\/421\/revisions\/422"}],"wp:attachment":[{"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/media?parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/categories?post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/tags?post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}