62

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.

3
  • For one thing, you have a \e instead of -e.
    – Jon Lin
    Aug 29, 2012 at 9:44
  • 1
    Please provide some examples of the full filenames you're processing.
    – unwind
    Aug 29, 2012 at 9:44
  • Do you want to remove 'test.extra' globally I mean replace all matches, and not just the first ?
    – hostmaster
    Aug 29, 2012 at 9:55

7 Answers 7

73

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
3
  • 3
    My filenames has spaces. Modifying the above and using the below line worked for me mv "$filename" "${filename//test.extra/}"
    – Ramesh
    Jul 17, 2016 at 5:29
  • 1
    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, 2021 at 23:59
  • 2
    @GG2 google for "string operations in bash", it is a string replacement syntax
    – hostmaster
    Apr 16, 2021 at 7: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
1
  • 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//;' * Sep 1, 2019 at 7:44
39

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
1
  • Yes, it is correct, but think for a minute what is there is a directory (not a file) over there.
    – hostmaster
    Feb 26, 2018 at 7:39
30

Try the rename command:

rename 's/test.extra//g' *.fasta
4
  • What rename command is that? I can't find one which supports sed-like replacement syntax.
    – unwind
    Aug 29, 2012 at 9:47
  • 1
    This rename version is actually a part of perl package.
    – hostmaster
    Aug 29, 2012 at 9:52
  • just install and use perl-rename - it follows this exact syntax and is very handy with regexps
    – r0berts
    Dec 29, 2018 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
    – user377628
    Apr 16, 2021 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).

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.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.