Results 1 to 2 of 2

Thread: Bash script for renaming files to consecutive numbers in multiple directories

  1. #1
    Join Date
    Oct 2010
    Beans
    32

    Bash script for renaming files to consecutive numbers in multiple directories

    hello,
    i have a problem very similar to this dude:
    https://stackoverflow.com/questions/...le-directories

    but my problem is slightly different
    the extensions of my files are not all jpg. i have various file types and i want to keep that extensions.
    so, i have changed the code to this:
    Code:
    #!/bin/bash
    for dir in $(find -type d \( ! -name '.*' \)) 
    do 
      i=1; 
      ext=
      for file in $(find "$dir" -name '*.*')
      do 
         ext="${i#*.}"
         a=$(printf "%02d" $i)
         new=${file%/*}
         echo "mv $file $new/$a.$ext"
         let i=i+1
      done 
    done
    got some inspiration from here too: https://ubuntuforums.org/showthread.php?t=1355021


    so, the input would be like this:
    Code:
    Folder1
     -> AD01
       -> DSC123.jpg
       -> DSC124.php
     -> AD02
       -> DSC234.png
       -> DSC1455.exe
     -> AD03
      ->...
     -> AD04
      ->...
     ->...
    Folder2
     ->...
    ...
    and the output would be like this
    Fo1
     -> Pg1
       -> Fo1_Pg1_1.jpg
       -> Fo1_Pg1_2.php
     -> Pg2
       -> Fo1_Pg2_1.png
       -> Fo1_Pg2_2.exe
     -> Pg3
      ->...
     -> Pg4
      ->...
     ->...
    Fo2
     ->...
    ...
    thanks
    Last edited by lilipop; December 21st, 2019 at 04:57 PM.

  2. #2
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    22,490
    Distro
    Ubuntu

    Re: Bash script for renaming files to consecutive numbers in multiple directories

    I would break this down into 2 separate steps and make a script for each that generates a script with lots of mv and rename lines.

    *.* is an MS-Dos thing because extensions are treated differently in that OS. Unix doesn't care. * is the same. File globbing is a subset of the regex language.

    If I had to do something like this every day for 500 directories and 20000 files, I wouldn't use bash. I'd use perl.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •