0

2001.png 2002.png 2003.png 2004.png 2005.png 2006.png

Let's say I want to programmatically rename these pics to be called:

1.png 2.png 3.png 4.png 5.png 6.png

Best way to do this with terminal? Does it involve Regex? In this case I would assume so since I'm truncating letters

  • When you say "terminal", do you mean "shell"? Which one? Did you try something? "Best way" is actually almost opinion based. It'd be useful if you showed what you tried. Is it always the first three characters you want to remove? Are they always exactly `200`? – Benjamin W. Jan 03 '18 at 05:57
  • Thank you for responding to my first stackoverflow post. Yes I mean shell. Yes I tried something--searched stackoverflow for a solution. Yes in this small use case, it's truncating 200 everytime. This post was semi-relevant: https://stackoverflow.com/questions/24102974/mac-os-x-terminal-batch-rename – Ryan Kapur Jan 03 '18 at 06:13
  • You should [edit] the question to update it with that information. – Benjamin W. Jan 03 '18 at 06:19

1 Answers1

0

You can get all the files in the current directory and change change their names by using move command. In this case, you want to take substring of the name from 3rd character(after 3rd characters 5 symbols are remaining, so take 5 characters starting with 3rd - file:3:5).

#!/bin/bash
for file in *.png; do
    new_file=${file:3:5}
    mv "$file" "${new_file%}"
done
gjiki
  • 169
  • 1
  • 6
  • Is the `%` a typo? It doesn't hurt, but it also doesn't do anything (it removes the empty string from the end of `new_file`). – Benjamin W. Jan 03 '18 at 06:31