Why file10 sorts before file2, and how to add leading zeros to file names

Your slides, scans or video frames look fine in one window, then a zip listing, an upload form or a script shows them as 1, 10, 11, 2, 3. Nothing is broken: two different kinds of sorting are disagreeing.

Two kinds of sorting

SortOrder of file1, file2, file10Used by
Character by character (lexicographic)file1, file10, file2Plain ls and sort, most programming languages' default sort, many zip tools, upload forms and cloud listings
Natural (digits read as numbers)file1, file2, file10Windows File Explorer, macOS Finder, ls -v and sort -V on Linux

Because you cannot choose how every other program sorts, the dependable answer is to make the two sorts agree. When every number has the same number of digits, character order and number order are the same.

How many digits?

Use enough digits for the largest number you will ever reach: 2 for up to 99 files, 3 for up to 999, 4 for up to 9,999. Padding further costs nothing, but going back later to add one more digit means renaming everything again.

Pad the numbers in the browser

  1. Load the names into the batch rename tool (choose the files or paste a list).
  2. Turn on natural sort so page2 stays before page10.
  3. Choose numbering, set the mode to replace the name or add a prefix, start at 1 and set digits to 3. Add a prefix such as page- if you like.
  4. Check the before/after table, then copy the bash or PowerShell script and run it in the folder.

Or pad them in a terminal

bash, for names like file7.jpg:

for f in file*.jpg; do
  n=${f#file}; n=${n%.jpg}
  case $n in (*[!0-9]*|'') continue;; esac
  mv -n -- "$f" "$(printf 'file%03d.jpg' "$((10#$n))")"
done

PowerShell:

Get-ChildItem -File file*.jpg | ForEach-Object {
  if ($_.BaseName -match '^file(\d+)$') {
    Rename-Item -LiteralPath $_.FullName -NewName ('file{0:D3}{1}' -f [int]$Matches[1], $_.Extension)
  }
}

The 10# in bash stops a number like 08 being read as octal. To list files in number order without renaming, use ls -v or sort -V.

Related

Number files in the order you choose · How to batch rename files safely (without uploading them) · Why a batch rename overwrites or skips files (name collisions) · Renaming a file when only the upper/lower case changes · Windows names you cannot use: CON, NUL, AUX, COM1 · Remove spaces and special characters from file names · Batch rename tool (names only, in your browser) · Compress many photos at once · Invisible characters in text

Questions

Why does file10 come before file2?

Most programs compare names one character at a time. The first different character is 1 versus 2, and 1 comes first, so file10 sorts before file2.

Why do my files look right in Explorer or Finder but wrong elsewhere?

Explorer and Finder use a natural sort that reads numbers as numbers. Upload forms, zip tools, scripts and many web apps use plain character order.

How do I add leading zeros to file names?

Give every number the same width, for example 001 to 999. The batch rename tool's numbering option does this after a natural sort, or use printf '%03d' in bash or {0:D3} in PowerShell.

How many digits should I use?

Enough for your largest number: two up to 99, three up to 999, four up to 9,999.

Can I sort by number without renaming?

On Linux use ls -v or sort -V. Elsewhere it depends on the app, which is why padded names are the reliable fix.

Published · updated .

Try it

Related guides

Newest guides