.

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Mir

.

Post by Mir »

...............
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 3 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
martywd
Level 3
Level 3
Posts: 147
Joined: Sun May 08, 2011 10:35 am
Location: TX

Re: Need a script to replace characters in filenames recursi

Post by martywd »

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/

.
Mir

.

Post by Mir »

...............
Last edited by Mir on Sat Dec 05, 2015 9:19 pm, edited 1 time in total.
lsatenstein

Re: Need a script to replace characters in filenames recursi

Post by lsatenstein »

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.
Locked

Return to “Scripts & Bash”