How best to remove certain characters from filenames and folders recursively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How best to remove certain characters from filenames and folders recursively
# 8  
Old 05-16-2012
You removed the [] from the find expression, which will break it. It needs to be a character range just like the bash one.

Why not try it, with the echo intact, to see what it does?
# 9  
Old 05-16-2012
Sorry about missing the brackets. This is why I can never be a coder.

I'm finally getting somewhere though!


When I ran the script, it seems like it's replacing more than I want it to. For instance, I ouput the results of the script to a file, and I'm seeing lots of entries like:

mv ./DESTIN LAB/Data/Collaborations/Daphna/619/PsychScience/Two Study/Destin_Oyserman_Feared_Selves.doc ./DESTIN LAB/Data/Collaborations/Daphna/619/PsychScience/Two Study/_estin_Oyserman_Feared__elves.doc

It's replacing the first D for some reason as well as added a double underscore at the end.

Here is the script so far:
Code:
#!/bin/bash

# Find all files or folders containing 'bad' characters.
find . -depth -name "*[~$*\/*?”<>|]*" |
# Read them line-by-line.
while read FILEDIR
do
       # Get the folder its inside
       DIR="${FILEDIR%/*}"
       # Get the plain name.
       FILE="${FILEDIR/*\/}"
       # Substitute _ for bad things.
       NEWFILE="${FILE//[\/~$*?”<>|]/_}"
       # Rename it.
       echo mv "$DIR/$FILE" "$DIR/$NEWFILE"
done

# 10  
Old 05-17-2012
I'm not sure but I think you might need to escape (prefix with \) the ~ and the $. Also, I'm quite sure you cannot have filenames with / in it.

Also, I'd go with the more thorough scrubbing. Replace the stuff inside both [...] with:
Code:
[^a-zA-Z0-9._-]

and in the find command, drop the "*" before the open bracket ([).
# 11  
Old 05-17-2012
Awesome!
Code:
[^a-zA-Z0-9._-]

worked terrific. If it's ok to leave the spaces in the name, what would I add or subtract out of that line to make it happen.

Thank you both again for your help!
# 12  
Old 05-17-2012
To tell it that spaces are okay, add a space to the expression before the ] .
# 13  
Old 05-17-2012
Is that in both lines? I tried it in both and it didn't work.


Code:
#!/bin/bash

# Find all files or folders containing 'bad' characters.
find . -depth -name "*[^a-zA-Z0-9._- ]*" |
# Read them line-by-line.
while read FILEDIR
do
       # Get the folder its inside
       DIR="${FILEDIR%/*}"
       # Get the plain name.
       FILE="${FILEDIR/*\/}"
       # Substitute _ for bad things.
       NEWFILE="${FILE//[^a-zA-Z0-9._- ]/_}"
       # Rename it.
       mv "$DIR/$FILE" "$DIR/$NEWFILE"
done

# 14  
Old 05-17-2012
- is a special character inside [ ]. If you mean it to match a literal -, give it \-
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to compare partial filenames in two folders and delete duplicates

Background: I use a TV tuner card to capture OTA video files (.mpeg) and then my Plex Media Server automatically optimizes the files (transcodes for better playback) and places them in a new directory. I have another Plex Library pointing to the new location for the optimized .mp4 files. This... (2 Replies)
Discussion started by: shaky
2 Replies

2. Shell Programming and Scripting

Change filenames recursively

Hello, I made a mistake in a script and now need to go back and change allot of filenames. I need to change "v4" in filenames to "v3". I was thinking of something like this. #!/bin/bash FILELIST=$(ls -f -R *) for FILE in $FILELIST do # create new filename ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

3. Shell Programming and Scripting

recursively going through folders and subdirectorys and running delete from crontab

Hio, So I have a crontab delete of older files setup. This script works fine if I run them by each individual directory. Problem is there are so many thousands of files and hundreds of directories and sub directories that I need to recursively have it go through and delete files by directory... (2 Replies)
Discussion started by: vsekvsek
2 Replies

4. Shell Programming and Scripting

ftp script to copy folders recursively

hi all I want a script that will use ftp to copy folder and sub folders from source server to current server. if i use -r switch then it just copies folders for 5 level. (1 Reply)
Discussion started by: kashif.live
1 Replies

5. Shell Programming and Scripting

HELP! Need to compare 2 folders on 2 different systems, and copy unmatched filenames to other folder

This has been tearing my hair out. I need to: 1: compare server1:/data/archive/ to server2:/data/archive/ (through rsync, ssh, etc) 2: filenames that don't match, get copied (scp) to server2:/data/ server1 and server2 have ssh, scp, rsync access between eachother. Is there any option in... (3 Replies)
Discussion started by: damang111
3 Replies

6. UNIX for Dummies Questions & Answers

find & remove characters in filenames

I have a group of files in different directories with characters such as " ? : in the file names. How do I find these files and remove these characters on mass? Thanks (19 Replies)
Discussion started by: barrydocks
19 Replies

7. Shell Programming and Scripting

Change all filenames under different folders...

Hi, all: I'd love to use shell script to change all filenames under different folders once for all: I've got over 100 folders, in each of them, there is a file named "a.ppm". I wanna change all these "a.ppm" to "b.ppm", and still . Visually, the directory structure looks like: and hope... (1 Reply)
Discussion started by: jiapei100
1 Replies

8. Shell Programming and Scripting

RSYNC script to transfer folders recursively without overwriting via FTP

Hi all, I would need a bash script to sync/transfer folders recursively via FTP/RSYNC (I initially planned to use FTP but I heard RSYNC would fit a lot better for this job(?)) The situation: 3 different Linux servers 1. source 2. destination - Samba 3. Server where the script runs on ... (2 Replies)
Discussion started by: thibautp
2 Replies

9. UNIX for Dummies Questions & Answers

delete recursively contents of folders

hi, I've a folder structure like : /home/project/LIBNAMEA/FILE1 /home/project/LIBNAMED/FILE2 /home/project/LIBNAMEC/FILE3 /home/project/LIBNAMED/FILE4 /home/project/LIBNAMEX/FILE5 (there is no relation in the letters after the project/ ) and i need to delete the files keeping... (5 Replies)
Discussion started by: jtmartins
5 Replies

10. Shell Programming and Scripting

remove special characters from filename recursively

hi: i have several thousand files from users and of course they use all kind of characters on filenames. I have things like: My special report (1999 ) Lisa & Jack's work.doc crazy. How do I remove all this characters in the current dir and subdirs too? Thanks. (3 Replies)
Discussion started by: jason7
3 Replies
Login or Register to Ask a Question