Copy only the most deeply nested file from a structure inside multiple compressed archives












1















I've bunch of .tgz folders in folder A. each of these has some files and tar files, and inside the tar files there are folders and sub folders.



I want to move a particular files from all these tgz,tar,folders/sub folders to a new directory



Here's an example of the directory structure:



ABC/Log_021403.tgz/Log_021403/Log_archive_130214.tar+test.log/start/collect/log/link/failure/test.log. 


I want to get only test.log files into a new folder.










share|improve this question




















  • 2





    Could you be more specific? Please edit your question to include an example of your tar structure and what you'd like to extract and where.

    – Oli
    Mar 28 '14 at 18:43
















1















I've bunch of .tgz folders in folder A. each of these has some files and tar files, and inside the tar files there are folders and sub folders.



I want to move a particular files from all these tgz,tar,folders/sub folders to a new directory



Here's an example of the directory structure:



ABC/Log_021403.tgz/Log_021403/Log_archive_130214.tar+test.log/start/collect/log/link/failure/test.log. 


I want to get only test.log files into a new folder.










share|improve this question




















  • 2





    Could you be more specific? Please edit your question to include an example of your tar structure and what you'd like to extract and where.

    – Oli
    Mar 28 '14 at 18:43














1












1








1








I've bunch of .tgz folders in folder A. each of these has some files and tar files, and inside the tar files there are folders and sub folders.



I want to move a particular files from all these tgz,tar,folders/sub folders to a new directory



Here's an example of the directory structure:



ABC/Log_021403.tgz/Log_021403/Log_archive_130214.tar+test.log/start/collect/log/link/failure/test.log. 


I want to get only test.log files into a new folder.










share|improve this question
















I've bunch of .tgz folders in folder A. each of these has some files and tar files, and inside the tar files there are folders and sub folders.



I want to move a particular files from all these tgz,tar,folders/sub folders to a new directory



Here's an example of the directory structure:



ABC/Log_021403.tgz/Log_021403/Log_archive_130214.tar+test.log/start/collect/log/link/failure/test.log. 


I want to get only test.log files into a new folder.







command-line bash scripts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 hours ago









Zanna

50.9k13137241




50.9k13137241










asked Mar 28 '14 at 18:41









user263043user263043

62




62








  • 2





    Could you be more specific? Please edit your question to include an example of your tar structure and what you'd like to extract and where.

    – Oli
    Mar 28 '14 at 18:43














  • 2





    Could you be more specific? Please edit your question to include an example of your tar structure and what you'd like to extract and where.

    – Oli
    Mar 28 '14 at 18:43








2




2





Could you be more specific? Please edit your question to include an example of your tar structure and what you'd like to extract and where.

– Oli
Mar 28 '14 at 18:43





Could you be more specific? Please edit your question to include an example of your tar structure and what you'd like to extract and where.

– Oli
Mar 28 '14 at 18:43










1 Answer
1






active

oldest

votes


















1














Here is the script you want. Enjoy!



  
#!/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="33[01;36m"
yellow="33[01;33m"
blue="33[01;34m"

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

echo -e "$turquoisenUsage: $blue$name$yellow /folder/with/compressed/filesn
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 "$turquoisenI 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 "$turquoisenThe 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 "$turquoisenThe 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 "$yellownThis 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





share|improve this answer


























  • Usage: Linux.sh /folder/with/compressed/files getting above message and only temp folder creating but nothing inside

    – user263043
    Mar 30 '14 at 5:47











  • I assume that inside /folder/with/compressed/files/ you have tgz files like file1.tgz, file2.tgz, file3.tgz. Is that correct?

    – desgua
    Mar 30 '14 at 13:50











  • yes you are right

    – user263043
    Mar 30 '14 at 15:41











  • Inside those tgz files there are tar files like "Log_archive_130214.tar" and inside those tar files are log files like startcollectloglinkfailuretest.log. Right?

    – desgua
    Mar 30 '14 at 15:59











  • If the above is right, can you send me one so I can test? (desgua@gmail.com)

    – desgua
    Mar 30 '14 at 16:00











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f440435%2fcopy-only-the-most-deeply-nested-file-from-a-structure-inside-multiple-compresse%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Here is the script you want. Enjoy!



  
#!/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="33[01;36m"
yellow="33[01;33m"
blue="33[01;34m"

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

echo -e "$turquoisenUsage: $blue$name$yellow /folder/with/compressed/filesn
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 "$turquoisenI 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 "$turquoisenThe 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 "$turquoisenThe 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 "$yellownThis 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





share|improve this answer


























  • Usage: Linux.sh /folder/with/compressed/files getting above message and only temp folder creating but nothing inside

    – user263043
    Mar 30 '14 at 5:47











  • I assume that inside /folder/with/compressed/files/ you have tgz files like file1.tgz, file2.tgz, file3.tgz. Is that correct?

    – desgua
    Mar 30 '14 at 13:50











  • yes you are right

    – user263043
    Mar 30 '14 at 15:41











  • Inside those tgz files there are tar files like "Log_archive_130214.tar" and inside those tar files are log files like startcollectloglinkfailuretest.log. Right?

    – desgua
    Mar 30 '14 at 15:59











  • If the above is right, can you send me one so I can test? (desgua@gmail.com)

    – desgua
    Mar 30 '14 at 16:00
















1














Here is the script you want. Enjoy!



  
#!/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="33[01;36m"
yellow="33[01;33m"
blue="33[01;34m"

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

echo -e "$turquoisenUsage: $blue$name$yellow /folder/with/compressed/filesn
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 "$turquoisenI 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 "$turquoisenThe 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 "$turquoisenThe 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 "$yellownThis 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





share|improve this answer


























  • Usage: Linux.sh /folder/with/compressed/files getting above message and only temp folder creating but nothing inside

    – user263043
    Mar 30 '14 at 5:47











  • I assume that inside /folder/with/compressed/files/ you have tgz files like file1.tgz, file2.tgz, file3.tgz. Is that correct?

    – desgua
    Mar 30 '14 at 13:50











  • yes you are right

    – user263043
    Mar 30 '14 at 15:41











  • Inside those tgz files there are tar files like "Log_archive_130214.tar" and inside those tar files are log files like startcollectloglinkfailuretest.log. Right?

    – desgua
    Mar 30 '14 at 15:59











  • If the above is right, can you send me one so I can test? (desgua@gmail.com)

    – desgua
    Mar 30 '14 at 16:00














1












1








1







Here is the script you want. Enjoy!



  
#!/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="33[01;36m"
yellow="33[01;33m"
blue="33[01;34m"

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

echo -e "$turquoisenUsage: $blue$name$yellow /folder/with/compressed/filesn
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 "$turquoisenI 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 "$turquoisenThe 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 "$turquoisenThe 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 "$yellownThis 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





share|improve this answer















Here is the script you want. Enjoy!



  
#!/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="33[01;36m"
yellow="33[01;33m"
blue="33[01;34m"

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

echo -e "$turquoisenUsage: $blue$name$yellow /folder/with/compressed/filesn
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 "$turquoisenI 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 "$turquoisenThe 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 "$turquoisenThe 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 "$yellownThis 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






share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 10 '14 at 22:01

























answered Mar 28 '14 at 20:46









desguadesgua

27.8k882112




27.8k882112













  • Usage: Linux.sh /folder/with/compressed/files getting above message and only temp folder creating but nothing inside

    – user263043
    Mar 30 '14 at 5:47











  • I assume that inside /folder/with/compressed/files/ you have tgz files like file1.tgz, file2.tgz, file3.tgz. Is that correct?

    – desgua
    Mar 30 '14 at 13:50











  • yes you are right

    – user263043
    Mar 30 '14 at 15:41











  • Inside those tgz files there are tar files like "Log_archive_130214.tar" and inside those tar files are log files like startcollectloglinkfailuretest.log. Right?

    – desgua
    Mar 30 '14 at 15:59











  • If the above is right, can you send me one so I can test? (desgua@gmail.com)

    – desgua
    Mar 30 '14 at 16:00



















  • Usage: Linux.sh /folder/with/compressed/files getting above message and only temp folder creating but nothing inside

    – user263043
    Mar 30 '14 at 5:47











  • I assume that inside /folder/with/compressed/files/ you have tgz files like file1.tgz, file2.tgz, file3.tgz. Is that correct?

    – desgua
    Mar 30 '14 at 13:50











  • yes you are right

    – user263043
    Mar 30 '14 at 15:41











  • Inside those tgz files there are tar files like "Log_archive_130214.tar" and inside those tar files are log files like startcollectloglinkfailuretest.log. Right?

    – desgua
    Mar 30 '14 at 15:59











  • If the above is right, can you send me one so I can test? (desgua@gmail.com)

    – desgua
    Mar 30 '14 at 16:00

















Usage: Linux.sh /folder/with/compressed/files getting above message and only temp folder creating but nothing inside

– user263043
Mar 30 '14 at 5:47





Usage: Linux.sh /folder/with/compressed/files getting above message and only temp folder creating but nothing inside

– user263043
Mar 30 '14 at 5:47













I assume that inside /folder/with/compressed/files/ you have tgz files like file1.tgz, file2.tgz, file3.tgz. Is that correct?

– desgua
Mar 30 '14 at 13:50





I assume that inside /folder/with/compressed/files/ you have tgz files like file1.tgz, file2.tgz, file3.tgz. Is that correct?

– desgua
Mar 30 '14 at 13:50













yes you are right

– user263043
Mar 30 '14 at 15:41





yes you are right

– user263043
Mar 30 '14 at 15:41













Inside those tgz files there are tar files like "Log_archive_130214.tar" and inside those tar files are log files like startcollectloglinkfailuretest.log. Right?

– desgua
Mar 30 '14 at 15:59





Inside those tgz files there are tar files like "Log_archive_130214.tar" and inside those tar files are log files like startcollectloglinkfailuretest.log. Right?

– desgua
Mar 30 '14 at 15:59













If the above is right, can you send me one so I can test? (desgua@gmail.com)

– desgua
Mar 30 '14 at 16:00





If the above is right, can you send me one so I can test? (desgua@gmail.com)

– desgua
Mar 30 '14 at 16:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f440435%2fcopy-only-the-most-deeply-nested-file-from-a-structure-inside-multiple-compresse%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

GameSpot

日野市

Tu-95轟炸機