LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-21-2012, 08:54 PM   #1
fatalerror0x00
Member
 
Registered: Oct 2012
Location: Holden, ME, USA
Distribution: SlackWare64 14.00
Posts: 185

Rep: Reputation: 0
Renaming files to have no whitespace or slashes


I can not emphasize on how disappointed I am in result I found in google on this. This topic and peoples solutions are a huge headache and I can't take it anymore but I have over 500 files to rename without slahses or white space but need to keep all other special symbols or maybe if I can get a while to change & to the text and. So my issue is every solution I find to making a file name have no whitespace is it doesn't actually rename the file it just shows me what it would be with no whitespace but then I do ls and there it is with whitespace and I checked to make sure it just isn't the old ones and that it didn't just copy and is above. I wish to get rid of the slashes along with it please. Can someone give me a small code for this please I'd really appreciate it.

so the code needs to actually rename the file not just display it showing what it would be with no whitespace

slashes need to go that show whitespaces

would be nice to be able to replace & with the text "and"

Thanks I appreciate it. This is just a huge headache I tried just doing tr ' ' '_' but that was taking forever and I'm scared I might have screwed up the files or something worse but everything I have found and tried is either not working or screwing up other stuff.

I already screwed up my directories on my system after on a usb drive trying to rename a file the same thing but making it not all caps.

Did you know that every OS I've ever used recently can't handle properly renaming a file when the only change was the case. it's really strange either it throws an error (linux) or it doesn't show the change unless you go to rename it again (windows) and I'm not sure about mac
 
Old 11-21-2012, 10:47 PM   #2
TKH
Member
 
Registered: Jul 2011
Location: Milky Way
Distribution: Ubuntu, LFS, Slackware, Fedora
Posts: 218

Rep: Reputation: 20
you want to replace all the whitespace chars to underscore, yes?

if so, you might make use of this script:

in this case, all the files are in one folder
Code:
for i in *
do
j=$(echo $i | sed 's/\ /_/')
mv $i $j
done
 
Old 11-22-2012, 12:50 AM   #3
bryanl
Member
 
Registered: Dec 2003
Posts: 96

Rep: Reputation: 35
what I usually do for things like this is to first use find to make a list of files (e.g. find . -iname "pattern" > files.lst)

then I use vi on the list to rename them with the substitute command (similar to above). It can be a bit tricky when you have multiple characters as that means quite a number of groups and the regexp can also be difficult to get right. -- this is the sort of thing true sys admins do rather than crossword puzzles or whatnot ;-)

OR, you might want to look at using AWK or something on the file of file names to pick up a line and loop through file name character by character to make desired changes. This would be easier if you used the printf option in find to separate the path from the file name so you'd have a two field comma separated (e.g. find . -iname "pattern" -printf "\"%f\",\"%h\"\n > files.csv), double quoted strings that AWK could parse and then process the file then output a line formatted as mv "filespec1" "filespec2"

OR, you can just make a file with find that you can edit manually. In this case, use the printf option %p twice with an mv on the front and escaped double quotes around the file name and a \n at the end.

either way, you'd get a shell script for the rename that you could examine to see if it was what you wanted before you tried it.

Also check the man page for rename - you might be able to use that successively changing just one thing at a time.

of course, in things like this there is no more comfortable feeling that having a good backup in several forms readily available.

Quote:
This topic and peoples solutions are a huge headache
I can sympathize with this! It is one of those simple sounding things that really isn't trivial.

Last edited by bryanl; 11-22-2012 at 12:52 AM.
 
Old 11-22-2012, 01:12 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
I'd amend post #2
Code:
for i in *
do
j=$(echo $i | sed 's/\ /_/g')
mv "$i" $j
done
Need sed 'g' option to get all spaces in a name (>1) and need quotes in mv cmd to mv (rename) single file, instead of treating it as several filenames, thus implying target is supposed to be a dir.
 
1 members found this post helpful.
Old 11-22-2012, 10:22 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
If using bash I would suggest dispensing with the call to sed as well:
Code:
for i in *
do
    mv "$i" "${i// /_}"
done
As there was mention of possibly other meta characters I would also quote second variable in the move, whatever process you use.
 
Old 11-22-2012, 11:53 AM   #6
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
I have once written a short Perl script which cleans up the file and directorynames in my mp3 music directory. Here it is:
Code:
#!/usr/bin/perl

use strict ;
use warnings ;
use File::Copy ;

opendir THISDIR, "." ;
my @files = readdir THISDIR ;
my ( $old, $new ) ;
foreach $old ( @files ) {
        $new = $old ;
        $new =~ s/ - /-/g ;
        $new =~ s/ /_/g ;
        $new =~ s//ae/g ;
        $new =~ s//Ae/g ;
        $new =~ s//oe/g ;
        $new =~ s//Oe/g ;
        $new =~ s//ue/g ;
        $new =~ s//Ue/g ;
        $new =~ s//ss/g ;
        $new =~ s/,/./g ;
        $new =~ s/'/./g ;
        $new =~ s/\(//g ;
        $new =~ s/\)//g ;
        $new =~ s/&/und/g ;
        next if $old eq $new ;
        move ( $old, $new ) ;
}
it changes every file and directoryname within the current workingdirectory. Furthermore it eliminates german Umlaute and other special characters. If a file with the new name already exists, it does nothing.

Markus
 
Old 11-23-2012, 04:24 AM   #7
fatalerror0x00
Member
 
Registered: Oct 2012
Location: Holden, ME, USA
Distribution: SlackWare64 14.00
Posts: 185

Original Poster
Rep: Reputation: 0
Thank you all for your help sadly I fixed the issue before you guys started replying and never closed this post but I'm sure someone else in the future going through this for the same issue at this time will be glad to find this I honestly don't remember what I used to fix it but I did use some sort of for statement i don't have the script anymore this was the last of my windows files and really only windows files I'll ever have really with spaces in the file names that I myself can't within minutes have reanmed with no spaces as here in this case there were 594 files and about 550 had spaces in the names
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bind9 config files: leading whitespace requirements? catkin Linux - Server 10 04-22-2011 12:56 PM
[SOLVED] Replacing "slashes" with back slashes - escape character problem kushalkoolwal Programming 2 01-08-2011 02:16 AM
remove multiple files with whitespace coldy78 Linux - General 1 12-10-2007 03:23 AM
renaming files starwarsfan982 Linux - Software 7 10-30-2006 03:06 PM
problem creating BASH script with files that have whitespace scattaneo Linux - General 2 11-06-2004 06:40 PM


All times are GMT -5. The time now is 08:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration