
Tengo un proceso FTP que constantemente coloca archivos en un directorio. La fecha de creación es parte del nombre del archivo en un formato como este:
AAAA-MM-DD-HH-MM-SS-xxxxxxxxxx.wav
Me gustaría mover los archivos a otro directorio según la fecha en que se creó el archivo. Puedo usar el nombre del archivo o la fecha, lo que sea más fácil. Sólo es necesario considerar el mes y el año. He creado directorios usando el siguiente formato:
Jan_2016
Feb_2016
He estado creando directorios y moviendo archivos manualmente, pero me gustaría automatizar esto con un script bash que creará el directorio si no existe.
Lo que he estado haciendo hasta ahora es crear manualmente los directorios y luego ejecutar este comando:
mv ./2016-02*.wav Feb_2016/
Respuesta1
### capitalization is important. Space separated.
### Null is a month 0 space filler and has to be there for ease of use later.
MONTHS=(Null Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
cd /your/ftp/dir ### pretty obvious I think
for file in *.wav ### we are going to loop for .wav files
do ### start of your loop
### your file format is YYYY-MM-DD-HH-MM-SS-xxxxxxxxxx.wav so
### get the year and month out of filename
year=$(echo ${file} | cut -d"-" -f1)
month=$(echo ${file} | cut -d"-" -f2)
### create the variable for store directory name
STOREDIR=${year}_${MONTHS[${month}]}
if [ -d ${STOREDIR} ] ### if the directory exists
then
mv ${file} ${STOREDIR} ### move the file
elif ### the directory doesn't exist
mkdir ${STOREDIR} ### create it
mv ${file} ${STOREDIR} ### then move the file
fi ### close if statement
done ### close the for loop.
Este debería ser un buen punto de partida para una persona sin experiencia. Intente escribir su guión a la luz de estas instrucciones y comandos. Puedes pedir ayuda si te quedas atascado
Respuesta2
Este guión puede ayudar. (elimine el eco de los archivos mv):
#!/bin/bash
shopt -s nullglob
month=(Jan Feb Mar May Apr Jun Jul Aug Sep Oct Nov Dec)
for y in 2016; do
for m in {01..12}; do
fn="$y-$m"
dn="${month[10#$m-1]}_$y"
[[ ! -d $dn ]] && mkdir -p "$dn"
for file in ./"$fn"*.wav; do
echo mv "$file" "./$dn/${file#\./}"
done
done
done