How to Strip Path While Archiving with Tar

How to strip path while archiving with TAR

In your "Extraction phase" you can use the strip-components flag like

tar xvf tarname.tar --strip-components=n

which will remove the first n leading components of the file name. Although if you have different file-path-components this will not work for all cases.

If you want to do it while archiving, only one thing comes to mind, and I will share

INPUT: list of files + full paths

1) for each line, split the path out of the filename

2) execute cd to that path and tar on that filename

3) repeat for each line

tar file preserves full path. how to stop it?

you can cheat..

exec('cd /path/to/tmp_folder/ && tar -cvf /path/to/myfile.tar innerfolder/');

This would would give your users just the innerfolder when they extracted the tarball

Tar a directory, but don't store full absolute paths in the archive

tar -cjf site1.tar.bz2 -C /var/www/site1 .

In the above example, tar will change to directory /var/www/site1 before doing its thing because the option -C /var/www/site1 was given.

From man tar:

OTHER OPTIONS

-C, --directory DIR
change to directory DIR

How can you archive with tar in NodeJS while only storing the subdirectory you want?

You need to change working directory:

// cwd The current working directory for creating the archive. Defaults to process.cwd().
new tar.Pack({ cwd: "./public/images" });
const source = Readable.from('');

Source: documentation of node-tar

Example: https://github.com/npm/node-tar/blob/main/test/pack.js#L93

How do I tar a directory of files and folders without including the directory itself?

cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd - 

should do the job in one line. It works well for hidden files as well. "*" doesn't expand hidden files by path name expansion at least in bash. Below is my experiment:

$ mkdir my_directory
$ touch my_directory/file1
$ touch my_directory/file2
$ touch my_directory/.hiddenfile1
$ touch my_directory/.hiddenfile2
$ cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd ..
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2
$ tar ztf my_dir.tgz
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2

how to suppress directory names in tar archived files?

Using GNU tar and tar --help:

File name transformations:

  • --strip-components=NUMBER

    strip NUMBER leading components from file names on extraction

  • --transform=EXPRESSION, --xform=EXPRESSION

    use sed replace EXPRESSION to transform file names

On the face of it, therefore, you use it at extract time:

tar -xf filename.tar --strip-components=1 -C somewhere

The -C somewhere changes directory to somewhere before extracting files.


Since you know about the --strip-components option but it doesn't meet your requirements, the next option to try is --transform. Since you want to remove all leading components of the path, then the expression can be quite simple:

tar -cf filename.tar --transform='s%.*/%%' .

This seems to work; I tested it using:

$ mkdir junk
$ cd junk
$ mkdir inc src
$ for file in inc/foo.h inc/bar.h inc/more.h src/foo.cc src/bar.cc more.cc; do cp ../makefile $file; done
$ tar -cf - --transform='s%.*/%%' . | tar -tvf -
drwxr-x--- jleffler/eng 0 2014-05-06 10:00 ./
-rw-r----- jleffler/eng 1183 2014-05-06 10:00 more.cc
drwxr-x--- jleffler/eng 0 2014-05-06 10:00 inc/
-rw-r----- jleffler/eng 1183 2014-05-06 10:00 more.h
-rw-r----- jleffler/eng 1183 2014-05-06 10:00 bar.h
-rw-r----- jleffler/eng 1183 2014-05-06 10:00 foo.h
drwxr-x--- jleffler/eng 0 2014-05-06 10:00 src/
-rw-r----- jleffler/eng 1183 2014-05-06 10:00 bar.cc
-rw-r----- jleffler/eng 1183 2014-05-06 10:00 foo.cc
$

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

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.

Extract tar archive excluding a specific folder and its contents

You can use '--exclude' to omit a folder:

tar -xf archive.tar -C /home/user/target/folder" --exclude="folderC"


Related Topics



Leave a reply



Submit