Replace spaces recursively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace spaces recursively
# 1  
Old 08-31-2007
Replace spaces recursively

Hi,

I have a directory with files and sub-directories (sub-directory depth might go upto 5). There will be one or more spaces (continuously or anywhere in the file name) which need to be replaced with HYPHENs. How can i replace all SPACE occurances with HYPHEN in file/dir names recursively. (2 or more continuous occurances of SPACES should be replaced with SINGLE HYPHEN).


Thanks
Prvn
# 2  
Old 08-31-2007
Try This:

Code:
find . -name "*" | sed '1,$s/ /\-/g; s/\-\-/\-/g'

# 3  
Old 08-31-2007
Quote:
Originally Posted by ahmedwaseem2000
Try This:

Code:
find . -name "*" | sed '1,$s/ /\-/g; s/\-\-/\-/g'

Waseem,

This will not rename the file. Option is to write a script with a combination of find ,sed and mv command.

Cheers,
K
kamitsin
# 4  
Old 08-31-2007
Quote:
Originally Posted by kamitsin
Waseem,

This will not rename the file. Option is to write a script with a combination of find ,sed and mv command.

Cheers,
K
I know it will not rename the file but it will only generate the list of filenames as expected. a simple mv can be performed after getting the list from my previous command.
# 5  
Old 08-31-2007
Thannks kamitsin and Waseem.

Kamitsin, yours offer recursive solution and yes, i will use mv to achieve the rest.

I need small addon to my requirement (sorry, i did not mention earlier) that LEADING and TRAILING spaces should be removed (not to be replaced with HYPHEN). In other words, file/dir names should not start/end with HYPHENS e.g. if a file with name
" m n " to become "m-n".

Txs
Prvn
# 6  
Old 08-31-2007
If you have Python and is able to use it as an alternative:
Code:
#!/usr/bin/python
import os,re
for root,dir,files in os.walk("/test"):
    for fi in files:
        if fi.count(" ")>0:
            fi=fi.strip()
            newfile = os.path.join(root,re.sub("\s+","-",fi))
            os.rename(os.path.join(root,fi),newfile)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find and replace a string with spaces and / recursively?

Hi all, I wanted to find and replace an email id from entire directory structure on a Linux server. I found that find . -type f -print0 | xargs -0 sed -i 's/abc@yahoo.com/xyz@gmail.com/g' would do it perfectly. But my search criteria has extended and now I want to search for a string1 like... (2 Replies)
Discussion started by: pat_pramod
2 Replies

2. Shell Programming and Scripting

Search/Replace in multiple files recursively

Hi there, I am using AIX and trying to search and replace a string with another string in multiple files in different directories. I wanted to search replace in steps so I don't change all of the instance anywhere in the server at once, minimizing impact. STEP 1: -------- I first searched... (5 Replies)
Discussion started by: zaino22
5 Replies

3. Shell Programming and Scripting

String replace that has spaces

cat rf|nawk '/Use SSL= 0/{n+=1}{if (n==3){sub("Use SSL= 0","Use SSL= 0x1",$0)};print }' > rf2Fails. sed 's/Use SSL= 0/Use SSL= 0x1/g' rf > rf2Fails. In addition, the goal is to ONLY replace the 2nd occurence of the... (15 Replies)
Discussion started by: rfransix
15 Replies

4. Shell Programming and Scripting

Replace with spaces

Hi Guys file:///C:/DOCUME%7E1/c104058/LOCALS%7E1/Temp/moz-screenshot.pngsed 's///g' /source/filename.txt > /destination/filename.txt The above code deletes the characters which are not A-Z, a-z and 0-9, but I wanted to replace it with space without deleting them. Any help is... (2 Replies)
Discussion started by: gowrishankar05
2 Replies

5. Shell Programming and Scripting

replace 2 spaces by one

Dear Friends, I have a flat file from which I want to remove single "space". And, wherever two spaces are provided it should replace it by only one space. E.g. I have N A T I O N A L E D U C A T I O N F O R O R G AN I S A T I ON S I want NATIONAL EDUCATION FOR ORGANISATIONS Please... (5 Replies)
Discussion started by: anushree.a
5 Replies

6. Shell Programming and Scripting

how to replace . with 100 spaces

i have a file like:: $ cat space asd fghj itkg now i want to replace the next line with . and thn this . with the 100 spaces. cat space | tr '\n' '.', it woked for me, to replce the new line to . Now i want to replace this . with 100 spaces. Thanks in advance. (10 Replies)
Discussion started by: Prashant Jain
10 Replies

7. Shell Programming and Scripting

sed search and replace recursively

Hi, I tried making a shell script which recursively search in a given directory for all files *.txt and then search and replace some text and after that save each file as $filename.dynamips.txt I would like to get this done with sed. I tried but couldn't get it to work. BTW this is not... (1 Reply)
Discussion started by: 2bone
1 Replies

8. Shell Programming and Scripting

find and replace a search string recursively in files

Hi , I have a directory structure as dir and subdirectories and files under it and so on.now I need to find the files which contain the search string under every dir and subdir and replace . my search string is like searchstring=/a/b string to be replaced=/a/c/b please help. ... (7 Replies)
Discussion started by: mohanpadamata
7 Replies

9. Shell Programming and Scripting

Replace spaces

Hi guys, so I have another issue. Can I use sed to replace spaces in a string or variable with %20 I am having trouble with using curl on URL's containing spaces Thanks! (12 Replies)
Discussion started by: tret
12 Replies

10. Shell Programming and Scripting

Replace all occurances of a string in all file-/foldernames, recursively

I need a script that will replace all occurances of a string in all filenames and foldernames, recursively. Right now I have this script: for f in `find -name *eye*`; do echo processing $f g=`expr "xxx$f" : 'xxx\(.*\)' | tr 'eye' 'm'` mv "$f" "$g" done The problem is that tr... (2 Replies)
Discussion started by: TheMJ
2 Replies
Login or Register to Ask a Question