DylanDog
DylanDog

Reputation: 109

Bash - Adapting script to rename files with numbers

I have this script

rename_files() {
  title="${1##*${2} - }"
  for filename in "$1/"*.*; do
    case "${filename##*.}" in
      ext1|ext2|ext3)
        mkdir "/User/Downloaded/${title}"
        new_path="/User/Downloaded/${title}/${title}.${filename##*.}"
        echo "moving $filename -> $new_path"
        mv "$filename" "$new_path"
        ;;
    esac
  done
}

rename_category() {
  for path in "/User/Downloads/${1}"*; do
    rename_files "$path" "$1"
  done
}

rename_category CAT1
rename_category CAT2

That do this

Before

.
├── Downloads
│   ├── CAT1 - Something ── File.ext1
│   └── CAT2 - SomethingElse ── OtherFile.ext1
├── Downloaded
    ├── CAT1
    ├── CAT2

After

.
├── Downloads
│   ├── CAT1 - Something
│   └── CAT2 - SomethingElse
├── Downloaded
    ├── CAT1 ──  Something ── Something.ext1
    ├── CAT2 ──  SomethingElse ── SomethingElse.ext1

What I'm trying to do is that if only there's more that one file with the same extension, then these files will be renamed adding _1, etc.

Before

.
├── Downloads
│   ├── CAT1 - Something ├── File.ext1
                         ├── ExampleFile.ext1
│   └── CAT2 - SomethingElse ── OtherFile.ext1
├── Downloaded
    ├── CAT1
    ├── CAT2

After

.
├── Downloads
│   ├── CAT1 - Something
│   └── CAT2 - SomethingElse
├── Downloaded
    ├── CAT1 ──  Something ── Something_1.ext1
                           ── Something_2.ext1
    ├── CAT2 ──  SomethingElse ── SomethingElse.ext1

EDIT:

#!/bin/bash

rename_files() {
   title="${1##*${2} - }"
   for filename in "$1/"*.*; do
   case "${filename##*.}" in
     doc|ext|ext)
       mkdir "/User/Downloaded/${title}"
       new_path="/User/Downloaded/${title}/${title}.${filename##*.}"
       let "iters=1"
       while [ -f $new_path ] ; do
          $new_path=$new_path"$iters"
          let "iters++"
       done
       echo "moving $filename -> $new_path"
       mv "$filename" "$new_path"
       ;;
   esac
   done
}

rename_category() {
  for path in "/User/Downloads/${1}"*; do
    rename_files "$path" "$1"
  done
}

rename_category CAT1

EDIT2:

There are two .doc files in /User/Downloads/DOC - TEST

/User/Downloads/DOC - TEST/Sample-doc-file-100kb.doc
                          /Sample-doc-file-200kb.doc

What I want is

/User/Downloaded/TEST/TEST_1.doc
                     /TEST_2.doc

This is the output log

chmod +x /User/TEST.sh
sh /User/TEST.sh
moving /User/Downloads/DOC - TEST/Sample-doc-file-100kb.doc -> /User/Downloaded/TEST/TEST.doc
mkdir: /User/Downloaded/TEST: File exists
/User/TEST.sh: line 12: /User/Downloaded/TEST/TEST.doc=/User/Downloaded/TEST/TEST.doc1: No such file or directory
/User/TEST.sh: line 12: /User/Downloaded/TEST/TEST.doc=/User/Downloaded/TEST/TEST.doc2: No such file or directory

etc.

Updated output log

sh /User/Script.sh 
moving /User/Downloads/DOC - TEST/Sample-doc-file-100kb.doc -> /User/Downloaded/TEST/TEST.doc
mv: rename {/User/Downloads/DOC - TEST/Sample-doc-file-100kb.doc} to {/User/Downloaded/TEST/TEST.doc}: No such file or directory
mkdir: /User/Downloaded/TEST: File exists
moving /User/Downloads/DOC - TEST/Sample-doc-file-200kb.doc -> /User/Downloaded/TEST/TEST.doc
mv: rename {/User/Downloads/DOC - TEST/Sample-doc-file-200kb.doc} to {/User/Downloaded/TEST/TEST.doc}: No such file or directory

OUTPUT UPDATED:

Here is my initial situation

User/Downloads/DOC - TEST/SampleDoc.doc
                         /OtherSampleDoc.doc
                         /OtherDoc.doc

User/Downloaded/

After the script I only obtain

User/Downloads/DOC - TEST/OtherSampleDoc.doc
                         /OtherDoc.doc

User/Downloaded/TEST/TEST.doc

But want I want would be

User/Downloads/DOC - TEST

User/Downloaded/TEST/TEST_1.doc
                    /TEST_2.doc
                    /TEST_3.doc

And this is the output

sh /User/Script.sh 
moving /User/Downloads/DOC - TEST/OtherDoc.doc -> /User/Downloaded/TEST/TEST.doc
mkdir: /User/Downloaded/TEST: File exists
/User/Script.sh: line 12: /User/Downloaded/TEST/TEST.doc=/User/Downloaded/TEST/TEST.doc1: No such file or directory
/User/Script.sh: line 12: /User/Downloaded/TEST/TEST.doc=/User/Downloaded/TEST/TEST.doc2: No such file or directory
/User/Script.sh: line 12: /User/Downloaded/TEST/TEST.doc=/User/Downloaded/TEST/TEST.doc3: No such file or directory
etc.

Upvotes: 0

Views: 110

Answers (1)

hansod1
hansod1

Reputation: 324

rename_files() {
   title="${1##*${2} - }"
   for filename in "$1/"*.*; do
   case "${filename##*.}" in
     ext1|ext2|ext3)
       mkdir "/User/Downloaded/${title}"
       new_path="/User/Downloaded/${title}/${title}.${filename##*.}"
       let "iters=1"
       while [ -f $new_path ] ; do
          new_path=$new_path"$iters"
          let "iters++"
       done
       echo "moving $filename -> $new_path"
       mv "${filename}" "${new_path}"
       ;;
   esac
   done
}

Upvotes: 1

Related Questions