Skip to content

Instantly share code, notes, and snippets.

@simonhaenisch
Last active August 16, 2016 12:56
Show Gist options
  • Save simonhaenisch/7259eb5070ba6f825b34f61e56bc6815 to your computer and use it in GitHub Desktop.
Save simonhaenisch/7259eb5070ba6f825b34f61e56bc6815 to your computer and use it in GitHub Desktop.
Move files with same name from sub-directories
#! /bin/bash
# mv e.g. /dir/00[1|2|3]/file.ext -> /dir/file-[1...3].ext
idx=1 # start index for new file-names (use 0 or zero-based index)
ext=".ext" # file extension (can be a wildcard)
from="/dir/*" # where to look for files
to="." # target of mv command, e.g. "dir/"
for file in $from/*$ext; do
mv "$file" `printf $to/$(basename $file $ext)-%03d$ext $idx`
idx=$((idx+1))
done
# one-liner
# for file in $from/*$ext; do mv "$file" `printf $to/$(basename $file $ext)-%03d$ext $idx` && idx=$((idx+1)); done
# clear variables
unset idx ext from to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment