I need a script to manipulate files based on a filename:
example filename: 66600_042706.pdf
the script will create a directory 66000 only if this directory is not existing. If that directory is existing it will just move the file to 66000/666000_042706.pdf
in addition, i want to make sure that when moving the filename to the directory and there's an existing file with the same filename, it will add a random digit e.g. 66000/66000_042706_123.pdf
#!/bin/bash
FILE=66600_042706.pdf
DIR=${FILE%%_*}
if [ -d $DIR ]
then
if [ -f $DIR/$FILE ]
then
NEWFILE=${FILE%.*}_$RANDOM.${FILE##*.}
else
NEWFILE=$FILE
fi
else
mkdir $DIR
fi
mv $FILE $DIR/$NEWFILE
FILE is a variable, i just initalized it at the beginning to have an example value.
Now you can :
Replace 1st line with FILE=$1, to have the ability to call the script like
./script 66600_042706.pdf
or put the code in a loop like :
for FILE in *.pdf
do
.....
done
---------- Post updated at 10:21 ---------- Previous update was at 10:15 ----------