1

I am learning Bash and therefore I would like to write a script with runs over my files and names them after the current directory.

E.g. current_folder_1, current_folder_2, current_folder_3...

#!/bin/bash

# script to rename images, sorted by "type" and "date modified" and named by current folder

#get current folder which is also basename of files
basename=$(basename "$PWD");
echo "Current folder is: ${basename}";
echo '';

#set counter for iteration and variables
counter=1;
new_name="";
file_extension="";

#for each file in current folder
for f in *
do
  #catch file name
  echo "Current file is:           ${f}"
  #catch file extension
  file_extension="${f##*.}";
  echo "Current file extension is: ${file_extension}"
  #create new name
  new_name="${basename}_${counter}.${file_extension}"
  echo "New name is:               ${new_name}";


  #mv $f "${new_name}";
  echo "Counter is:                ${counter}"
  ((counter++));
done

One of my two problems is I would like to sort them by first type and then date_modified before running the for-each-loop.

enter image description here

Something like

for f in * | sort -k "type" -k "date_modified"
[...]

I'd appreciate some help.

EDIT1: Solved the sorting by date problem with

for f in $(ls -1 -t -r)
AndreasInfo
  • 1,062
  • 11
  • 26
  • I noticed the edited question and just want to check if I understood it correctly. Does my answer sort it the way you'd like? – Ted Lyngmo Oct 16 '20 at 15:05
  • Thanks for you help, I updated the question and answer. – AndreasInfo Oct 27 '20 at 11:38
  • Changing the fundamental base of a question isn't really nice since it invalidates answers you've already gotten that actually answers the question as it was originally posed. If you get an answer to your question it'd be better to accept it and ask a new question if you decide to change the requirements. – Ted Lyngmo Oct 27 '20 at 11:43
  • .... also, I don't see the change in the question that makes your answer valid? – Ted Lyngmo Oct 27 '20 at 11:44

3 Answers3

1

This could be a start. I've commented in the code where I think it's needed but please ask if anything is unclear.

#!/bin/bash

#get current folder which is also basename of files
folder=$(basename "$PWD");
echo "Current folder is: ${folder}";
echo

#set counter for iteration
counter=1;

for f in *
do
    file_extension="${f##*.}";

    # replace all whitespaces with underscores
    sort_extension=${file_extension//[[:space:]]/_}

    # get modification time in seconds since epoch
    sort_modtime=$(stat --format=%Y "$f")

    # output fed to sort
    echo $sort_extension $sort_modtime "/$f/"

    # sort on extension first and modification time after
done | sort -k1 -k2n | while read -r dummy1 dummy2 file
do
    # remove the slashes we added above
    file=${file:1:-1}

    file_extension="${file##*.}";

    new_name="${folder}_${counter}.${file_extension}"

    echo "moving \"$file\" to \"$new_name\""
    #mv "$file" "$new_name"

    (( counter++ ))
done
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

ls -lt | sort

The -t parameter will sort by date and time, and sort should sort by type of file.

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5
  • Thanks a lot, "ls -1 -t -r" served the time and date. Type though I was hoping I could just different between Image and Video, so .jpg and .png are treated the same way. – AndreasInfo Oct 14 '20 at 12:44
0

I decided to do a workaround with two loops, one for images and one for video.

#used for regex
shopt -s extglob

#used to get blanks in filenames
SAVEIFS=$IFS;
IFS=$(echo -en "\n\b");
[...]

image_extensions="(*.JPG|*.jpg|*.PNG|*.png|*.JPEG|*.jpeg)"
video_extensions="(*.mp4|*.gif)"
[...]
for f in $(ls -1 -t -r *${media_file_extensions})
[...]
for f in $(ls -1 -t -r *${video_extensions})
[...]

#reverse IFS to default
IFS=$SAVEIFS;

AndreasInfo
  • 1,062
  • 11
  • 26
  • I see your point and I did not want to invalidate your answer. Sorry about that. But the workaround with two loops is a workaround answering the original problem as well, no? – AndreasInfo Oct 27 '20 at 13:19
  • Don't worry. As you said, this is a Q&A site and we should try to maintain it like that. I forgot some lines on top which make this approach work. You find the whole script here: https://stackoverflow.com/questions/64537789/bash-command-does-not-work-in-script-but-in-console/64538171#64538171 – AndreasInfo Oct 27 '20 at 13:38
  • Ah, yes, I can see that this would sort .jpg and .png's together and .mp4 and .gif's together although I suspect you'll have problems with files with whitespaces and directories. – Ted Lyngmo Oct 27 '20 at 14:01