Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda