PDA

View Full Version : Linux bash script to move images/filetypes to their directories



Fli
12-18-2014, 11:51 PM
U have folder with subfolders full of images or other files with different extensions (filetypes) ?

and you want to for example move all .jpg to jpg folder all .png to png folder
example:
find .jpg in /path/to/images and move them into /path/to/images/jpg

this Linux bash script can do it:


# "This script asks for path/directory with various images or other file types.
# When provided path, it will find all images or other set file types and for each filetype create a log file with full paths of all filetype matching files.
# Then script will list contents of these log files and asks if images/filetypes should be moved to their own directories (like .jpg to jpg folder)
#
# Script can be executed also with bigger automation without prompts by adding parameters after script like:
# sh /path/to/this/script /path/to/images/folder"

clear
if [ "$1" == "" ];then
echo "Please input path (directory) where are images located. Example input: /home/lapsang/images (without trailing slash). To use current path ($(pwd)), just hit Enter"
read srcpath

if [ "$srcpath" == "" ];then
srcpath=$(pwd)
fi
else
srcpath="$1"
fi

filetypes="jpg
gif
png"

# delete logfiles if exist to prevent duplicate lines
for filetype in $filetypes;do rm -rf $srcpath/$filetype/_img_move_log_$filetype;done

# search for filetypes and create log files
for filetype in $filetypes;do
mkdir $srcpath/$filetype 2> /dev/null
echo "Searching for $filetype images in $srcpath and writing file pathes into $srcpath/$filetype/_img_move_log_$filetype"
find $srcpath -type f -iname "*.$filetype" -exec echo "{}" >> $srcpath/$filetype/_img_move_log_$filetype \;
done

echo ""
echo "DONE. Now listing last 5 lines in each log file:"
echo ""
for filetype in $filetypes;do
echo "LOGFILE of $filetype images ($srcpath/$filetype/_img_move_log_$filetype)"
tail -n5 $srcpath/$filetype/_img_move_log_$filetype
echo ""
done

echo "-------------------------------------"
echo "Found images? Move filetypes in $srcpath to their own directories?
.jpg to $srcpath/jpg
.gif to $srcpath/gif
.png to $srcpath/png
?
(Before continuing make sure you have $srcpath folder backup!)"
echo "Continue now? Move images? c = continue or any other key to exit"
read movenow
if [ "$movenow" == "c" ];then

for filetype in $filetypes;do
echo "Creating destination directory $srcpath/$filetype for all $filetype images:"
mkdir $srcpath/$filetype;ls $srcpath/$filetype
echo "Moving $filetype images to $srcpath/$filetype"
find $srcpath -type f -iname "*.$filetype" -exec mv {} $srcpath/$filetype \;
echo ""
echo "It should be complete now. There can be many errors, but check if images was moved properly. if not, restore your backup."
done

else
echo "No images moved. Log files (mentioned above) was not deleted by this script."
exit
fi

by default it works with .jpg, .gif, .png , but one can edit and change file types
I made this script so after execution it do not do any harmfull action without asking.

- it asks for path where to do search
- it create log file for each file type (default jpg, gif, png) without asking
- it show these log files contents and asks if it should move files in set directory to filetype directories
example:
find .jpg in /path/to/images and move them into /path/to/images/jpg
nothing else is done