Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
Move files in S3 to folders based on filename

I have s3 folder where files are staged from an application. I need to move these files based on a specified folder structure using the filenames.

The files are named in a particular format:

s3://bucketname/staging/file1_YYYY_MM_DD_HH_MM_SS 
s3://bucketname/staging/file1_YYYY_MM_DD_HH_MM_SS

I need to move them to s3 folders of this format:

s3://bucketname/file1/YYYY/MM/DD

I have the following code now to store all the filenames present in the staging folder in a file.

path=s3://bucketname/staging
count=`s3cmd ls $path | wc -l`
echo $count

if [[ $count -gt 0 ]]; then 
    list_files_to_move_s3=$(s3cmd ls -r $path | awk '{print $4}' > files_in_bucket.txt)
        echo "exists"
else
        echo "do not exist"
fi

I now need to read the filenames and move the files accordingly. Can you please help.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can parse the contents of files_in_bucket.txt with sed to produce the output you want:

---> cat tests3.txt
s3://bucketname/staging/file1_YYYY_MM_DD_HH_MM_SS 
s3://bucketname/staging/file1_YYYY_MM_DD_HH_MM_SS

---> sed -r "s|^(s3://.*)/.*/(.*)_(.*)_(.*)_(.*)_.*_.*_.*$|\1/\2/\3/\4/\5|g" tests3.txt
s3://bucketname/file1/YYYY/MM/DD
s3://bucketname/file1/YYYY/MM/DD

---> 

What's happening there is it's parsing out each line from the file tests3.txt, with each bit inside parentheses saved as a "variable" (I'm not sure what the correct term is for sed, but you get the idea) which can then be referenced in the substitution string as \1, \2, \3, etc. So it's picking out the first bit, including up until the first slash, skipping the "staging" bit, and then picking out the file and date portions of the file name.

Note that this assumes a very standardized layout of the filenames and your desired output.

Let me know if you have any questions about this or need further help.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda