1

I have a lot of data with file names like this:

combination.2005.0801.txt
combination.2005.0802.txt 
...
combination.2005.0830. txt

What should I do if I want to rename that files to be like this?

20100801.txt  
20100802.txt
...
20100803.txt

I already tried to use the following script, but unfortunately the script is not working.

rename 's/combination.(.*).(.*).txt/$1.txt/' *.txt

2 Answers 2

0

When using (Perl compatible; or more general, most) regular expressions, you'll have to keep in mind that . will match any character; not just periods (but also only one character unless there's a quantifier).

The following is untested, but i'd probably try to use this solution based on this question and answers:

$ for old in ./combination*.txt; do
    new=$(echo $old | sed -r 's/combination\.([[:digit:]]+)\.([[:digit:]]+)\.txt$/\\1\\2.txt/')
    mv -v "$old" "$new"
  done

Keep in mind that this is untested and might break. You could also use echo rather than mv to test the whole thing first. :)

4
  • I already tried to use that command. But the result: Commented Mar 19, 2014 at 8:11
  • I already tried to use that script. But the result = new: command not found. And then I tried change the line 2 (new = .....) by deleted the space (new=.....) and when I ran the script, the result = ./combination.2005.0801.txt ./combination.2005.0801.txt. It is mean the script is not working. Commented Mar 19, 2014 at 8:21
  • My bad, try the updated script. Commented Mar 19, 2014 at 8:34
  • The result by using the updated script= -v ./combination.2005.0801.txt ./\1\2\.txt Commented Mar 19, 2014 at 8:47
-1
$ for i in combination*.txt; do
   mv $i $(echo $i | awk -F\. '{print $2$3"."$4}'); 

done
2
  • thx, we edited it at the same sec :) Commented Apr 3, 2014 at 21:35
  • Why do you believe that you need the \ (backslash) between the -F and the .? Commented Apr 3, 2014 at 22:33

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.