LinuxQuestions.org
Review your favorite Linux distribution.
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 10-01-2008, 02:43 AM   #31
jrodert@yahoo.com.au
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0

Quote:
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"

Code:
#!/bin/bash
#
usage()
{
        echo "Usage: $0 [directory]"
        exit 1;
}

test -d "$1" || usage

dir="$1"

ls $dir | grep -e "[:alnum:]" | \
while read i; do
j=`echo -n "$i" | sed -e 's/ /_/'`
echo mv "$dir/$i" "$dir/$j"
done
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, 10:43 AM   #32
viabsb
LQ Newbie
 
Registered: Apr 2004
Posts: 13

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, 03:22 PM   #33
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,499

Rep: Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489
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:

Code:
 rename pic pic_ pic*
Obviously need to cd to the directory in which the files reside.
 
Old 05-13-2009, 06:53 PM   #34
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

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

Rep: Reputation: 0
Quote:
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
cases...

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

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

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

IIRC...
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).

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

My test tree was as follows:
~/F\ A/RecursiveSpaceSubstituter.sh
~/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, 02:10 AM   #37
madhukarkumarbcet
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 02:26 AM. Reason: better understanding
 
  


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