LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Renaming files with sed (https://www.linuxquestions.org/questions/linux-newbie-8/renaming-files-with-sed-869791/)

TommySprat 03-20-2011 11:09 AM

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
for i in $( ls ); do
    NewName='sed' 's/AA//g' '$i'
    mv '$i' $NewName
done

The output however is a lot of times this:
Code:

./test.sh: line 3: s/AA//g: No such file or directory
mv: missing destination file operand after `$i'

From that 2nd line i can tell that $NewName is just empty. I also read something about sed needing the -e option for scripting purposes but i just don't understand it.

sycamorex 03-20-2011 11:23 AM

Try the following:
Code:

#!/bin/bash

for i in *.lol
do
        mv $i $(echo $i | sed "s/AA//")

done


anishkumarv 03-20-2011 11:56 AM

Hi TommySprat,

Try this!!
Code:

for file in AA*; do
  mv "$file" "${file#AA}"
done


TommySprat 03-20-2011 01:34 PM

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.

sycamorex 03-20-2011 03:10 PM

From the Bash manual:
Quote:

Command Substitution
Command substitution allows the output of a command to replace the command name. There are two forms:

$(command)
or
`command`

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the
command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word
splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed
by $, `, or \. The first backquote not preceded by a backslash terminates the command substitution. When using the
$(command) form, all characters between the parentheses make up the command; none are treated specially.

Command substitutions may be nested. To nest when using the backquoted form, escape the inner backquotes with back‐
slashes.

If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results

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.

H_TeXMeX_H 03-20-2011 03:18 PM

And for the simplest rename:

Code:

rename AA "" *

kurumi 03-20-2011 07:54 PM

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/,""))  }'
Any of the others like Perl,Python etc does similar as well.

anishkumarv 03-20-2011 08:04 PM

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.