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.
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)