Skip to content
System

Change the file extensions in a folder on Linux

Avatar illustré de Julien Macé 4 min read
Lot de fichiers dans un dossier avec une destination existante protégée
Illustration conceptuelle générée : changement des noms sans conversion des fichiers.

A batch of .txt files needs to become .text? An overly broad selection or an already-used name can cause an incorrect rename. This Bash method handles only regular files in the current directory: it first displays the planned names, then runs a separate command that refuses occupied destinations.

Before you begin, move to the desired directory and check the path:

pwd
ls -ld .

If an application is using these names, stop it or work on a copy. Renaming changes the name, not the contents. A .txt file renamed to .text is not converted, and references using the old name may stop working.

Lot de fichiers dans un dossier avec une destination existante protégée
Illustration conceptuelle générée : changement des noms sans conversion des fichiers.

Preview the new names without changing anything

The loop below targets names ending exactly in .txt, with this case. It does not descend into subdirectories. By default, Bash does not select hidden files with *. Symbolic links are excluded, including those pointing to a regular file.

(
  set +f
  unset GLOBIGNORE
  shopt -u failglob nocaseglob dotglob
  shopt -s nullglob
  # To include hidden files: shopt -s dotglob
  files=(./*.txt)
  count=0
  for src in "${files[@]}"; do
    [[ -f "$src" && ! -L "$src" ]] || continue
    ((count+=1))
    dst=${src%.txt}.text
    if [[ -e "$dst" || -L "$dst" ]]; then
      printf 'COLLISION, skipped: %q -> %q\n' "$src" "$dst"
      continue
    fi
    printf 'PLANNED: %q -> %q\n' "$src" "$dst"
  done
  ((count > 0)) || printf 'No regular .txt files selected.\n'
)

PLANNED indicates a pair of names, not an operation that has been performed. COLLISION, skipped means an entry already occupies the destination. If no regular files match, the block displays the message provided for that case.

The expression ${src%.txt} removes the .txt suffix from the end of the name. Thus, archive.tar.txt becomes archive.tar.text: the rest of the name is preserved.

Rename the batch while refusing occupied destinations

Read the entire preview before running this second block. It uses the same selection and filters. In the event of a collision, it leaves both names in place and moves on to the next file.

(
  set +f
  unset GLOBIGNORE
  shopt -u failglob nocaseglob dotglob
  shopt -s nullglob
  # To include hidden files: shopt -s dotglob
  files=(./*.txt)
  count=0
  status=0
  for src in "${files[@]}"; do
    [[ -f "$src" && ! -L "$src" ]] || continue
    ((count+=1))
    dst=${src%.txt}.text
    if [[ -e "$dst" || -L "$dst" ]]; then
      printf 'COLLISION, skipped: %q -> %q\n' "$src" "$dst"
      continue
    fi
    if command mv -nT -- "$src" "$dst"; then
      if [[ ! -e "$src" && ! -L "$src" && -f "$dst" && ! -L "$dst" ]]; then
        printf 'RENAMED: %q -> %q\n' "$src" "$dst"
      else
        printf 'CHECK REQUIRED, source still present or unexpected destination: %q -> %q\n' "$src" "$dst" >&2
        status=1
      fi
    else
      printf 'FAILED: %q -> %q\n' "$src" "$dst" >&2
      status=1
    fi
  done
  ((count > 0)) || printf 'No regular .txt files selected.\n'
  exit "$status"
)

-n tells GNU mv not to replace an existing destination. -T prevents a destination that is a directory from being interpreted as a target directory; -- marks the end of the options. The tests before and after the move provide a diagnostic because the exit status of mv -n varies between versions. A zero final exit status therefore does not mean every file was renamed: collisions are skipped and the loop continues.

The GNU documentation notes that -n is deprecated because of differences in exit status across platforms. This command is intended for Bash and GNU Coreutils on Linux; do not use this block as-is with sh, zsh, BusyBox, or BSD. Do not combine -n with -i or -f, because only the last of these options takes effect.

Bash arrays, quotes, and the ./ prefix keep spaces, tabs, newlines, glob characters, and names beginning with a hyphen in separate arguments. printf %q makes special characters visible in the output. This output is a preview calculated for your directory, not a universal test result.

Include hidden files if needed

Hidden files are excluded by default. To include them, uncomment shopt -s dotglob in both blocks, just before files=(./*.txt). The setting remains limited to the subshell in parentheses. Case matching remains exact: .TXT is not selected.

Both blocks must use the same selection. If you adapt the example to a different suffix, change the pattern, the suffix being removed, and the destination in both blocks. Do not change only *.txt: the transformation also uses ${src%.txt} and .text.

Check the names after renaming

The loop can produce RENAMED, COLLISION, skipped, FAILED, or CHECK REQUIRED. After it runs, check a representative pair using its exact name:

ls -ld -- './rapport final.text'
[[ ! -e './rapport final.txt' && ! -L './rapport final.txt' ]] && printf 'Old name absent\n'

This check verifies the names and the type of the entry. By itself, it does not prove that the contents are unchanged if another process modifies the directory at the same time.

Checking that the destination is free and renaming are two separate steps. The preview does not reserve any names; mv -nT refuses to replace a destination when the action is performed, but the batch is neither an atomic transaction nor a lock. Stop concurrent writes before running the loop. If another process could modify or swap the paths during the operation, this method is not suitable. A batch may stop partway through after an error; no automatic rollback is provided.

For the general syntax of mv, see our guide to renaming a file with mv. If your question concerns the contents rather than the name, see how to identify the actual format with file. The GNU Bash documentation on filename expansion, parameter expansion, conditional tests, and the GNU mv command describe the behaviors used here.

Share Facebook X WhatsApp LinkedIn Pinterest Reddit Email