1

I have a folder with a bunch of png image files. I need to trim the filenames to remove some information from the end.

E.g.

some file-170227-222746.png
some other file-170228-222742.png
another file-170226-222743.png

I need to remove everything after the - while retaining the .png extension, to end up with:

some file.png
some other file.png
another file.png

All files are png image files and I don't need to keep the original files.

I've tried this which works but does the wrong thing, in that it removes the .png extension

for file in *.png; do
  mv -- "$file" "$(file%%.png"
done

Can the above mv command be reworked to do what I need? Should I use a different method?

Thanks

4
  • For clarity, I think you are trying to remove everything between the - and the .png at the end (not simply "everything after the -"), yes? Commented Mar 12, 2017 at 12:02
  • Yes that is correct Commented Mar 12, 2017 at 12:04
  • 1
    @RakeshSharma please avoid answering questions in the comments! (ie please post an answer) Commented Mar 12, 2017 at 12:21
  • @Zanna. Placed in the Answer Section. Thanks. Commented Mar 12, 2017 at 12:27

3 Answers 3

2

Your approach on the right track, just needs some accuracy. You could do this:

for file in ./*.png; do  mv  "$file"  "${file%%-*}.png";  done
1
  • 1
    to understand how this works: ${FOO%%suffix} command Removes long suffix devhints.io/bash Commented Aug 6, 2021 at 6:57
0

If you have access to the perl script rename you could also use

rename -n 's/(.*).{14}(\.png)/$1$2/' *

Remove -n after testing to actually rename the files.

Notes

  • -n print what will be changed, don't actually do anything
  • s/old/new replace old with new
  • (.*) save any characters in this position for later
  • .{14} exactly 14 characters
  • (\.png) save .png
  • $1$2 print the first saved pattern and the second saved pattern
8
  • I'm running into a problem in that some filenames have a premature dash (-) resulting in too much information being trimmed. Is there a way to dictate removing the last 14 characters of the file? This would avoid any over trimming of filenames with an early dash (-) Commented Mar 12, 2017 at 13:06
  • @Frank in that case, you need another way to distinguish the part you do want from the part you don't. Is the part you want to trim always the first number, or the first number of a specific length? Commented Mar 12, 2017 at 13:08
  • Every file has a 14 character suffix. Commented Mar 12, 2017 at 13:10
  • @Frank answer edited Commented Mar 12, 2017 at 13:11
  • some file-170227-222746.png - so every file ends with 14 characters prior to the .png - if there were a command to remove exactly 14 characters (the -170227-222746 section) from the end of the file not counting the .png that would work flawlessly. To result in - some file.png Commented Mar 12, 2017 at 13:16
0

Thank you for all the help everyone.

The best solution I have is:

for file in ./*.png; do  
mv  "$file"  "${file%%-17*}.png";  done

Note the -17. This avoids the problem of files being trimmed too much when they have an early dash before the area I need trimmed.

E.g.

some file-name-170227-222746.png

Is trimmed to:

some file-name.png

Instead of:

some file.png
1
  • 1
    Now you have just moved the goal post. If "17" were such an important number then it should have clearly been pointed so in the description. Others can't divine just by looking at 3 lines what is required. Specs have to be as unambiguous as is possible. Commented Mar 12, 2017 at 15:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.