Removing part of a filename for multiple files on Linux

LinuxBashShell

Linux Problem Overview


I want to remove test.extra from all of my file names in current directory

for filename in *.fasta;do 

    echo $filename | sed \e 's/test.extra//g'

done

but it complains about not founding file.echo is to be sure it list correctly.

Linux Solutions


Solution 1 - Linux

First of all use 'sed -e' instead of '\e'

And I would suggest you do it this way in bash

for filename in *.fasta; do 
    [ -f "$filename" ] || continue
    mv "$filename" "${filename//test.extra/}"

done

Solution 2 - Linux

Try rename "extra.test" "" *

Or rename 's/extra.test//;' *

$ find
./extra.test-eggs.txt
./extra.testbar
./fooextra.test
./ham-extra.test-blah

$ rename "extra.test" "" *
$ find
./-eggs.txt
./bar
./foo
./ham--blah

Solution 3 - Linux

I know this tread is old, but the following oneliner, inspired from the validated answer, helped me a lot ;)

for filename in ./*; do mv "./$filename" "./$(echo "$filename" | sed -e 's/test.extra//g')";  done

Solution 4 - Linux

Try the rename command:

rename 's/test.extra//g' *.fasta

Solution 5 - Linux

$ mmv '*test.extra*.fasta' '#1#2.fasta'

This is safe in the sense that mmv will not do anything at all if it would otherwise overwrite existing files (there are command-line options to turn this off).

Solution 6 - Linux

 // EXTENSION - File extension of files
 // STRING - String to be Replace

      for filename in *.EXTENSION;
      do  [ -f "$filename" ] || continue;  
      mv "$filename" "${filename//STRING/}"; 
      done

Solution 7 - Linux

In Kali linux rename command is rename.ul

rename.ul 'string-to-remove' 'string-to-replace-with' *.jpg

example: rename.ul 'useless-string' '' *.jpg This will delete useless-string from all the jpg image's filname.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshaqView Question on Stackoverflow
Solution 1 - LinuxhostmasterView Answer on Stackoverflow
Solution 2 - LinuxtheonView Answer on Stackoverflow
Solution 3 - LinuxStephaneAGView Answer on Stackoverflow
Solution 4 - LinuxRody OldenhuisView Answer on Stackoverflow
Solution 5 - LinuxbobbogoView Answer on Stackoverflow
Solution 6 - LinuxHakeem P AliView Answer on Stackoverflow
Solution 7 - LinuxPetriderView Answer on Stackoverflow