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

155
Visualizações
Find and move files to appropriate directories using shell script

I have below setup and I want to find and move files.

I have files /home/backup/abc/123.wav and /home/backup/xyz/456.wav. Same directories exist at /usr/src/abc and /usr/src/xyz which does not have any files.

I want to find .wav files from home_dir and move them to particular dest_dir.

So 123.wav should move to /usr/src/abc and 456.wav should move to /usr/src/xyz. I am using below command for that.

home_dir=/home/backup/
dest_dir=/usr/src/
cd $home_dir && find . -iname "*.wav" -exec mv {} $dest_dir \;

But all the .wav files(123.wav and 456.wav) moved to /usr/src and not to its respective directories(/usr/src/abc and /usr/src/xyz).

Is it possible to achieve what I want ? Please suggest.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Use cp --parents option with find to create parent directories of each file being copied:

home_dir=/home/backup/
dest_dir=/usr/src/
cd "$home_dir"

find . -iname "*.wav" -exec cp --parents {} "$dest_dir" \; -delete
over 4 years ago · Santiago Trujillo Relatório

0

This would be a lot easier if mv had a --parents option, but unfortunately it doesn't. It's best to use mv instead of cp because cp will copy all the data unnecessarily if the source and destination directories are on the same filesystem. if you've got Bash 4 (which supports globstar) you could do it like this:

home_dir=/home/backup/
dest_dir=/usr/src/

shopt -s globstar nullglob dotglob

for src_wav in "$home_dir"/**/*.wav ; do
    rel_wav=${src_wav#$home_dir/}
    dst_wav=$dest_dir/$rel_wav
    dst_parent=${dst_wav%/*}
    [[ -d $dst_parent ]] || mkdir -p -- "$dst_parent"
    mv -- "$src_wav" "$dst_wav"
done
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