Need a script to replace characters in filenames recursively

Forum rules
Before you post please read this

Need a script to replace characters in filenames recursively

Postby Mir on Tue Jun 24, 2014 8:21 am

I'm copying tens of thousands of files (of varying sizes, some up to several gigabytes) to and no files containing these characters will copy:

\ / : * ? | < >

Also included in this list are the tab, space, and newline characters.

I need a bash script that will go through every file and folder in a specified directory and those directories within it, replacing each of those characters with an underscore when and where they are found.

It probably isn't a good idea to rename both the files and the folders at the same time, so make that two scripts, one for each.
Mir
Level 1
Level 1
 
Posts: 8
Joined: Sat Apr 12, 2014 8:58 pm

Linux Mint is funded by ads and donations.
 

Re: Need a script to replace characters in filenames recursi

Postby martywd on Tue Jun 24, 2014 11:00 am

Mir wrote:I'm copying tens of thousands of files (of varying sizes, some up to several gigabytes) to and no files containing these characters will copy:

\ / : * ? | < >

Also included in this list are the tab, space, and newline characters.

I need a bash script that will go through every file and folder in a specified directory and those directories within it, replacing each of those characters with an underscore when and where they are found.

It probably isn't a good idea to rename both the files and the folders at the same time, so make that two scripts, one for each.


My scripting skills suck. But as an alternative in a gui you might take a looked at 'pyrenamer''?

Code: Select all
xxxxxxxx ~ # aptitude show pyrenamer
Package: pyrenamer                       
State: installed
Automatically installed: no
Version: 0.6.0-1.1
Priority: optional
Section: universe/gnome
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: all
Uncompressed Size: 754 k
Depends: gconf2 (>= 2.28.1-2), python (>= 2.4), python-support (>= 0.90.0), python-gtk2 (>= 2.4),
         python-glade2, python-hachoir-metadata | python-eyed3, python-gconf
Recommends: python-gnome2
Description: mass file renamer written in PyGTK
 You can rename files using patterns, search and replace, substitutions, insert or delete text,
 or even rename files manually. You can also rename images using their EXIF tags and music using
 their internal tags.
Homepage: http://www.infinicode.org/code/pyrenamer/



.
martywd
Level 3
Level 3
 
Posts: 119
Joined: Sun May 08, 2011 10:35 am
Location: Texas

Re: Need a script to replace characters in filenames recursi

Postby Mir on Tue Jun 24, 2014 5:54 pm

I figured it out after some research. FAT partitions don't allow certain characters. I was copying to a FAT32 partition.

Caja wouldn't let me paste to Ext4 partitions without root access, and doing a paste from root with Caja often corresponded with a memory leak in either the plugin manager in Firefox or mate-system-daemon. Caja also would use 4 gigabytes of RAM and 100% of one core of my CPU if I pasted anything greater than about 8 gigabytes to any external hard drive. The solution for me was to switch to a different file manager and use an EXT4 partition (NTSF will fail if you remove it without first ejecting it). This was problematic because gnome-disk-utility wouldn't allow me to modify partitions on any of my external hard drives, so I had to dig up an old EXT4 hard drive, disassemble a case for it, swap it out with the existing external hard drive, and reassemble the case.

As it stands I'll reinstall my OS or a different one, to get away from gnome, mate, and systemd. I never had any of these problems until systemd made it into debian testing. Bye, Mint!
Mir
Level 1
Level 1
 
Posts: 8
Joined: Sat Apr 12, 2014 8:58 pm

Re: Need a script to replace characters in filenames recursi

Postby lsatenstein on Fri Jun 27, 2014 12:01 pm

Mir wrote:I figured it out after some research. FAT partitions don't allow certain characters. I was copying to a FAT32 partition.

Caja wouldn't let me paste to Ext4 partitions without root access, and doing a paste from root with Caja often corresponded with a memory leak in either the plugin manager in Firefox or mate-system-daemon. Caja also would use 4 gigabytes of RAM and 100% of one core of my CPU if I pasted anything greater than about 8 gigabytes to any external hard drive. The solution for me was to switch to a different file manager and use an EXT4 partition (NTSF will fail if you remove it without first ejecting it). This was problematic because gnome-disk-utility wouldn't allow me to modify partitions on any of my external hard drives, so I had to dig up an old EXT4 hard drive, disassemble a case for it, swap it out with the existing external hard drive, and reassemble the case.

As it stands I'll reinstall my OS or a different one, to get away from gnome, mate, and systemd. I never had any of these problems until systemd made it into debian testing. Bye, Mint!


What you are trying to do can be done with a program written in C. trying to do it with scripting will work, but it will require multipasses of the bash translate command.

As a simple idea, you build a tale in C of 256 chars, using one-to-one. Then you choose to replace the characters you want by replacing that table entry with the new one.

A simple pass over the filename through the table will yield the translated name. Use the translated name as output.

Let me know what you decide to do or if you need C help.
lsatenstein
Level 1
Level 1
 
Posts: 17
Joined: Sat Dec 03, 2011 2:33 pm


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest