General Category > Blogs

Linux programs and Bash scripts to consolidate photos and begin a slideshow

<< < (3/3)

Dragon:
Still sorting some things out, due to lots of duplicate files and then lots of other places where files ended up. I've used rsync to get a bunch of files back into my Photos directory on my storage drive, but many of them in the root directory didn't have file names. I found that the timestamp on many were appropriate, unlike the ones that were downloaded from Google Photos, which hadn't maintained their original timestamps. Thinking this might be a good time to get the files renamed with their date rather than leaving it up to the metadata, I've put together this script:


--- Code: ---if [ $# -ne 1 ]; then
    echo "$# args given - need one to run the process"
    exit 1
fi

#YEAR=2000
#NEXT=2001
OIFS="$IFS"
IFS=$'\n'

echo "Moving files beginning with $1"
#echo "Also moving files from $YEAR to $NEXT"

echo "" > tmp.txt
#find ./ -maxdepth 1 -type f -name "$1*.jpg" > tmp.txt
find ./ -maxdepth 1 -type f -name "$1*" >> tmp.txt
#find ./ -maxdepth 1 -type f -newermt "$YEAR-01-01 00:00:00" -not -newermt "$NEXT-01-01 00:00:00" >> tmp.txt

for file in `cat tmp.txt` 
do
if [ $file == $0 ]; then
echo "Not changing $file"
exit 1
fi
moddate=`stat --format="%y" $file | awk '{print $1}'`
# newfilename=`stat --format="%N" $file`
newfilename=${file// /_}
newfilename=${newfilename/\.\//_}
newfilename=${newfilename//:/-}
newfilename=${moddate}${newfilename}
echo $file " to " $newfilename
mv $file $newfilename

done
IFS="$OIFS"


--- End code ---

Sections commented out were helpful during testing but not what I wanted in the end.

Dragon:
Regarding my last post about my dragonRename.sh, I tried this on my iMac (MacOS Catalina). The stat function doesn't work the same on Mac as it does on my Ubuntu Linux machine, so the process failed on that stat function, throwing some "illegal option" errors.

Dragon:
I've added more to my dragonRename script. Now it will take a second argument to determine the max depth that it will go down to pick out matching files. They will be renamed and moved to the current directory, but will include the name of the directory in the new filename.


--- Code: ---#!/bin/bash
# Find files beginning with the argument given, then rename the files
# to use include the timestamp in the filename and remove spaces.
# E.G.
#-rwxrwxr-x 1 dragon dragon     15319 2000-10-25  Nathaniel1.jpg
# to be renamed 2000-10-25_Nathaniel1.jpg
#-rwxrwxr-x 1 dragon dragon     14778 2000-09-08  Tim & Autumn.jpg
# to be renamed 2000-09-08_Tim_&_Autumn.jpg


#for f in $(find ./ -maxdepth 1 -newermt "2000-01-01 00:00:00" -not -newermt "2001-01-01 00:00:00"); do
# echo "$f"
#done
if [ $# -lt 1 ]; then
    echo "$# args given - need at least one to run the process."
    exit 1
elif [ $# -gt 2 ]; then
    echo "$# args given - need one to run the process and optionally one for the directory depth."
    exit 1
elif [ $1 = "--help" ]; then
    echo "Usage: $0 <YYYY-MM-DD> [DEPTH=1]"
    echo "Find files beginning with the argument given, then rename the files "
    echo "to use include the timestamp in the filename and remove spaces. "
    exit 1
fi

dirname='./'
if [ -z "$2" ]; then
    dirdepth=1
elif [ "$2" -gt 0 ]; then
    dirdepth=$2
else
    dirdepth=1
fi

#YEAR=2000
#NEXT=2001
OIFS="$IFS"
IFS=$'\n'

echo "Moving files beginning with $1 in $dirname and maxdepth of $dirdepth"
#echo "Also moving files from $YEAR to $NEXT"

echo "" > tmp.txt
#find ./ -maxdepth 1 -type f -name "$1*.jpg" > tmp.txt
#find ./ -maxdepth 1 -type f -newermt "$YEAR-01-01 00:00:00" -not -newermt "$NEXT-01-01 00:00:00" >> tmp.txt
#find $dirname -maxdepth 1 -type f -name "$1*" >> tmp.txt
find $dirname -maxdepth $dirdepth -type f -name "$1*" >> tmp.txt

echo "Matching files found:"
cat tmp.txt
echo ""
echo "The above files will be renamed here."
read -p "Continue? [y/N]:" prompt

if [ "$prompt" = "y" ]; then
echo "Renaming files..."
else
echo "Exiting without renaming."
exit 1
fi

for file in `cat tmp.txt` 
do
if [ $file == $0 ]; then
echo "Not changing $file"
exit 1
fi
moddate=`stat --format="%y" $file | awk '{print $1}'`
# newfilename=`stat --format="%N" $file`
newfilename=${file// /_}
newfilename=${newfilename/\.\//_}
newfilename=${newfilename//:/-}
newfilename=${newfilename//\//_}
newfilename=${moddate}${newfilename}
echo $file " to " $newfilename
mv $file $newfilename

#     echo "file = $file"
#     diff "$file" "/some/other/path/$file"
#     read line </dev/tty
done
IFS="$OIFS"
find ./ -empty -type d -delete
--- End code ---

Example of use:

~/dragonRename.sh M 5
Moving files beginning with M in ./ and maxdepth of 5
Matching files found:
 
./2016-08/MVI_2507 (1).MOV
./Amazon Drive/2016-10/MVI_2921.MOV

The above files will be renamed here.
Continue? [y/N]:y
Renaming files...
./2016-08/MVI_2507 (1).MOV to 2016-08-17_2016-08_MVI_2507_(1).MOV
./Amazon Drive/2016-10/MVI_2921.MOV to 2016-10-22_Amazon_Drive_2016-10_MVI_2921.MOV

Dragon:
Here is an example that I've run using fdupes to recursively check through the Documents and Downloads directories, then delete duplicates without prompting the user (leaving the 1st found, starting in Documents), and log the results in a file on the Desktop:


--- Code: ---fdupes -R Documents Downloads -d -N >> ~/Desktop/fdupes.log.txt
--- End code ---

And here's another, checking for duplicate files recursively in the user's entire directory, logging the results, but not deleting anything.


--- Code: ---fdupes -R ~/ >> ~/Desktop/fdupes.log.txt
--- End code ---

WARNING: DO NOT just recursively delete duplicate files from your entire home directory. Using the above command to check for duplicates and find out where you might want to run it is the safe way to go, but definitely do the search without the delete at least once to find where those extra things are at. The fdupes program will find all kinds of matching files, more than just duplicate photos, so you'll want to at least run the command to check through some directories before others and probably want to avoid Library sub-directories, cache folders, and maybe even Downloads, depending on the organization of things, so that you don't end up deleting important files that might be duplicated for multiple applications. 

Navigation

[0] Message Index

[*] Previous page

Go to full version