Tarring files exclude the path before the folder i want to tar - linux

Im trying to tar a set of subfolders and then tar its parent folder afterwards via a ruby script.
The structure is as follows:
/x/y/z/ParentFolder/Subfolder1
/x/y/z/ParentFolder/Subfolder2
/x/y/z/ParentFolder/Subfolder3
/x/y/z/ParentFolder/Subfolder4
So what i want to end up with is Subfolder1.tar.gz,Subfolder2.tar.gz,Subfolder3.tar.gz,Subfolder4.tar.gz all contained in ParentFolder.tar.gz.
My problem at the moment is that im able to tar the parent folder with its subfolders but it structure remains as /x/y/z/ParentFolder/SubFolder1----4
tarParentFolder = "tar -zcvf /x/y/z/ParentFolder.tar.gz /x/y/z/ParentFolder 2>/dev/null"
`#{tarParentFolder}`
I have searched around but cannot seem to find a solution to this,
Anybody got any ideas?
Thanks

The answer to how to get the path to be relative to the right path is to use the -C tar option. That's a capital C. The parameter you pass is the directory from which you want the tar to start relative.
so you would do:
tar -zcvf /x/y/z/ParentFolder.tar.gz -C /x/y/z ParentFolder
But ... you should also probably think twice about putting tars in tars. You should be fine just tarring up the containing dir.

For creating tar archives containing multiple files/folders use this:
$ mkdir f1 f2
$ tar -czf tar.tgz f1 f2 # creates the tar
$ tar -tzf tar.tgz # lists tar contents
f1/
f2/
f3/
$
So you should write something like:
tar -zcvf /x/y/z/ParentFolder.tar.gz /x/y/z/Subfolder{1,2,3,4}

Related

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

Having trouble compressing a file in a different directory

Okay so essentially what I'm doing, is I'm taking all the directories inside of the /servers/ folder, and moving them to a secondary hard drive mounted at /media/backupdrive/. This script is ran once a day, so it makes the directory with the name of the date, and should copy the folders directly over there (The reason I have to do it this way is because my client has limited disk space on his main hard drive and his worlds are upwards of 6-7gb each). Anyway, I can get them to copy the folders to /media/backupdrive/currentdate, but then when I try to compress it, it says it can't compress an empty directory or something along the lines of that.
Here's the code:
#!bin/bash
folderName=$(date +"%m-%d-%y")
mkdir "/media/backupdrive/$folderName"
for i in servers/*; do
cp -rf $i /media/backupdrive/$folderName/
cd /media/backupdrive/$folderName/
tar -C ${i:8} -czvf "${i:8}.tar.gz"
cd /root/multicraft/
done
Sorry for the image, it was on a virtual machine and I had to re-type it, because I couldn't copy and paste.
It looks to me like your tar command is missing its input (e.g., a final "."), and therefore says, "tar: Cowardly refusing to create an empty archive".
Your script appears to work for me with this tar command:
tar -C ${i#servers/} -czvf "${i#servers/}.tar.gz" .
I'd try a slightly different approach. tar by itself doesn't use temporary files, so you could tar the sources directly to the destination and compress them wizh gzip in a second step.
#!bin/bash
dst="/media/backupdrive/$(date +"%m-%d-%y")"
for d in servers/*; do
tarfile="$dst/${d#servers/}.tar"
tar -C "$d" -cvf "$tarfile" .
gzip -9 "$tarfile"
done

How to tar a directory but exlude a file

I want to tar.gz a directory but want to exclude a big file? The is for intermediary extraction. Dump will be written to and will want to keep.
E.g tar -zcf remote_test.tar.gz mydir/* except tsung.dump
From man tar:
--exclude=PATTERN
exclude files, given as a PATTERN
So you can use
tar -zcf remote_test.tar.gz --exclude=tsung.dump mydir/*
There's an exclude option for tar. If you want to tar everything in mydir except a tsung.dump, the command would look like this:
$ tar -zcf remote_test.tar.gz --exclude='tsung.dump' mydir

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

Make Tar + gzip ignore directory paths

Is it possible, when making a tar + gzip through the 'tar c ...' command, to have the relative paths will be ignored upon expanding?
For example,
tar cvf test.tgz foo ../../files/bar
And then expanding the test.tgz with
tar xvf test.tgz
gives a directory containing:
foo files/bar
I want the directory to contain the files:
foo bar
Is this possible?
If all the paths begin with the same initial list of directories then you can use e.g. tar cvf test.tgz -C ../.. other/dir. Beware that the shell won't expand wildcards in pathnames "properly" however because -C asks tar to change directory.
Otherwise, the only way I've ever come up with is to make a temporary directory filled with appropriate symlinks and use the -h option to dereference through symlinks. Of course that won't work if some of the files you want to store are actually symlinks themselves.

Resources