Skip to content

Instantly share code, notes, and snippets.

@Kedrigern
Created April 3, 2012 17:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kedrigern/2294052 to your computer and use it in GitHub Desktop.
Save Kedrigern/2294052 to your computer and use it in GitHub Desktop.
BASH: Sorted files from gived dirs by file's sufixes. And it is little bit complicated than just copy (for example see arg LIMIT etc.).
#!/bin/bash
# Author: Ondřej Profant, 2012
# Email: ondrej.profant <> gmail.com
# Licence: GPL
# Sorted files from gived dirs by file's sufixes. And it is little bit complicated than just copy (for example see arg LIMIT etc.).
# I use it as final step after PhotoRec recovery app.
# Sorry for Czech texts.
# TODO:
# -- cmd args
# -- clean code
# -- code comments
PREFIXES=""; # prefix for directories
PREFIXESDEF="TRUE recup_dir FALSE all_directories"
SUFIXES=""; # sufixes for files
SUFIXESDEF="TRUE txt FALSE tx? TRUE doc TRUE docx TRUE rtf TRUE xls TRUE xlsx TRUE ppt TRUE pptx TRUE pdf TRUE html TRUE xml TRUE zip TRUE rar TRUE gif TRUE jpg TRUE png TRUE bmp TRUE mp3 TRUE waw TRUE exe TRUE java TRUE jar TRUE dll TRUE sys TRUE ttf TRUE lnk TRUE tex TRUE c TRUE h"
DIRS=""; # dirs
TARGET=""
COMMAND="mv"
LOG=
LIMIT=5000
UNIQUE=20;
function suf {
SUFIXES=`zenity \
--list --checklist \
--height=380 \
--separator=' ' \
--title "Krok 1: Co" \
--text "Vyberte, které typy souborů, chcete roztřídit:" \
--column "Zpracovat" --column "Sufix" \
$SUFIXESDEF ;`
LOG="$LOG \n\tFile sufixes: $SUFIXES"
}
function dirs {
PREFIXES=`zenity \
--list --checklist \
--separator=' ' \
--title "Krok 2: Odkud" \
--text "Vyberte předpony složek, které budou prohledávány:" \
--column "Zpracovat" --column "Prefix" \
$PREFIXESDEF ;`
if echo $PREFIXES | grep -q "all_directories" ; then
PREFIXES='*';
fi;
LOG="$LOG \n\tDirs Prefixes: $PREFIXES"
}
function chTarget {
mkdir -p sorted
TARGET=`zenity \
--file-selection \
--directory \
--filename="sorted" \
--title="Vyberte složku do které se zachráněná data uloží"`
LOG="$LOG \n\tTarget dir: $TARGET"
}
function sourceDirs {
for d in ${PREFIXES} ; do
DIRS="$DIRS ${d}*"
done;
LOG="$LOG \n\tSource dirs: $DIRS"
}
function browseAndCopy {
for d in ${DIRS} ; do
if [ -d $d ]; then
cd $d;
for p in $SUFIXES ; do
count=0;
$COMMAND *".${p}" "${TARGET}/${p}/" 2> /dev/null
count=$((count+1)) ;
if [ `ls "${TARGET}/${p}/" | wc -l` -gt $LIMIT ]; then
mv "${TARGET}/${p}/" "${TARGET}/${p}-${UNIQUE}"
mkdir -p "${TARGET}/${p}"
UNIQUE=$((UNIQUE+1));
fi;
done;
# other:
$COMMAND * "${TARGET}/other/" 2> /dev/null
if [ `ls "${TARGET}/other/" | wc -l` -gt $LIMIT ]; then
mv "${TARGET}/other/" "${TARGET}/other-${UNIQUE}"
mkdir -p "${TARGET}/other"
UNIQUE=$((UNIQUE+1));
fi;
cd ..;
fi;
done;
}
function prepareDirs {
if [ -z $TARGET ]; then
exit 1;
fi;
for p in $SUFIXES ; do
mkdir -p "${TARGET}/${p}"
done;
mkdir -p "${TARGET}/other"
}
function showLog {
echo "[info] Počítá se s unikátními názvy, přepisování stejným názvem není řešeno."
LOG="$LOG \n\tFiles in one dir limit: $LIMIT"
echo -e "Souhrn:$LOG"
if zenity --title "V pořádku?" --question --text="<b>Souhrn:</b>\n$LOG \n\nZatím nebyly provedeny změny,\n<b>chcete pokračovat?</b>"; then
:
else
echo "[info] Pro jiné parametry spusťte program znovu"
exit 0;
fi;
}
suf;
dirs;
chTarget;
prepareDirs;
sourceDirs;
showLog
browseAndCopy;
if zenity --question --title="Úklid" --text="Smazat prázdné složky?"; then
rmdir ${TARGER}/* 2> /dev/null
rmdir $DIRS 2> /dev/null
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment