Extracting a .tar file and creating a filelist - string

i have this little code here
for file in *.tar.gz;
do tar xzvf "${file}" && rm "${file}";
done
It extracts a tar.gz and deletes it. Now I have to create a filelist file (.fl) named like a substring from the .tar . For example, I have to delete the first 5 letters and the last 5 (the extension) from the name of the .tar.gz . And that for every .tar.gz that I extract.
Example:
I have a ABC_A.tar.gz with a ABC_A.xml in it.
I have to make a A.fl
and in that A.fl i have to write ABC_A.xml
Thanks in advance.

In your loop, you can do the following for each file:
# delete first five characters
name=${file:5}
# delete .tar.gz suffix
name=${file%%.tar.gz}

Use
for file in *.tar.gz
do
# this creates the file list into a .fl file:
tar tfz "${file}" > "${file:5:-5}.fl"
# this extracts and afterwards removes the tar archive:
tar xzvf "${file}" && rm "${file}"
done
You also can combine the two in one step:
for file in *.tar.gz
do
tar xzvf "${file}" > "${file:5:-5}.fl" && rm "${file}"
done

Related

I am working in linux and have tot extract archives that were already in an archive. Could anyone explain how to extract this while using loops?

#!/bin/bash
tar -xvf assignment_UA_InleidingProgrammeren_Huistaak1-HelloWorld_2019-11-11.tgz
a=$(echo assignment_UA_InleidingProgrammeren_Huistaak1-HelloWorld_2019-11-11.tgz | cut -b 15-35)
b=$(echo assignment_UA_InleidingProgrammeren_Huistaak1-HelloWorld_2019-11-11.tgz | cut -b 37-56)
#cutcommand van geeksforgeeks.org
mkdir -p "$a"/"$b"
mv assignment_UA_InleidingProgrammeren_Huistaak1-HelloWorld_2019-11-11/*.tgz InleidingProgrammeren/Huistaak1-HelloWorld
rmdir assignment_UA_InleidingProgrammeren_Huistaak1-HelloWorld_2019-11-11
for x in InleidingProgrammeren/Huistaak1-HelloWorld
I have already extracted the first archive but i have to extract tthe tgz archives that are in this archive without using hardcode.
I have tried using different loops but it doesn't work and i don't know if i am using them correctly.
Assumptions:
you have an archive, a.tgz, containing some files.
you have an archive, b.tgz, containing some files.
both a.tgz and b.tgz are themselves contained in another archive, top.tgz.
both a.tgz and b.tgz do not exist outside top.tgz when the script starts.
t.tgz
    - a.tgz
         - some files
    - b.tgz
         - some files
Script:
#!/bin/bash
tar -xzf top.tgz
rm -f top.tgz
for F in *.tgz
do
tar -xzf "$F"
rm -f "$F"
done
Extract the top archive first.
Delete that top archive (or move it somewhere else) so the for F in *.tgz does not process it again.
Then loop on the new archives and extract them.
Final result, all files from a.tgz and b.tgz are available.

Extracting specific file types from all tar files from specific folder

I need shell script which accept two arguments.
First one is path to the specific folder and second one is int value (1 or 2).
If second argument is 1 then I have to go through all tar files in mentioned folder and extract just executable files into specific folder inside path from first argument. in this case name of that folder is "unpacked".
If second argument is 2 then I have to extract all *.txt files from all tar files from folder given by first argument.
I am trying something like this but don't know how to catch every tar file and extract one of these two file types.
#!/bin/bash
cd $1
if [$2 –eq 1 ]
then
for f in *.tar; do
tar –xv –f "$f" –-wildcards EXECUTABLE FILES -C ./unpacked
done
fi
if [$2 –eq 2 ]
then
for f in *.tar; do
tar –xv –f "$f" –-wildcards "*.txt" -C ./unpacked
done
fi
The [MEMBER...] argument must come last.
#!/bin/bash
cd $1
if [$2 –eq 1 ]
then
for f in *.tar; do
tar –xv –f "$f" –-wildcards -C ./unpacked EXECUTABLE FILES
done
fi
if [$2 –eq 2 ]
then
for f in *.tar; do
tar –xv –f "$f" –-wildcards -C ./unpacked "*.txt"
done
fi
To Extract specific files form a tar file execute in yor terminal:
$ tar -zxvf TARNAME.tar.gz PATH/FILNAME

extract file using bash in one tar file but composed of many tar files inside

I have created a script below and when I execute it, I face a problem. Instead to extract it 5 times it will just extract once. So how to get this issue resolved?
i=0
for tarfile in *.tar.gz;
do
((i++))
[ $i = 5 ] && break ;
tar -xzvf $tarfile
done
rm -rvf $tarfile
Help is greatly appreciated. I want to extract the tar.gz file and inside of it is only tar.gz file. Noted: it is decompressed 5 times and I want to get the last tar.gz file decompressed. Please help me.
If and only if the filenames for each of the nested tar do not collide, you can do the following.
for i in `seq 1 4`; do name=$(ls *.tar.gz); tar xvfz $name; rm $name; done;
If they are all the same, like foo.tar.gz inside foo.tar.gz inside foo.tar.gz, you can simply delete the rm $name from the above.
If there are only some collisions you will have to play more clever tricks, such as moving intermediate files into another directory.
Update: If you just want to move the final tar to a new directory, just do use mv after the loop above.
mv *.tar.gz OtherDirectoryName

Unix unzip: how to batch unzip zip files in a folder and save in subfolders?

Say if I have a folder 'images' and inside it there are 0001.zip to 9999.zip, I want to unzip all of them and save them in subfolder which has their file name, for example, 0001.zip will be unzipped and saved to /0001, 0002.zip will be unzipped and saved to /0002, I tried to do
unzip '*.zip'
but that extracts all files in current folder.
You could do something like this:
for file in *.zip; do
dir=$(basename "$file" .zip) # remove the .zip from the filename
mkdir "$dir"
cd "$dir" && unzip ../"$file" && rm ../"$file" # unzip and remove file if successful
cd ..
done
or, run it together on one line:
for file in *.zip; do dir=$(basename "$file" .zip); mkdir "$dir"; cd "$dir"; unzip ../"$file" && rm ../"$file"; cd ..; done
If you need/want to keep the original .zip files, just remove the && rm ../"$file" bit.
for zip in *.zip
do
unzip "$zip" -d "${zip%.zip}"
done

linux shell tar unwanted extra directories

I have the following problem:
I have directorties a/b/c and inside c many text files.
I want to make a .tar.gz file in drectory a/mydir with the c directory inside and then unzip it to that same directory to create a/mydir/c (with all the files inside)
I am at directory a and run: (shell)
~:$ tar -czf mydir/output.tar.gz b/c
~:$ tar -zxf mydir/output.tar.gz -c mydir
but the result is directories a/mydir/b/c (with the files inside)
The problem is I don't want directory b in the middle, just c with all its contents
This works for me. Create data
mkdir -p a/b/c
echo 42 > a/b/c/file.dat
Archive
tar zc -f c.tar.gz -C a/b c
created a/b/c directories, from directory a kindly try this command.
so the file under b/c/files were done out.tar.gz
new directory "mydir" create under "b" and files extracted too.
out.tar.gz removed from "a".
# tar -cvzf out.tar.gz b/c/* ; mkdir -p b/mydir ; tar -xvzf out.tar.gz -C b/mydir/ ; rm -rf out.tar.gz
Thanks!

Resources