Precisa de script para o Ubuntu

1

Precisa de um script para usar no ubuntu

Tenho várias pastas .tgz na pasta A, cada tgz tem alguns arquivos e arquivos tar, dentro dos arquivos tar existem pastas e subpastas. Quer mover um determinado arquivo de todas essas pastas / subpastas tgz, tar para um novo diretório

Por exemplo: abaixo da estrutura do diretório (dentro do grupo ABC, os Tgzs estão lá com datas diferentes

ABC \ Log_021403.tgz \ Log_021403 \ Log_archive_130214.tar + test.log e pastas como \ start \ collect \ log \ link \ falha \ test.log. quer obter apenas arquivos test.log para uma pasta

    
por user263043 28.03.2014 / 19:41

1 resposta

1

Aqui está o script que você deseja. Apreciar!

  
#!/bin/bash
# License: GPL-2
# by desgua 
# 2014 April 10
# 
# This script is provided "as is" without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability, fitness for a particular purpose, or non-infringement.
# 
# Always backup your files

# Alias for color
turquoise="3[01;36m" 
yellow="3[01;33m" 
blue="3[01;34m" 

# Help
if [[ "$#" -ne "1" || "$@" == "-h" || "$@" == "--help" ]]; then
name=$(echo $0 | sed 's|.*/||')

echo -e "$turquoise\nUsage: $blue$name$yellow /folder/with/compressed/files\n
Please backup your files first!\n"
exit 0

fi

# Folder with the files
folder="$1"

# Make sure the folder exist
if [ ! -d "$folder" ]; then
    echo -e "$turquoise\nI didn't find the folder $folder"
    exit 1
fi

# Move into folder 
cd "$folder"

# Make a trash folder
if [ ! -d trash ]; then 
    mkdir trash
fi

# Make sure not to mess with existing temp folder
if [ -d temp ]; then 
    echo -e "$turquoise\nThe folder temp already exist. I will move it to temp-old."
    mv temp ./trash/temp-old
fi

# Create a working temp diretory
mkdir temp

# First extract all tar files from tgz files
for file in *.tgz; do
    echo -e $turquoise "Extracting from $file..."
    tar -xf "$file" -C ./temp/ --wildcards "*tar*"
    tar -xf "$file" -C ./temp/ --wildcards "*OPP*xml"
done

# Move extracted files to the root of working diretory
find "$folder/temp/" -name '*.tar' -exec mv {} ./ \;

# Extract all log files from tar files
for file in *.tar; do
    tar -xf "$file" -C ./temp/ --wildcards "*OPP*xml"
    echo -e $turquoise "Extracting $file..."
    mv "$file" ./trash/
done

# Make sure not to mess with existing folder
if [ -d logs ]; then 
    echo -e "$turquoise\nThe folder logs already exist. I will move it to logs-old."
    mv logs ./trash/logs-old/
fi

# Create the logs folder
mkdir logs

# Move logs files to logs folder 
echo -e $turquoise "Moving $file into folder ./logs/"
find "$folder/temp/" -name '*.xml' -exec mv {} "$folder/logs/" \;

# Clean up
mv temp/ ./trash/temp/

# Explain where to find the logs file
echo -e "$yellow\nThis script create a trash folder that you may want to delete now. It is $folder"trash/" 

You will find your files into folder $folder"logs/"
"

# I didn't delete the temporary folders trying to make this script more safe

exit 0
    
por desgua 28.03.2014 / 21:46