Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

As a follow-up to my previous question, if I have multiple files of the form

sw.ras.001
sw.ras.002
sw.ras.003
…

What command can I use to remove the ras. in the middle of all the files?

share|improve this question
    
Do you want to remove only the ras, i.e. end up with sw..001, or the ras., leaving sw.001? –  Kevin Mar 2 '12 at 20:11
    
i'd probably want to remove the 'ras.'. would the command be very different? –  Paul Mar 2 '12 at 20:12
    
No, just whether you include the \. in the patterns. –  Kevin Mar 2 '12 at 20:19
add comment

2 Answers

up vote 7 down vote accepted

You can do this with a fairly small modification of either answer from the last question:

rename s/ras\.// sw.ras.*

or

for file in sw.ras.*; do
    mv "$file" "${file/ras./}"
done

Explanation:

rename is a perl script that takes a perl regular expression and a list of files, applies the regex to each file's name in turn, and renames each file to the result of applying the regex. In our case, ras is matched literally and \. matches a literal . (as . alone indicates any character other than a newline), and it replaces that with nothing.

The for loop takes all files that start with sw.ras. (standard shell glob) and loops over them. ${var/search/replace} searches $var for search and replaces the first occurrence with replace, so ${file/ras./} returns $file with the first ras. removed. The command thus renames the file to the same name minus ras.. Note that with this search and replace, . is taken literally, not as a special character.

share|improve this answer
    
Could you explain your answer to others learning from? What "s/ras\.// sw.ras.*" do? I guess I know it but I want to be sure, and other can learn too. Second option same way. –  H_7 Mar 3 '12 at 16:47
1  
@H_7 Good idea, I've added an explanation. –  Kevin Mar 3 '12 at 18:09
add comment

In any POSIX-compliant shell (bash, dash, ksh, etc):

for file in sw.ras.[[:digit:]][[:digit:]][[:digit:]]; do
    mv "${file}" "${file/ras\./}"
done

Or with rename:

rename 's/ras\.//' sw.ras.[[:digit:]][[:digit:]][[:digit:]]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.