Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing every file in a directory after opening it bash

Looking around I didn't see exactly what I was looking for. Some similar stuff, but for some reason what I tried so far hasn't worked.

My main goals:

  1. run script in my current directory
  2. open the picture to see what it is
  3. rename the picture i just viewed
  4. repeat the process without running the script again

These were the sources I attempted to follow:

Bash Shell Loop Over Set of Files

Bash loop through directory and rename every file

How to do something to every file in a directory using bash?

==================================================================================

echo "Rename pictures. Path"
read path
for f in $path
do  
    eog $path
    echo "new name"
    read newname
    mv $path $newname
    cat $f
done
like image 892
Scott Avatar asked Jan 21 '26 04:01

Scott


1 Answers

You should pass the script an argument rather than trying to make it interactive. You also have numerous quoting problems. Try something like this instead (untested):

#!/usr/bin/env bash

moveFile() {
    local newName=
    until [[ $newName ]]; do
        printf '%s ' 'new name:'
        read -er newName # -e implies Bash with readline
        echo
    done
    mv -i "$1" "${1%/*}/${newName}"
}

if [[ ! -d $1 ]]; then
    echo 'Must specify a path' >&2
    exit 1
fi

for f in "$1"/*; do
    eog "$f"
    moveFile "$f"
done
like image 114
ormaaj Avatar answered Jan 23 '26 21:01

ormaaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!