![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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.
|
|
||||
|
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 |
|
||||
|
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 | ||
|
|