cannot tar on Mac? - macos

I am trying to compress a log file directory on my mac. The log directory exists.
For example /var/logs/my_log_dir/
I am trying to run something like:
$ tar –cpzf x.tar.gz /var/logs/my_log_dir
I also tried other variations such as compressing only one of the files in the log dir - no luck. All I get from my mac is:
Usage:
List: tar -tf <archive-filename>
Extract: tar -xf <archive-filename>
Create: tar -cf <archive-filename> [filenames...]
Help: tar --help
Thoughts?

Using -p doesn't make sense in the context of compression, since you're already shoving in whatever permissions are on the files by default. When extracting, however, you can use the -p flag if you're concerned about permissions.
Compress: tar –czvf x.tar.gz /var/logs/my_log_dir
Extract: tar –xpzvf x.tar.gz

-p option (preserve file permissions) works in x mode only.

Related

How to extract tar file in Mac terminal

As titled. I want to use some command, like for .zip files I can say
unzip myfiles.zip -d mydirectory
But is there a thing for .tar file on Mac as well?
Yes, you can run:
tar -xvf myfile.tar
For .tar.gz, you can run:
tar -xzvf myfile.tar.gz
If you want to extract to any directory other than your cwd, use -C. e.g:
tar -xvf myfile.tar -C somedirectory
I suggest you read the man page for tar if you wish to do anything further:
man tar

edit a text file in an uncompressed .tar

I have created some .tar folders that for the most part contain some text files. Is is possible to edit programmatically a text file from the command line (bash and related tools) without fully un-packing the tar?
Context: these .tars were created by a script and I realized I made a mistake. I'm looking for the most efficient and simple solution to edit a part of a single line of a text file.
You could use tar --delete and tar --update to replace a file inside a tar
i think that you won't be able do it with out descompressing. You could to do a loop for descompress only your text file, edit with sed and compress again. In other case, i think that it is impossible...
maybe something like this
xzcat blah | sed /../../ | xz blah
within the loop that runs through all the files
Thanx for the inspiration Grammy, here is a minimal example in case someone comes looking for the same.
Create some data in a folder:
cd /tmp;
mkdir dir1;
echo "foo" >> ./dir1/test_txt;
echo "bar" >> ./dir1/test_txt;
Pack in a tar:
tar -cf test_tar.tar dir1;
Delete the directory that we are going to decompress and edit.
rm -rf dir1;
And unpack only the relevant file from the tar folder and edit:
WARNING:
Tar updates only if the timestamp changed since the last edit!
This is why there is a sleep command here (to change the timestamp.).
sleep 1
tar -xf test_tar.tar dir1/test_txt
sed -i 's/foo/baar/' dir1/test_txt ;
Update the relevant file.
tar -uf test_tar.tar dir1/test_txt
Check if this worked:
tar -xf test_tar.tar dir1
cat ./dir1/test_txt
[...]$
baar
bar

tar command - compress without folders

I use a tar command like this:
tar -cf backupdb/db/2012/11/database.2012-11-25.tar.gz backupdb/db/2012/11/25/*.sql
But when I open the tar file I see a directory backupdb/db/2012/11/25 :/
How to modify the command to compress files backupdb/db/2012/11/25/*.sql but without parent directories.
I know the -C switcher in tar command, but I don't know how to use it to compress files with specify exptension.
You are looking for the -C flag:
tar -C backupdb/db/2012/11/25/ -cf backupdb/db/2012/11/database.2012-11-25.tar.gz *.sql

Creating incremental backup with tar - --listed-incremental -g

I have problem with the tar linux program. I would like to create a incremental backup. I use following tar command for the first full backup:
tar --create --gzip --listed-incremental=$SAVEDIR/backup.snar --file=$SAVEDIR/$DATE.tar.gz $EXCLUDE $DIRECTORY
$EXCLUDE contains for example "--exclude test/testdir --exclude test/testdir2"
$DIRECTORY contains "-C /Users/user1/Desktop/ test"
If I execute the command I get following error:
tar: Option --listed-incremental=/Users/hofmeister/Desktop/test/backup.snar is not supported
Usage:
List: tar -tf <archive-filename>
Extract: tar -xf <archive-filename>
Create: tar -cf <archive-filename> [filenames...]
Help: tar –help
If I change the --listed-incremental option to -g =$SAVEDIR/backup.snar. I get:
Usage:
List: tar -tf <archive-filename>
Extract: tar -xf <archive-filename>
Create: tar -cf <archive-filename> [filenames...]
Help: tar --help
What went wrong?
I use following version of tar: bsdtar 2.8.3 - libarchive 2.8.3
The problem is the tar version which is bsd. With macports you could install gnutar. Here everything works fine!
Looks like you're using bsdtar, and not the gnu tar. bsdtar doesn't support incremental backups. They have slightly different flags too.

Folder Renaming After Tar Extraction

I have a tarball, myarchive.tar.gz. When I uncompress it using "tar -zxvf myarchive.tar.gz", it creates a folder myarchive-x980-2303-ssioo. What's the easiest way to automatically rename the extracted folder to ensure it matches the name of the archive? I've checked tar's manpage, but it doesn't seem to have an option for this.
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*}
mkdir pretty_name && tar xf ugly_name.tar -C pretty_name --strip-components 1
from https://unix.stackexchange.com/questions/11018/how-to-choose-directory-name-during-untarring

Resources