61

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.

jww
  • 97,681
  • 90
  • 411
  • 885
shaq
  • 799
  • 1
  • 7
  • 12

7 Answers7

72

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
hostmaster
  • 1,822
  • 16
  • 17
  • 3
    My filenames has spaces. Modifying the above and using the below line worked for me `mv "$filename" "${filename//test.extra/}"` – Ramesh Jul 17 '16 at 05:29
  • What is that 2nd parameter to `mv` (the `"${filename//test.extra/}"`)? Looks like a regex, except the match and replacement are switched. Tried looking it up, but don't know how to find that through Google. – GG2 Apr 14 '21 at 23:59
  • 1
    @GG2 google for "string operations in bash", it is a string replacement syntax – hostmaster Apr 16 '21 at 07:10
57

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
theon
  • 14,170
  • 5
  • 51
  • 74
  • 5
    I you get the error `Bareword "extra.test" not allowed while "strict subs" in use at (eval 2) line 1.`, try using this syntax instead: `rename 's/extra.test//;' *` – Josh Friedlander Sep 01 '19 at 07:44
38

I know this thread 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
Dan Swain
  • 2,910
  • 1
  • 16
  • 36
StephaneAG
  • 1,107
  • 1
  • 12
  • 11
30

Try the rename command:

rename 's/test.extra//g' *.fasta
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • What `rename` command is that? I can't find one which supports sed-like replacement syntax. – unwind Aug 29 '12 at 09:47
  • 1
    This rename version is actually a part of perl package. – hostmaster Aug 29 '12 at 09:52
  • just install and use `perl-rename` - it follows this exact syntax and is very handy with regexps – r0berts Dec 29 '18 at 12:44
  • On fedora, I used rename like `rename 'test.extra' '' *.fasta`, with the arguments being the substring, what to replace it with, and which files to rename –  Apr 16 '21 at 20:49
4
$ 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).

bobbogo
  • 14,989
  • 3
  • 48
  • 57
3
 // EXTENSION - File extension of files
 // STRING - String to be Replace

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

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.

Petrider
  • 35
  • 3