I am getting some files with this mask:
aaaa_SP_bbb.txt
aaaa_FX_bbbb.txt
aaaabbbb.txt
I want to rename the files containing:
_SP_ -> Rename - >> fileSP.TXT
_FX_ -> Rename - >> fileFX.txt
and other file that does not contain these words rename them:
filenamexx.txt
as I can do?
This:
mover_fichero(){
## Compramos los ficheros y los movemos.
ls *_FX_* | awk '{system("mv " $0 " " sprintf("userbjbfx%02d", ++i))}'
ls *_SP_* | awk '{system("mv " $0 " " sprintf("userbjbsp%02d", ++i))}'
}
I currently solved temporarily. But I do not like the solution I have now.
SERVICE="BJBuser"
DIRRECIBIDOBJB="/tmp/home/usuariossftp/BJBuser/archivos/"
DIRRENOMBRADOBJB="/tmp/home/usuariossftp/BJBuser/archivos/.toMUTUACTIVOS/"
FILEOUT="datosbjb"
FICHEROS=""
LOGFILE="/tmp/var/log/BJB/BJBmove"$(date +%Y_%m)".log"
MD5SUM=""
NULL=/dev/null
i='let i=1'
cd $DIRRECIBIDOBJB
check(){
i=`ls $DIRRENOMBRADOBJB | wc -l`
}
logs(){
echo "$(date) $*: Recibido: $fichero --> Renombrado: $FILEOUT$i.txt ($MD5SUM)" >> $LOGFILE
}
compare_dir(){
FICHEROSCOMP=`ls -1 $DIRRENOMBRADOBJB`
for compfile in $FICHEROSCOMP
do
if [[ ! -r "$DIRRENOMBRADOBJB$compfile" || ! -r "$DIRRENOMBRADOBJB$fichero" ]]
then
echo "El fichero $compfile existe" >> $NULL
fi
continue
done
}
while true
do
valor=$(ps aux | grep root | grep -v grep | grep -i sshd | grep BJBuser)
# comprobamos si hay una conexion sftp del usuario BJBuser
#
if [ ! "$valor" ]
then
# echo "No hay conexion del usuario: $SERVICE por sftp"
FICHEROS=`ls -1 $DIRRECIBIDOBJB`
check;
for fichero in $FICHEROS
do
if [ ! -f $DIRRECIBIDOBJB$fichero ] # Comprueba que existe
then
echo "$FICHEROS no exite." >> $NULL
sleep 2
continue # Seguimos
fi
compare_dir;
sleep 2
let "i+=1"
if [ -f $DIRRENOMBRADOBJB$FILEOUT$i.txt ]
then
echo "$DIRRENOMBRADOBJB$FILEOUT$i.txt existe" >> $NULL
else
ls $DIRRECIBIDOBJB*_FX_* | awk '{system("mv " $0 " " sprintf(".toMUTUACTIVOS/userbjbfx%02d.txt", ++i))}'
ls $DIRRECIBIDOBJB*_SP_* | awk '{system("mv " $0 " " sprintf(".toMUTUACTIVOS/userbjbsp%02d.txt", ++i))}'
mv $DIRRECIBIDOBJB$fichero $DIRRENOMBRADOBJB$FILEOUT$i.txt
MD5SUM=`md5sum $DIRRENOMBRADOBJB$FILEOUT$i.txt`
fi
logs;
chown BJBuser $LOGFILE >> $NULL
done
else
echo "El servicio $SERVICE esta levantado" >> $NULL
fi
exit 0
done
I would like to improve.
When you rename a file containing _FX_ destination is overwritten if it exists and does not want to.
Sorry for my English, it helps me a google translator