0

I need to rename a bunch of files (about 167k), and only part of the title in each file. From: Aaaa.bb - 2 tag tag_tag 9tag Aaaa.bb - 125 tag_tag 9tag Aaaa.bb - 3567 tag 9tag To: Aaaa.bb - 000002 tag tag_tag 9tag Aaaa.bb - 000125 tag_tag 9tag Aaaa.bb - 003567 tag 9tag Everything is in external HDD Is there any tool or script that can help me in this situation? Also forgot to mention that im still new in ubuntu and all that tricky stuff, so i will ask to simplify the answers

Ceslovas
  • 37
  • 6

4 Answers4

1

I would reccomend Python and using regex. Here is an example of a script I made which can cnahge all files in a folder, and subfolders.

directoryChosen = (sys.argv[1])

print directoryChosen + "   thi is inside dollartohash"
if os.path.isdir(directoryChosen):
    for n in os.listdir(directoryChosen):
        if not n.startswith('.'):

            newname =  n.replace('$', '#')
            print newname
            if newname != n:
                path = os.path.join(directoryChosen, n)
                print path + "    this is path"
                target = os.path.join(directoryChosen, newname)
                print target + "   this is target"
                os.rename(path, target)

    newdir = directoryChosen.replace('$', '#')
    print newdir
    if directoryChosen != newdir :
         os.rename(directoryChosen, newdir)

As you can see it takes the directory as an argument, and changes a symbol to another symbol. Im sure you can modify the replace part.

Edziu Eames
  • 180
  • 9
1

Here's my take at it,using bash, awk, and mv .

If we look at the file name from awk standpoint, filename is just a string with space separated fields, and of particular interest is field $3 which has to be padded with zeros. The script bellow does exactly that. Make sure you place the script and run it in the same directory as the files that you want to rename. Also , make sure it is executable with chmod 755 renamerScript.sh

Demo

testdir:$ ls
Aaaa.bb - 125 tag tag_tag 9tag  Aaaa.bb - 2 tag tag_tag 9tag  Aaaa.bb - 4207 tag tag_tag 9tag  BACKUP/  renamerScript.sh*

testdir:$ ./renamerScript.sh                                                                                                                          
mkdir: cannot create directory ‘BACKUP’: File exists
cp: omitting directory ‘BACKUP’
renaming Aaaa.bb - 125 tag tag_tag 9tag to Aaaa.bb - 000125 tag tag_tag 9tag 
renaming Aaaa.bb - 2 tag tag_tag 9tag to Aaaa.bb - 000002 tag tag_tag 9tag 
renaming Aaaa.bb - 4207 tag tag_tag 9tag to Aaaa.bb - 004207 tag tag_tag 9tag 

testdir:$ ls
Aaaa.bb - 000002 tag tag_tag 9tag  Aaaa.bb - 000125 tag tag_tag 9tag  Aaaa.bb - 004207 tag tag_tag 9tag  BACKUP/  renamerScript.sh*

Script

#!/bin/bash
# renamerScript.sh
# Author: Serg Kolo
# Date: Oct 17, 2015
# Purpose : renaming specific files
# Written for: http://askubuntu.com/q/686794/295286

# Make sure you have backup of the files !!!
# comment/uncomment if you do want to do a backup

mkdir BACKUP
cp -t BACKUP *

# This is where the magic happens:
# If we look at filenames from the awk point of view
# "Aaaa.bb - 2 tag tag_tag 9tag" are all really 
# space-separated fields. Field #3 is the one we need to edit.
# What we can do is pad it with zeros and then chop off 6 
# characters from the end, and store that as new filename
# The rest is just doing mv command from old filename to new
# And also making sure we're operating on files , not directories
# and not renaming the script file,too

for FILENAME in * ; 
do  
 if [ -f "$FILENAME" -a "$FILENAME" != "renamerScript.sh" ]; then

   OLDFILENAME="$FILENAME"
   NEWFILENAME=$(echo "$FILENAME" | awk '{$3="00000"$3; len=(length($3)-5); $3=(substr($3,len)); print  }')
   printf "renaming $OLDFILENAME to $NEWFILENAME \n"
   mv "$OLDFILENAME" "$NEWFILENAME"
 fi
done 
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • Can this script be modified in a way, that it at first checks file-name and then renames it when require? Noticed, that it takes too long to rename all files again, when it's not needed. – Ceslovas Nov 14 '15 at 00:16
0

You could use the perl-based rename command to re-print the sequence of digits following space-hyphen-space in the desired format and field width e.g.

rename -vn -- 's/ - (\d+)/sprintf " - %06d", $1/e' *

(the -n flag means 'no op' i.e. it will just do a dry-run to test the expression: remove it if/when you are sure that it's working right).

steeldriver
  • 136,215
  • 21
  • 243
  • 336
0

Try pyRenamer the bulk file renamer which rename files using patterns, substitutions, insert or delete text, or even rename files manually.

It has GUI window with these features:

  • Patterns to rename files. Search & replace to rename files. Insert and delete character anywhere from file name. Capitalization (make upper case file name). Common substitutions. Manually rename selected files. Rename images using their metadata. Rename music using its metadata. Source

Install: sudo apt-get install pyrenamer

jimmy
  • 1