How to Rename Files You Put into a Tar Archive Using Linux 'Tar'

how to rename files you put into a tar archive using linux 'tar'

You can modify filenames (among other things) with --transform. For example, to create a tape archive /tmp/foo.tar, putting files /etc/profile and /etc/bash.bashrc into it while also renaming profile to foo, you can do the following:

tar --transform='flags=r;s|bar|foo|' -cf file.tar file1 file2 bar fubar /dir/*

Results of the above is that bar is added to file.tar as foo.

The r flag means transformations are applied to regular files only. For more information see GNU tar documentation.

You can use --transform multiple times, for example:

tar --transform='flags=r;s|foo|bar|' --transform='flags=r;s|baz|woz|' -cf file.tar /some/dir/where/foo/is /some/dir/where/baz/is /other/stuff/* /dir/too

How to rename .tar.gz file without extracting the contents and creating the new .tar.gz file in UBUNTU?

The easiest is to simply rename ("move") the file:

mv Existing.tar.gz New.tar.gz

Rename Directory Name Before tar Happens

Which tar?

GNU Tar accepts a --transform argument, to which you give a sed expression to manipulate filenames.

For example, to rename during unpacking:

tar -zxf my-dir.tar.gz --transform s/my-dir/your-dir/

BSD tar and S tar similarly have an -s argument, taking a simple /old/new/ (not a general sed expression).

Folder Renaming After Tar Extraction

Manually create folder, and strip components from tarball:

archive=my.tar.gz
mkdir ${archive%.tar*}
tar --extract --file=${archive} --strip-components=1 --directory=${archive%.tar*}

How to extract, rename and view some log files from user inputed tar filename?

try this:

#!/bin/bash
fname=$1

if [[ -f "${fname}.tar" ]]; then
tar -xvf "${fname}.tar"
fi

mv tftpboot $fname

then ./extract.sh cubelog_457890

is it possible to create a tar of any file which will give different file name after untar

Try the following set/sequence of commands:
assuming your original file is a.b_backup...you can try this..

$ tar -cvf test.tar a.b_backup
$ tar --transform='s/_backup//' -xf test.tar
$ ls
a.b
a.b_backup


Related Topics



Leave a reply



Submit