The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to append spaces(say 10 spaces) at the end of each line based on the length of th prathima UNIX for Dummies Questions & Answers 17 01-28-2009 04:10 PM
Replace blank spaces by single tab, and right alignment Jae Shell Programming and Scripting 1 08-08-2007 10:58 PM
Replace all occurances of a string in all file-/foldernames, recursively TheMJ Shell Programming and Scripting 2 04-12-2006 01:40 AM
Replace spaces with 0's having numeric values. videsh77 Shell Programming and Scripting 1 04-15-2005 01:22 AM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 02:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-31-2007
prvnrk prvnrk is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 137
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 (permalink)  
Old 08-31-2007
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
Try This:

Code:
find . -name "*" | sed '1,$s/ /\-/g; s/\-\-/\-/g'
  #3 (permalink)  
Old 08-31-2007
kamitsin's Avatar
kamitsin kamitsin is offline
Registered User
  
 

Join Date: Nov 2006
Location: /dev/null
Posts: 177
Quote:
Originally Posted by ahmedwaseem2000 View Post
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
  #4 (permalink)  
Old 08-31-2007
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
Quote:
Originally Posted by kamitsin View Post
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 (permalink)  
Old 08-31-2007
prvnrk prvnrk is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 137
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 (permalink)  
Old 08-31-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,436
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)
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0