0

I'm trying to change multiple file names with a for loop.

This works to send the output to the screen:

for i in *.gz; do echo $i | sed 's/\-//g'; done

However, when I try to overwrite the file name using sed -i, I get this error:

for i in *.gz; do echo $i | sed -i 's/\-//g'; done
sed: no input files

Any suggestions?

2
  • What is your intention here? To remove the - character from the names of your .gz files? How are you using sed -i? Include that code in your question so that it is clear. See this related post: Batch renaming files on Unix & Linux. Commented Sep 11, 2018 at 21:41
  • Yeah, I'm just trying to remove the hyphens Commented Sep 11, 2018 at 21:46

2 Answers 2

1

there is a command for this

$ rename - '' *.gz

NB. this is the standard one, not the advanced perl version.

Sign up to request clarification or add additional context in comments.

Comments

0

Use Perl rename instead:

rename 's/-//g' *.gz

Or use simple parameter expansion:

for i in *.gz; do mv -- "$i" "${i//-}"; done

2 Comments

Thanks, rename worked. Just curious though, why doesn't sed -i work in this case?
@AlexBlumenfeld Because sed simply edited the text sent to it by echo. It didn't actually work with any files, hence the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.