LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-14-2006, 03:25 AM   #1
asilentmurmur
Member
 
Registered: Mar 2005
Location: Washington DC area
Posts: 214

Rep: Reputation: 30
please help me remove the spaces in filenames in a directory!


Hey everyone

I recently copied a folder over from my Winbl0wz partition. I noticed that many of the files have spaces in their file names. Do you all know of any scripts that i could use to remove all the spaces in the names of files contained in a directory? All help is GREATLY appreciated in advance!
 
Old 07-14-2006, 04:51 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Not onehundred percent trouble free so YMMV(VM):
Code:
find /home/asilentmurmur/windowsdir -type f | while read f; do mv "${f}" "${f// /_}"; done
BTW, try searching LQ, we definately have similar solutions, probably in the Programming forum.
 
Old 07-14-2006, 05:03 AM   #3
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
how about
Code:
find /home/asilentmurmur/windowsdir -type f -execdir rename '" "' '""' '{}' \;
 
Old 07-14-2006, 07:17 PM   #4
bushidozen
Member
 
Registered: Oct 2004
Posts: 215

Rep: Reputation: 30
If you have python installed try this:
Code:
#!/usr/bin/python
# the following script will traverse through all of the files
# within the directory where it is placed and replace each
# space within the filename with an underscore

import os, sys
if len(sys.argv) == 1:
    filenames = os.listdir(os.curdir)
else:
    filenames = sys.argv[1:]
for filename in filenames:
    if ' ' in filename:
        newfilename = filename.replace(' ', '_')
        print "Renaming", filename, "to", newfilename, "..."
        os.rename(filename, newfilename)
place the code in a text file (with a .py extension), make it executable, place it in the directory whose files you wish to change, and either run it from the command line (python name-of-file.py) or just click on it.

If you want to do it in bash, try this:
Code:
 for i in *; do mv "$i" "$(echo $i | tr ' ' '_')"; done
or this:
Code:
for i in * *; { mv ''$i'' $(echo $i | sed 's/ /_/g'); }
or this (this one is recursive):
Code:
#!/bin/bash
# Recursive replacement of spaces with "_" in files/dirs
[ $# -ne "1" ] && echo "Usage: $0 <dir-path>" && exit 1
find $1 -name "* *" | sed 's/^.*$/mv \"&\"/g' > t1$$
find $1 -name "* *"|sed -e 's/ /_/g' -e 's/^.*$/\"&\"/g' >t2$$
paste t1$$ t2$$ > t3$$; sh t3$$; #rm -f t?$$
#[t?$$ files will help you in an accidental action]
just cd to the directory and type one of the above. It should work, but I think the python code is a little faster.

Last edited by bushidozen; 07-14-2006 at 07:20 PM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
script to remove spaces from multiple filenames jeffreybluml Linux - Newbie 36 07-31-2013 02:10 AM
spaces in filenames dotancohen Programming 12 02-17-2006 02:42 PM
ls and filenames with spaces rose_bud4201 Programming 10 07-01-2005 08:28 AM
replacing all spaces in filenames of a directory merlin23 Linux - Newbie 1 01-11-2005 07:52 AM
spaces in filenames ebone Linux - General 2 11-12-2001 11:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:18 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