I have a set of files in a directory as shown below and I want to remove the string after the occurrence of the last underscore from it. The string may be different each time. It will be good if it can be done with sed.

Input files:


Resulting file names:


closed as unclear what you're asking by Jeff Schaller, JigglyNaga, G-Man, Archemar, RalfFriedl Dec 7 '18 at 6:20

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Why sed? Sed doesn't deal with files, it only deals with text. More importantly, what operating system are you using? Is it a Linux variant? Unix? MacOS? BSD? Something else? – terdon Dec 6 '18 at 15:03
  • Would there ever be a conflict? With an existing file named, say, com_101_00000_0001.txt before you start the renaming process? What should happen in that case, if so? – Jeff Schaller Dec 6 '18 at 15:10
  • I tried with sed to Edit the file name ..But couldn't get the .Txt or .ctl part appeneded.that's why i thought it will be easy to follow up.but it's ok whichever command appropriate in this scenario can use. Unix variant system am using. – Vinay Dec 6 '18 at 15:10
  • What Unix variant system? There are significant differences in the way the various default tools behave on different systems. Please edit your question and give us the actual operating system you are using. – terdon Dec 6 '18 at 15:13

If your system has the perl rename, you can do:

rename -n 's/_[^_]+?\././' *

If your files could end up with the same name, to avoid overwriting them, use -i:

rename -in 's/_[^_]+?\././' *

If that does what you expect, remove the -n to actually rename the files.

If you really insist on shoehorning sed into this, you could do something needlessly complicated like this:

for f in *_*.*; do
    new=$(printf "%s\n" "$f" | sed 's/\(.*\)_[^_]*\(\.[^_]*\)$/\1\2/')
    echo mv "$f" "$new"

Alternatively, without sed, the saner:

for f in *_*.*; do echo mv "$f" "${f%_*}.${f##*.}" ; done

For both of the last examples, if they do what you expect, remove the echo to actually rename the files.

  • Thank you for the reply @terdon. But system doesn't have perl,not able to use rename. – Vinay Dec 6 '18 at 15:16
  • 1
    @Vinay well, that's precisely why I asked you (two times) to tell me what operating system you are using! And I give 3 solutions in my answer, do none of them work? – terdon Dec 6 '18 at 15:16
  • @Vinay I have given three options. And you still haven't answered the simple question that will tell me what tools you can use. So I ask again. What operating system are you using? – terdon Dec 6 '18 at 15:17
  • The system we are keeping these files maintained by seperate team ,we have just access to a few folders from where we move this to our servers .If I am correct it's Linux /Unix installed machine . – Vinay Dec 6 '18 at 15:30
  • ..Definitely i will check the solutions shared ,to see which one works fine for me – Vinay Dec 6 '18 at 15:31

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