Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-01-2008, 03:43 AM   #31
LQ Newbie
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0

Originally Posted by homey View Post
I wonder if the original script didn't copy over correctly. I like to use the code tags to help prevent that sort of problem. Also, you do know the script has to be executable in order to use ./scriptname . Otherwise, you would use sh scriptname.
Anywho, here is another sample which you would make executable and run like this ...
./scriptname /home/myimages

Note: when it looks ok, remove the echo from the move statement....
echo mv "$dir/$i" "$dir/$j"
to look like this....
mv "$dir/$i" "$dir/$j"

        echo "Usage: $0 [directory]"
        exit 1;

test -d "$1" || usage


ls $dir | grep -e "[:alnum:]" | \
while read i; do
j=`echo -n "$i" | sed -e 's/ /_/'`
echo mv "$dir/$i" "$dir/$j"
You need a sed -e 's/ /_/g' ; ie a g for global replace in the sed line. else it only changes the first occurrence of space ' '
Old 05-13-2009, 11:43 AM   #32
LQ Newbie
Registered: Apr 2004
Posts: 12

Rep: Reputation: 10
Remove spaces

IFS=$'\n';for f in `find .`; do file=$(echo $f | tr [:blank:] '_'); [ -e $f ] && [ ! -e $file ] && mv "$f" $file;done;unset IFS
Old 05-13-2009, 04:22 PM   #33
LQ Guru
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 7,332

Rep: Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407
Solution to the OP's original problem from 4+ years ago, replacing spaces in filenames in a directory with underscore, from pic 001.jpg to pic_001.jpg:

 rename pic pic_ pic*
Obviously need to cd to the directory in which the files reside.
Old 05-13-2009, 07:53 PM   #34
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Actually, its
rename 'pic ' pic_ pic*
otherwise it inserts the underscore in front of the space, instead of replacing it
Old 04-17-2011, 03:43 PM   #35
Registered: Jun 2010
Posts: 47

Rep: Reputation: 0
Originally Posted by Bruce Hill View Post
I searched "remove spaces" and "Chinaman" and found this script

Hope that's it. I'm in America, and on my main PC in China I have a
file named "good_commands" which I use to copy and paste in such

This script will replace the space with an _ (underscore). The space is
considered an escape character, IIRC...

I think you mispelled the j=$((j-1)) to i=$((j-1)) and also you can add the a line
cd $1 inorder to point to a directory for running

#./script testdir
Old 08-22-2011, 04:32 AM   #36
LQ Newbie
Registered: Nov 2006
Location: Australia
Distribution: Mythbuntu 12.04
Posts: 1

Rep: Reputation: 0
Originally Posted by Bruce Hill View Post
I searched "remove spaces" and "Chinaman" and found this script

This script almost worked for me but in nested directories with space in both the directory names and file names it failed to substitute for spaces in the deepest directory (all directory names where handled correctly and all file names in directories above the deepest level were also handled correctly). My work around was to comment out the j=$((j-1) line (see below).

# This is
j=`find $1 -printf "%d\n" | sort -u | tail -n 1`
echo "Max dir depth:" $j
for (( i=0; i<=j ; i++ ))
for name in `find -mindepth $i -maxdepth $i -iname "* *" -printf "%p\n"`
newname=`echo "$name" | tr " " "_"`
echo "$name" "$newname"
mv "$name" "$newname"

My test tree was as follows:
~/F\ A/
~/F\ A/1\ 1
~/F\ A/2\ 1
~/F\ A/3\ 1
~/F\ A/F\ B/1\ 2
~/F\ A/F\ B/2\ 2
~/F\ A/F\ B/3\ 2
~/F\ A/F\ B/F\ C/1\ 3
~/F\ A/F\ B/F\ C/2\ 3
~/F\ A/F\ B/F\ C/3\ 3
All file and directory names above were handled OK.
The three file names below were not changed by the original script.
~/F\ A/F\ B/F\ C/F\ D/1\ 4
~/F\ A/F\ B/F\ C/F\ D/2\ 4
~/F\ A/F\ B/F\ C/F\ D/3\ 4

I hope this helps somebody. I would love some feedback on this.

Best regards.
Old 07-31-2013, 03:10 AM   #37
LQ Newbie
Registered: Sep 2012
Posts: 1

Rep: Reputation: Disabled
space with file name

Hello again,

Hoping somebody is feeling generous today...

I suck at writing scripts. I can admit that.
Can somebody post a short script that will take a directory full of .txt all of which have spaces in the filenames, and remove the spaces from the names? I'm trying to streamline the file name , but the file name is taking the always puts spaces in the file name ...

EX : file name is : ?prepago-?peque�aempresa-?mujer.txt or " PREPAGO- Peque�aEmpresa- ".txt

I want to remove ? or space from file name . please help me ASAP.
Wanted file : prepago-peque-aempresa-mujer.txt

I greatly appreciate whomever is feeling nice enough to help me here....

Thanks as always,

Madhukar Kumar

Last edited by madhukarkumarbcet; 07-31-2013 at 03:26 AM. Reason: better understanding


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
Bash script to remove capitalisation and spaces form a filename scuzzman Programming 11 05-18-2008 01:28 PM
script to change multiple filenames in a directory jeffreybluml Linux - Newbie 8 12-06-2006 02:46 AM
need help with script to remove all metachars from filenames BrianK Programming 5 08-21-2005 12:10 AM
Changing multiple filenames with a script brecki Linux - Newbie 8 01-30-2004 04:10 PM
spaces in filenames ebone Linux - General 2 11-12-2001 12:56 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:27 PM.

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