Hi,

I am trying to write a Bash Script to sort some images but I am new to Bash and not having any luck.
I have a folder of images.

Example:
P100.jpg
P172.jpg
P342.jpg
P400.png

I need to change these filenames to <filename>_DEFAULT<extension>.
ie. P100_DEFAULT.jpg and then move the file into a folder with the name of the original file.

Pseudo:

    Load file list for directory

    For each file in the directory:
    {
        filename = <filename>
        newfilename = <filename>_DEFAULT<extension>
        mkdir <filename> #if directory doesn't exist
        mv <filename> <newdir/newfilename>
    }

NOTE: I will be running the script on MacOSX (BASH 3.2)

Any help will be appreciated.

Dani AI

Generated

Quick, safe alternative for that builds on 's suggestion but avoids common pitfalls (parsing ls, hard-coded extensions, and broken behavior on filenames with spaces). This version is macOS-friendly (Bash 3.2), operates only on the top-level files in the target folder, preserves each file's extension, creates a folder named after the base filename, appends _DEFAULT and avoids clobbering existing files by adding a small numeric suffix when needed.

#!/bin/bash
# usage: ./rename_sort.sh /path/to/dir
dir="${1:-.}"
shopt -s nullglob

for file in "$dir"/*; do
  [ -f "$file" ] || continue
  base="$(basename "$file")"
  if [[ "$base" == *.* ]]; then
    name="${base%.*}"
    ext="${base##*.}"
    dot=".$ext"
  else
    name="$base"
    ext=""
    dot=""
  fi

  mkdir -p "$dir/$name"
  new="$dir/$name/${name}_DEFAULT${dot}"
  # if target exists, add numeric suffix to avoid overwrite
  if [ -e "$new" ]; then
    n=1
    if [ -n "$ext" ]; then
      while [ -e "$dir/$name/${name}_DEFAULT_$n.$ext" ]; do n=$((n+1)); done
      new="$dir/$name/${name}_DEFAULT_$n.$ext"
    else
      while [ -e "$dir/$name/${name}_DEFAULT_$n" ]; do n=$((n+1)); done
      new="$dir/$name/${name}_DEFAULT_$n"
    fi
  fi

  printf "DRY: %s -> %s\n" "$file" "$new"
  # to perform the move, uncomment the next line:
  # mv "$file" "$new"
done

Notes: run this on a copy or in a test folder first (the script defaults to a dry run). If you want to process only particular extensions, filter the glob (e.g., "$dir"/*.jpg), but be aware that matching only one extension will skip others such as .png. This approach avoids the ls/command-substitution issues raised earlier and addresses the rename-and-move goal while handling spaces, missing extensions, and existing target files.

Recommended Answers

All 2 Replies

what you mean change the file name and copy in a folder
are you want to a move copy of your file with a new name and move in another folderor
change the orignal file extention and rename
explain

See if this helps
#!/bin/bash
if [ -d $1 ]
then
  cd $1
  for file in $(ls *.jpg)
  do
     newfile=${file%.jpg}"_DEFAULT"${file:(-4)}
     echo "New file name: $newfile"
  done
else
  echo "Directory $1 doesn't exist"
fi
Testing
$ ls ./temp
P100.jpg    P172.jpg    P342.jpg    P400.jpg
$ ./myScript temp/
New file name: P100_DEFAULT.jpg
New file name: P172_DEFAULT.jpg
New file name: P342_DEFAULT.jpg
New file name: P400_DEFAULT.jpg
$
Just add the rest
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.