how to create tar file so that files at placed at root folder of tar.gz file - linux

I need to create targ.gz file and contents of file should be at root folder
say. I want angle and Simple files at root of tarfile shown at bottom. Any suggestions. I tried as below but extra folder is created at root
tar -zcvf tarfile.tar.gz -C /example/tarFile .
./
./angle.txt
./Simple.jar
tar -zcvf tarfile.tar.gz -C /example/tarFile .
./angle.txt
./Simple.jar

Please try this
tar -czvf tarfile.tar.gz /example/tarFile
after creating tar file use cp or mv command to move anywhere you want.

Related

Loop through directory and zip specific folder without parent path

I'm trying to make a bash script to loop through all folder in directories, and individually zip just the folder I want without all path and choose where to zip theme.
#!/bin/bash
for dir in /MyPersonalFolder/*/*/WhatIWantFolder
do
folder_number=$(basename ${dir%/*}) ### basename get the name of this folder [*] 'folder have numbers' /MyPersonalFolder/*/[*]/WhatIWantFolder
sudo tar -cf "${folder_number}-WhatIWantFolder".tar.gz --absolute-names "$dir"
mkdir -p ./backup-theme/ && sudo mv "${folder_number}-theme".tar.gz $_ ### I use this to move zipped folder to specific directory if i can choose where to zip file in the zip command line it's better
done
I can zip the folder I want, but the output zip file comes with this content:
/MyPersonalFolder/0001/0001/WhatIWantFolder
But what I need is to output the file with this path:
0001/
|___WhatIWantFolder/
I tried to change "$dir" in this line
sudo tar -cf "${folder_number}-WhatIWantFolder".tar.gz --absolute-names "$dir"
with basename ${dir%/*}
sudo tar -cf "${folder_number}-WhatIWantFolder".tar.gz --absolute-names "basename ${dir%/*}"
tar not found the folder it's come with errors
tar: e0001: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
You can cd to another directory while running tar using the --cd option.
#!/bin/bash
for dir in MyPersonalFolder/*/*/WhatIWantFolder; do
parent="${dir%/*/*}"
subdir="${dir#*/*/*}"
outfile="backup-theme/${subdir/\//-}.tar.gz"
tar --cd "$parent" -cvzf "$outfile" "$subdir"
done
The directory structure of the tar file would look something like this:
tar -tf backup-theme/0001-WhatIWantFolder.tar.gz
0001/WhatIWantFolder/
0001/WhatIWantFolder/f1

Bash script to backup modified file in a particular folder structure

I have a folder structure from there i am backing up files which are modified
Below is folder structure
/home/aaditya/customer/jiva/foo/bar/File1.txt
Using below command i want to backup File1.txt file
cd /home/aaditya/customer/jiva/foo/bar
tar -zcvf archive_backup_folder.tar.gz File1.txt
But my problem statement is that when i unzip the tar archive_backup_folder, File1.txt should be there inside customer/jiva/foo/bar/File1.txt not only File1.txt
Can anybody help me how to do that.
Just do:
cd /home/aaditya/
tar -zcvf archive_backup_folder.tar.gz customer/jiva/foo/bar/File1.txt
That is, include the path structure in the archive.
You can try something like this
TIME=`date +%b-%d-%y`
FILENAME=backup-$TIME.tar.gz
SRCDIR= directory or file that has to be backed up
DESDIR= place where you need to store it
tar -cpzf $DESDIR/$FILENAME $SRCDIR
cd /home/aaditya/
find customer -mtime -1 -exec tar -rvf test.tar {} \;
the "find" command will find out all files modified within 1 day and for each of them, run "tar -rvf test.tar the_file" to add it to test.tar (you way want to "rm -f test.tar" before running the "find" command

tar extract all files from folder inside archive

Let's say I have a tar archive with the following structure:
"NAME/FOLDER/FILES"
e.g.
tarfolder.tar.gz/test123/[file1,file2,filex]
Now I wan't to extract all files from the folder test123 without extracting the "test123" folder itself. What's the command to do so?
Try:
tar -zxfv tarfolder.tar.gz test123/ --strip-components=1
Works for me.
Use the tar zxvf option, like this:
tar -zxvf tarfolder.tar.gz test123/file1

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

Updating a single file in a compressed tar

Given a compressed archive file such as application.tar.gz which has a folder application/x/y/z.jar among others, I'd like to be able to take my most recent version of z.jar and update/refresh the archive with it.
Is there a way to do this other than something like the following?
tar -xzf application.tar.gz
cp ~/myupdatedfolder/z.jar application/x/y
tar -czf application application.tar.gz
I understand the -u switch in tar may be of use to avoid having to untar the whole thing, but I'm unsure how to use it exactly.
Well, I found the answer.
You can't use tar -u with a zipped archive. So the solution I used was the following. Note that I moved the z.jar file to a folder I created in the current directory called application/x/y for this purpose.
gzip -d application.tar.gz
tar -uf application.tar application/x/y/z.jar
gzip application.tar
When I did a tar -tf application.tar (after the update, before the gzip) it showed up properly.
If the file you want to update is text file. Then you can use vim editor directly to open the tarball that contains the file and open it, just like open folder using vim editor. Then modify the file and save it and quit.
However, if the file is a binary. I have no idea about the solution.
in my case, I had to delete the file and then add the new file with the following steps:
my tar file
file.tar
└── foo.json
└── bar.json
└── dir
└── zoo.json
and I wanted only to modify/update foo.json file without extracting and re-creating the whole tar file file.tar, Here are the commands:
tar -x -f file.tar foo.json # extract only foo.json file to my current location
# now modify the file foo.json as you want ...
tar --delete -f file.tar foo.json # delete the foo.json file from the file.tar
tar -uf file.tar foo.json # add the specific file foo.json to file.tar
compressed file:
if it is compressed file, like file.tar.gz, you will need to extract the tar file from the compressed file (in this example gzip) by using gunzip file.tar.gz which will create for you the tar file file.tar. then you will be able to do the above steps.
at the end you should compress the tar file again by using gzip file.tar which will create for you compressed file with the name file.tar.gz
sub directories:
in order to handle sub dirs you will have to keep the same structure also in the file system:
tar -x -f file.tar dir/zoo.json
# now modify the file dir/zoo.json as you want ...
tar --delete -f file.tar dir/zoo.json
tar -uf file.tar dir/zoo.json
view the file structure:
by using the less command, you can view the structure of the file:
less file.tar
drwxr-xr-x root/root 0 2020-10-18 11:43 foo.json
drwxr-xr-x root/root 0 2020-10-18 11:43 bar.json
drwxr-xr-x root/root 0 2020-10-18 11:43 dir/zoo.json

Resources