LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-03-2008, 03:04 PM   #1
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 25;CentOS 7; Kubuntu; Debian
Posts: 860

Rep: Reputation: 37
Is there a script for changes spaces to underscores?


I have a bunch of files I created back in the Windows days with spaces in the filenames. Although Linux can handle filenames with spaces, it makes working in the commandline really annoying. So is there a script out there that you have used and works well (bash, perl, python) that will changes the spaces in a directory of filenames into underscores?

Thanks,
 
Old 02-03-2008, 03:54 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
If you're using bash as your command shell, you can change the field separator to something other than whitespace by (re)setting the IFS Bourne shell variable. So you may not need to "fix" the blanks to accomplish your goal.

That said, the script you want should be rather trivial to write in, e.g., gawk.
 
Old 02-03-2008, 09:50 PM   #3
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 25;CentOS 7; Kubuntu; Debian
Posts: 860

Original Poster
Rep: Reputation: 37
Awesome script here in the first comment:

http://penguinpetes.com/b2evo/index....&c=1&tb=1&pb=1
 
Old 02-04-2008, 12:39 AM   #4
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Code:
#!/bin/bash

 function basic {
 for i in *
 do
     if [ -d "$i" ]
     then
         cd "$i"
         win2lin #name of this script. Must be in PATH or full path to script must be given.
         cd ..
     fi
     tempa=$(echo "$i" | sed 's/ /_/g' | sed 's/_-_/-/g' |  sed 's/\ -\ /-/g' | sed 's/__/_/g' \
     | sed "s#'##g" | sed "s#;##g" | sed "s#,##g")
     [[ "$i" != "$tempa" ]] && mv -v "$i" "$tempa"
 done
 }

 function cmd {
 for i in "$@"
 do
     tempa=$(echo "$i"  | sed 's/ /_/g' | sed 's/_-_/-/g' |  sed 's/\ -\ /-/g' | sed 's/__/_/g' \
     | sed "s#'##g" | sed "s#;##g" | sed "s#,##g")
     [[ "$i" != "$tempa" ]] && mv -v "$i" "$tempa"
     [[ ! -d "$tempa" ]] && break
     cd "$tempa"
     win2lin #name of this script. Must be in PATH or full path to script must be given.
     cd ..
 done
 }

 [[ "$@" != "" ]] && cmd "$@"
 [[ "$@" == "" ]] && basic
 
Old 02-04-2008, 09:28 PM   #5
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 25;CentOS 7; Kubuntu; Debian
Posts: 860

Original Poster
Rep: Reputation: 37
Code:
for x in *\ *;

do 

y=$(echo "$x"| sed y/\ \/_/)

mv "$x" "$y" 

done
worked perfectly for me and it's simple enough to understand
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script to replace spaces with - lleb Linux - Newbie 16 01-04-2008 04:15 PM
Cannot replace spaces w/ underscores clem_c_rock Linux - Newbie 7 09-27-2007 01:17 PM
bash script with spaces Quantum0726 Programming 2 11-14-2005 09:26 PM
How to replace spaces in filenames with underscores rosslaird Linux - Software 4 02-22-2005 01:08 AM
bash script ? -- spaces in passed parameters azwr Linux - Newbie 3 06-18-2004 06:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:41 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
Open Source Consulting | Domain Registration