Download, extract and copy file from a folder that has a version in its name - linux

I'm writing a bash script that downloads an compressed archive from an universal URL (in which new release of the software will automatically be presented), extracts it and copies a file called wimboot to a folder.
This is what I currently have:
sudo curl http://git.ipxe.org/releases/wimboot/wimboot-latest.tar.gz -o ./wimboot.tar.gz
sudo tar -zxvf ./wimboot.tar.gz #Extracts into a folder called "wimboot-2.5.2-signed", in it is the file I need ("wimboot").
cd ./wimboot*/
sudo cp wimboot /my-folder/
But this doesn't work. Is there a method that will allow me to do this?

You can ask tar for a file listing (-t option), which you can then grep for wimboot -- that should give you the relative path to the file also. A naive first try would be something like:
src_file=$(tar tf wimboot.tar.gz | grep wimboot)
cp "$src_file" my_folder/
But you will probably want to add some error checking and stuff to that. And probably a more complicated grep expression to ensure you get the one thing you're after.
There's also no need to extract the entire archive. You can just ask tar to extract the file you're interested in:
tar zxf wimboot.tar.gz "$src_file"

I've built on top of the other answers and this worked for me:
#Create a temporary folder, this folder must be empty!
sudo mkdir temp
#Download the archive and save it in the temporary folder.
sudo curl http://git.ipxe.org/releases/wimboot/wimboot-latest.tar.gz -o ./temp/wimboot-latest.tar.gz
#Extract the downloaded archive to the temporary folder.
sudo tar xvf ./temp/wimboot-latest.tar.gz -C ./temp
#Search for and copy files with the name "wimboot" to the web directory.
sudo find ./temp/ -name 'wimboot' -exec cp {} /var/www/ \;
#Delete the temporary folder.
sudo rm -Rf temp
I do not recommend this for large archives.

Related

Remove a file has the same name with folder after using tar

I created a script to find files, move them to a folder, compress this folder using tar. Then delete original folder. But after running the script, the folder was removed but a file having same name with the folder was created. I tried to run one by one command, it's OK. There is not this file. I added rm command in the script to remove it but not working.
I don't know why this file was create.
My script is below:
#!/bin/bash
cd /home/tuan/testrm/9/
mkdir compressdir
sudo find . -type f -name "*.txt" -print | xargs -I {} mv {} compressdir > /dev/null 2>&1 &
tar -cvzf compresslog.tar.gz --remove-files compressdir
mv compresslog.tar.gz compresslog-`date +"%d%m%Y"`.tar.gz
rm -rf compressdir
I want to know why this file was create and how to prevent this happen.
You should remove & at the end of sudo find line, and it will works.
Because of The & makes the command run in the background.
Root cause: the sudo find command line and tar -> mv -> rm run at synchronized.
If you had checked the file compresslog.tar.gz which the script generated, you will found that it was null or error, and the compress file contain the same content with someone file which you find.

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.

Can tar extraction erase brother directory ?

I made several backups on different directories with Backup Manager. Eg: /home/user1 /home/user2...
It gives me some tar files. The content of a tar file looks like :
home/user1/
home/user1/.profile
home/user1/.bash_history
home/user1/.bash_logout
...
I tried to test the restoration with something like :
tar -xvzf home.user1.tar.gz -C home/user1
But the command above recreate all the structure inside the choosen directory. That gives /home/user1/home/user1/filname1.
So I guess I should use the command specifying the home directory (/home) instead of the user directory. But is there any risk to erase other user's directories in /home ?
Thks for your time.
Actually tar does not erase data as a default. But any files that are contained within the tar archive will overwrite files of the same name if they are already present. Likewise a sub-directory's contents will not be overwritten if the tar archive does not contain files matching them.
mkdir -p foo/bar/
touch foo/file1 foo/bar/file1
tar -cf foo.tar foo/
rm -rf foo
mkdir -p foo/bar/
touch foo/file2 foo/bar/file2
tar -xf foo.tar
ls foo foo/bar/
As once can see both file1 and file2 are present and the newly unarchived directory did not overwrite the old. Here is the output of ls from my system:
foo:
bar file1 file2
foo/bar/:
file1 file2

My script does not unzip file

I downloaded a file from a server in the format .tar.gz
my script has the commands
tar -zxvf data.tar.gz
rm -rf data.tar.gz
When I run this manually it unzips it and displays the contents, and then it deletes the file data.tar.gz
However, after running this from the script, when I go in to the folder where it is saved, the file data.tar.gz is there, but none of its contents are present
What I do not get is why it does not unzip or get deleted.
Part of the script:
OUT=$(date +%Y%m%d -d yesterday)-blah.gz
wget ftp://blah:blah#ftp.haha.com/"$OUT" -O /myFolder/Documents/"$OUT"
tar -zxvf /myFolder/Documents/"$OUT"
#whent the file is unzipped it produces 2 files called abcd and efgh
#because i dont need abcd or the original zipped file
rm -rf /myFolder/Documents/"$OUT"
rm -rf /myFolder/Documents/abcd
OK SO ANOTHER UPDATE: When I run this with cron, it does not work, However, when I bash run it, it works.
The problem you have is that you're not specifying the output directory for tar. By default, it outputs the file from where you're executing the script. You have several fixes available, but the easiest is:
OUT=$(date +%Y%m%d -d yesterday)-blah.gz
wget ftp://blah:blah#ftp.haha.com/"$OUT" -O /myFolder/Documents/"$OUT"
cd /myFolder/Documents/
tar -zxvf "$OUT"
#whent the file is unzipped it produces 2 files called abcd and efgh
#because i dont need abcd or the original zipped file
#remember you cd'd here, after we're done ...
rm -rf "$OUT"
rm -rf abcd
#go back
cd -
Its seem you don't launch your script from the directory your archive is in,
You must use cd command in your script to move to that directory and then launch tar and rm command
Edit:
tar -zxvf /myFolder/Documents/"$OUT"
tar will extract the archive in the current directory.

How do I tar a directory without retaining the directory structure?

I'm working on a backup script and want to tar up a file directory:
tar czf ~/backup.tgz /home/username/drupal/sites/default/files
This tars it up, but when I untar the resulting file, it includes the full file structure: the files are in home/username/drupal/sites/default/files.
Is there a way to exclude the parent directories, so that the resulting tar just knows about the last directory (files)?
Use the --directory option:
tar czf ~/backup.tgz --directory=/home/username/drupal/sites/default files
Hi I've a better solution when enter in the specified directory it's impossible (Makefiles,etc)
tar -cjvf files.tar.bz2 -C directory/contents/to/be/compressed .
Do not forget the dot (.) at the end !!
cd /home/username/drupal/sites/default/files
tar czf ~/backup.tgz *
Create a tar archive
tar czf $sourcedir/$backup_dir.tar --directory=$sourcedir WEB-INF en
Un-tar files on a local machine
tar -xvf $deploydir/med365/$backup_dir.tar -C $deploydir/med365/
Upload to a server
scp -r -i $privatekey $sourcedir/$backup_dir.tar $server:$deploydir/med365/
echo "File uploaded.. deployment folders"
Un-tar on server
ssh -i $privatekey $server tar -xvf $deploydir/med365/$backup_dir.tar -C $deploydir/med365/
To gunzip all txt (*.txt) files from /home/myuser/workspace/zip_from/
to /home/myuser/workspace/zip_to/ without directory structure of source files use following command:
tar -P -cvzf /home/myuser/workspace/zip_to/mydoc.tar.gz --directory="/home/myuser/workspace/zip_from/" *.txt
If you want to tar files while keeping the structure but ignore it partially or completely when extracting, use the --strip-components argument when extracting.
In this case, where the full path is /home/username/drupal/sites/default/files, the following command would extract the tar.gz content without the full parent directory structure, keeping only the last directory of the path (e.g. files/file1).
tar -xzv --strip-components=5 -f backup.tgz
I've found this tip on https://www.baeldung.com/linux/tar-archive-without-directory-structure#5-using-the---strip-components-option.
To build on nbt's and MaikoID's solutions:
tar -czf destination.tar.gz -C source/directory $(ls source/directory)
This solution:
Includes all files and folders in the directory
Does not include any of the directory structure (or .) in the final product
Does not require you to change directories.
However, it requires the directory to be given twice, so it may be most useful in another script. It may also be less efficient if there are a lot of files/folders in source/directory. Adjust the subcommand as necessary.
So for instance for the following structure:
|- source
| |- one
| `- two
`- working
the following command:
working$ tar -czf destination.tar.gz -C ../source $(ls ../source)
will produce destination.tar.gz where both one and two (and sub-files/-folders) are the first items.
This worked for me:
gzip -dc "<your_file>.tgz" | tar x -C <location>
For me -C or --directory did not work, I use this
cd source/directory/or/file
tar -cvzf destination/packaged-app.tgz *.jar
# this will put your current directory to what it previously was
cd -
Kindly use the below command to generate tar file without directory structure
tar -C <directoryPath> -cvzf <Path of the tar.gz file> filename1 filename2... filename N
eg:
tar -C /home/project/files -cvzf /home/project/files/test.tar.gz text1.txt text2.txt
tar -Cczf ~/backup.tgz /home/username/drupal/sites/default/files
-C does the cd for you

Resources