Renaming files with sed
Today i am trying to learn how to use sed. I set up a testing folder with the following files:
AAb.lol AAc.lol AAx.lol test.sh My goal is to create a script (test.sh) which renames all the files to their original name without AA. I want to end up with this: b.lol c.lol x.lol test.sh sed seemed to be the perfect tool so i went ahead and created a script which i think should clear the job. Code:
#!/bin/bash Code:
./test.sh: line 3: s/AA//g: No such file or directory |
Try the following:
Code:
#!/bin/bash |
Hi TommySprat,
Try this!! Code:
for file in AA*; do |
Thanks for the replies. I tried both methods and they both worked. The problem is solved but i still have some questions.
First about sycamorex's method. I thought that a dollar sign in front of a word was the instruction for bash to substitute it with the value of the variable. I see you use it on something that's not a variable, can anyone explain the need and function of the dollar sign there? A related question i have is what the function of the parentheses is. My first question for anishkumarv is the same as my first question because he also uses the dollar sign on a place i don't understand. Secondly i would like to know what the curly brackets do as well as the pound sign. I tried looking this all up myself of course but it's either not in there or i don't have enough basic knowledge about bash to grasp it. Although the problem is "solved" i would appreciate more answers because I would love to be able to come up with this myself next time. |
From the Bash manual:
Quote:
The answer to the second question can also be found in the bash manual under the parameter expansion heading. It makes a very interesting read. |
And for the simplest rename:
Code:
rename AA "" * |
If you have the choices, you can pick up a programming language. Eg Ruby
Code:
$ ruby -e 'Dir["*"].each {|x| File.file?(x) && File.rename(x, x.gsub(/^AA/,"")) }' |
Hi Tommy
Why is $(...) preferred over `...` (backticks)? http://mywiki.wooledge.org/BashFAQ/082 Check this link It really useful to u, Mainly it is useful for to avoid unnecessary mistakes in script. |
All times are GMT -5. The time now is 12:45 AM. |