how to tar without the parent directory but with the last directory - linux

I am trying to tar a directory without the parent directories and I have found a solution so far which removes the parent directories and tar all the files under a directory called (DIR) to a DIR.tar.gz file using the following:
tar -cvzf $DIR.tar.gz -C $path/$DIR .
But if I want to untar the tared file, it will untar all the files in the current directory, however, I want to be able to tar the dir in a way which when I use tar -xzvf, it will untar it into folder called DIR. IT should be simple, but I am not able to find the solution on the net. Any hint?

I believe what you want is:
tar -cvzf $DIR.tar.gz -C $path ./$DIR
This should change the directory to your folder path (-C) and only include the relative directory ($DIR) that you're trying to archive.

Related

Tar command keeps bundling up entire directory path

I have a few sub-directories with files inside each of them in /home/user/archived/myFiles that I'm trying to bundle into a single tar file. The issue is, it keeps bundling a full directory path instead of just everything in the myFiles folder.
When I untar the file, I just want all the bundled sub-directories/files inside to appear in the directory I extracted the file rather than having to go through a series of folders that get created.
Instead, when I currently untar the file, I get a "home" folder and I have to go through /home/user/archived/myFiles to reach all the files.
I tried using the -C flag that I saw suggested online here Tar a directory, but don't store full absolute paths in the archive where you insert parameters for the full directory minus the last folder, and then the name of the last folder which contains all the stuff you want bundled. But the tar command doesn't work as I get a no such file or directory error.
#!/bin/bash
archivedDir="/home/user/archived/myFiles"
tar -czvf "archived-files.tar.gz" "${archivedDir}"/*
rm -vrf "${archivedDir}"/*
# Attempt with -C flag
#tar -cvf "${archivedDir}/archived-files.tar.gz" -C "${archivedDir}" "/*"
So for example, if I did an ls on /home/user/archived/myFiles, and it listed two directories called folderOne and folderTwo, and I ran this bash script and did an ls on /home/user/archived/myFiles again, that directory should only contain archived-files.tar.gz.
If I extracted the tar file, then folderOne and folderTwo would appear.
As I explain already here you should first change to this directory and then create the archive.
So change you script to something like:
archivedDir="/home/user/archived/myFiles"
cd $archivedDir
tar -czvf "../archived-files.tar.gz" *
This will create the archive in upper directory so you will not remove it with the next command.
the extraction should be something like:
archivedDir="/home/user/archived/myFiles"
cd $archivedDir
tar -xzvf "../archived-files.tar.gz"

How to create a tar archive from an absolute path and package files as if they were relative?

On Linux, I am trying to create a .tar.gz archive from a different directory, that is I have a bash script that will be executed from a different directory. The script will package the folder, I will give the absolute directory of the folder say /home/user1/Documents/folder1 however when it packages the tar file, it puts the entire absolute directory in the archive, whereas I only want the relative one from folder1.
For example:
tar czf /home/user1/Documents/folder1.tar.gz /home/user1/Documents/folder1
This will create an archive but where the first folder will be home and then inside that user1 inside that documents and inside that the folder1, no other subfolders from other branches of course.
Also the console gives this error:
tar: Removing leading `/' from member names
I want it to be packaged as if I would execute the command from the same folder, so the only folder in the archive should be folder1, and inside that it's own subfolders.
So the archive inside should look just as if I would have executed this code from the same directory folder1 is in:
tar czf folder1.tar.gz folder1
You can use the -C option to change the directory before performing any operations:
tar czf /home/user1/Documents/folder1.tar.gz -C /home/user1/Documents folder1
Now, the contents of your archive will look like this:
$ tar tf /home/user1/Documents/folder1.tar.gz
folder1/
folder1/file1
The message you get is not an error, by the way. It's tar telling you that it made the paths in the archive relative so as to avoid overwriting files, which could easily happen when unpacking an archive with absolute paths. You can turn off the leading slash removal with -P, but you often don't want that.

Exclude directories from tar archive with a .tarignore file

In a similar way to .gitignore, is it possible to do that a .tarignore file in a subdirectory makes it excluded from archive when doing
tar cfjvh backup.tar.bz2 .
(I know the --exclude parameter of tar but here it's not what I want).
Using the --exclude-ignore=.tarignore option causes Tar to use .tarignore files in a similar way that git uses .gitignore files. Use the same syntax as you would usually use in .gitignore files, so if you want to ignore all sub-directories and files in a particular directory, add a .gitignore file with * to that directory. For example:
$ find dir
dir
dir/a
dir/b
dir/c
dir/dir1
dir/dir1/1a
dir/dir1/1b
dir/dir2
dir/dir2/2a
dir/dir2/2b
$ echo "*" > dir/dir1/.tarignore
$ tar -c --exclude-ignore=.tarignore -vjf archive.tar.bz2 dir
dir/
dir/a
dir/b
dir/c
dir/dir1/
dir/dir2/
dir/dir2/2a
dir/dir2/2b
If you want to ignore all directories that have an empty .tarignore file, take a look at the --exclude-tag-all=.tarignore option.
$ find dir
dir
dir/a
dir/b
dir/c
dir/dir1
dir/dir1/1a
dir/dir1/1b
dir/dir2
dir/dir2/2a
dir/dir2/2b
$ touch dir/dir1/.tarignore
$ tar -c --exclude-tag-all=.tarignore -vjf archive.tar.bz2 dir
tar: dir/dir1/: contains a cache directory tag .tarignore; directory not dumped
dir/
dir/a
dir/b
dir/c
dir/dir2/
dir/dir2/2a
dir/dir2/2b
Also take a look at the --exclude-tag= and --exclude-tag= options, which are variations which have slightly different behaviors.
You can do something like
$ COPYFILE_DISABLE=true tar -c --exclude-from=.tarignore -vzf archiveName.tar.gz
The breakdown:-
Reason for COPYFILE_DISABLE=trueis for making sure files starting with ._ are not included in the .tar file made. Enable it permanently adding export COPYFILE_DISABLE=true to ~/.bash_profile on your Mac if you frequently do tar operations.
–exclude-from=.tarignore: Ignore all files and folders listed in .tarignore
You can have the following content in your .tarignore, here is mine
$ cat .tarignore
.DS_Store
.git
.gitignore
I just found out that git has a tool to package the tracked files itself:
git archive. Using that is probably the easiest way.
EDIT: If you want to make a package out of files / data that is not yet commited (but tracked), using tar -czf files.tar.gz $(git ls-files) works.

Create a .tar.bz2 file Linux

On my Linux machine, I wish to create a .tar.bz2 file of a certain folder. Once I place myself in that folder (in the terminal), what do I type in the terminal command line to place the compressed folder in the home directory of my machine?
Let's say I am in the folder /home/user/folder. In the folder "folder" are several files (txt, .c etc). How do I compress that folder of type .tar.bz2 and place it in my /home directory?
In the /home/user/folder, I've tried sudo tar -cvjSf folder.tar.bz2 but get an error:
tar: Cowardly refusing to create an empty archive
You are not indicating what to include in the archive.
Go one level outside your folder and try:
sudo tar -cvjSf folder.tar.bz2 folder
Or from the same folder try
sudo tar -cvjSf folder.tar.bz2 *
Try this from different folder:
sudo tar -cvjSf folder.tar.bz2 folder/*
tar cvzf file.tar.gz *.c OR tar cvzf file.tar.gz *
for more read this article https://www.geeksforgeeks.org/tar-command-linux-examples/

Compress files and directories

I need to compress certain files and a directory. Suppose they're placed in /root/project.
The thing is that i need to compress them in a gzip-tarball format with certain name (name.tar.gz) and in the "root" directory, i mean, that as soon as i opened the .tar.gz all the files and directories i want to compress are there.
I have tried using the following commands:
tar czfv name.tar.gz /root/project
tar czfv name.tar.gz /root/project/*
but then the whole substructure gets compress (i mean, when i open the .tar.gz i have to navigate through the directories root/project.. which i dont, i need that the files were as soon as i opened in "/" i suppose)
I hope i've explained myself... excuse me for my bad english and thanks in advance!
If you are using GNU tar you can use the following command:
tar -C /root/project -zcvf /root/name.tar.gz .
The -C causes tar to change to the /root/project directory before adding the . directory to the archive. Make sure the destination directory for your archive is not in the directory you are archiving (/root/project). This example creates the archive in /root.
If you want the filenames in the tarball to all have a project/ prefix (which is often advisable), do
(cd /root && tar czvf `pwd`/name.tar.gz project)
If you don't want a prefix at all, do
(cd /root/project && tar czvf `pwd`/name.tar.gz .)
(The pwd captures the present working directory prior to doing the cd; the parentheses execute the command in a subshell so your current shell stays in the same directory.)

Resources