0

This does not work... I am still rather bad with sh scripts.

for imageFolder in *; do
    [ -d $imageFolder ] && cd "$imageFolder" && 
        for file in *.*
        do
          echo "$file" #"${file/?/?}" //the bit I am stuck on I believe.
        done
done

I have the following folder structure and I would like all files to start with 1 rather then 0

name

So the folder structure would look like this

enter image description here

2

I don't believe you will find a ${file/?/?} parameter substitution that produces a numeric sequence - especially not a per-folder numeric sequence. Instead you will need to set and increment a variable.

What parameter substitution (specifically, ${file##*.}) will be useful for is extracting the file extension, so that you can re-add it to the sequential file name. So for example you could do:

#!/bin/bash

shopt -s nullglob

for imageFolder in */; do 
  cd "$imageFolder" || continue
  n=1
  for file in *.*; do 
    echo mv -- "$file" "$((n++)).${file##*.}"
  done
  cd -
done

or (with a subshell)

#!/bin/bash

shopt -s nullglob

for imageFolder in */; do 
(
  cd "$imageFolder" || exit
  n=1
  for file in *.*; do 
    echo mv -- "$file" "$((n++)).${file##*.}"
  done
)
done
  • Thank you very much Steeldriver!! What would be simple with JS is much harder for me in bash :) – Jamie Hutber Nov 29 '19 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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