Last active
August 16, 2016 12:56
-
-
Save simonhaenisch/7259eb5070ba6f825b34f61e56bc6815 to your computer and use it in GitHub Desktop.
Move files with same name from sub-directories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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