how to tar last few lines - tar

Is it possible to create a tar of only the last few lines of a file?
Something like this does not seem to be working.
tail abc.xml | tar -zcf bac.tar.gz
I am trying to keep the compressed file size as small as possible. I do also want to transfer it over the network as fast as possible.

You can have standard input as the source for tar but what do you want to do? There is no need to create a tar archive for just a single file. You can pipe directly to gzip:
tail abc.xml | gzip - > bac.gz
bac.gz will then contain the last 10 lines of your file (compressed).
But I suspect that your question does not reflect what you want to achieve: you really want to send the last part of the XML file as a compressed gzip file?

Related

Exclude a directory from `podman/docker export` stream and save to a file

I have a container that I want to export as a .tar file. I have used a podman run with a tar --exclude=/dir1 --exclude=/dir2 … that outputs to a file located on a bind-mounted host dir. But recently this has been giving me some tar: .: file changed as we read it errors, which podman/docker export would avoid. Besides the export I suppose is more efficient. So I'm trying to migrate to using the export, but the major obstacle is I can't seem to find a way to exclude paths from the tar stream.
If possible, I'd like to avoid modifying a tar archive already saved on disk, and instead modify the stream before it gets saved to a file.
I've been banging my head for multiple hours, trying useless advices from ChatGPT, looking at cpio, and attempting to pipe the podman export to tar --exclude … command. With the last I did have small success at some point, but couldn't make tar save the result to a particularly named file.
Any suggestions?
(note: I do not make distinction between docker and podman here as their export command is completely the same, and it's useful for searchability)

What does "dump" mean in the context of the GNU tar program?

The man page for tar uses the word "dump" and its forms several times. What does it mean? For example (manual page for tar 1.26):
"-h, --dereferencefollow symlinks; archive and dump the files they point to"
Many popular systems have a "trash can" or "recycle bin." I don't want the files dumped there, but it kind of sounds that way.
At present, I don't want tar to write or delete any file, except that I want tar to create or update a single tarball.
FYI, the man page for the tar installed on the system I am using at the moment is a lot shorter than what appears to be the current version. And the description of -h, --dereference there seems very different to me:
"When reading or writing a file to be archived, tar accesses the file that a symbolic link points to, rather than the symlink itself. See section Symbolic Links."
P.S. I could not get "block quote" to work properly in this post.
File system backups are also called dumps.
—#raymond-chen, quoting GNU tar manual

tarring and untarring between two remote hosts

I have two systems that I'm splitting processing between, and I'm trying to find the most efficient way to move the data between the two. I've figured out how to tar and gzip to an archive on the first server ("serverA") and then use rsync to copy to the remote host ("serverB"). However, when I untar/unzip the data there, it saves the archive including the full path name from the original server. So if on server A my data is in:
/serverA/directory/a/lot/of/subdirs/myData/*
and, using this command:
tar -zcvf /serverA/directory/a/lot/of/subdirs/myData-archive.tar.gz /serverA/directory/a/lot/of/subdirs/myData/
Everything in .../myData is successfully tarred and zipped in myData-archive.tar.gz
However, after copying the archive, when I try to untar/unzip on the second host (I manually log in here to finish the processing, the first step of which is to untar/unzip) using this command:
tar -zxvf /serverB/current/directory/myData-archive.tar.gz
It untars everything in my current directory (serverB/current/directory/), however it looks like this:
/serverB/current/directory/serverA/directory/a/lot/of/subdirs/myData/Data*ext
How should I formulate both the tar commands so that my data ends up in a directory called
/serverB/current/directory/dataHERE/
?
I know I'll need the -C flag to untar into a different directory (in my case, /serverB/current/directory/dataHERE ), but I still can't figure out how to make it so that the entire path is not included when the archive gets untarred. I've seen similar posts but none that I saw discussed how to do this when moving between to different hosts.
UPDATE: per one of the answers in this question, I changed my commands to:
tar/zip on serverA:
tar -zcvf /serverA/directory/a/lot/of/subdirs/myData-archive.tar.gz serverA/directory/a/lot/of/subdirs/myData/ -C /serverA/directory/a/lot/of/subdirs/ myData
and, untar/unzip:
tar -zxvf /serverB/current/directory/myData-archive.tar.gz -C /serverB/current/directory/dataHERE
And now, not only does it untar/unzip the data to:
/serverB/current/directory/dataHERE/
like I wanted, but it also puts another copy of the data here:
/serverB/current/directory/serverA/directory/a/lot/of/subdirs/myData/
which I don't want. How do I need to fix my commands so that it only puts data in the first place?
On serverA do
( cd /serverA/directory/a/lot/of/subdirs; tar -zcvf myData-archive.tar.gz myData; )
After some more messing around, I figured out how to achieve what I wanted:
To tar on serverA:
tar -zcvf /serverA/directory/a/lot/of/subdirs/myData-archive.tar.gz -C /serverA/directory/a/lot/of/subdirs/ myData
Then to untar on serverB:
tar -zxvf /serverB/current/directory/myData-archive.tar.gz -C /serverB/current/directory/dataHERE

Tar subdirectories separately

I need to tar a bunch of files, actually ~60 Millions. They are ordered in year/month/day directories. Every day has ~700 files. Is there a "neat" way of taring first the daily directories then put them into monthly tared dirs and finally tar them to yearly directories?
Of course I can try and write a script to do that, but I thought perhaps there is something "out there" or even an inbuilt function that I can use for this task.
ok, not nice to answer your own question, nevertheless here is what I did just using the command line:
for m in /mypath/<year>/*;do for d in ${m}/*;do tar -cf <year>`basename $m``basename $d`.tar $d;done;done
as result I get daily tar files in the form: yyyymmdd.tar
To then tar them to monthly or yearly tar files is then not so difficult.
Any other, more elegant solutions are always welcome.

Need a little help, I need correct syntax for grep and copy

I currently have 3TB of data on a disk with small to medium files in hundreds of folders.
I need to find certain text files witch contain certain words ( more than one word ).
I've already tried grep-ping for them.
This works as it prints the path to every file.
But this is a long list and I'm now looking for a workable way to copy them to another folder.
any ideas ?
Is there some way to put -exec cp -rv /estinationfolder in the syntax and have it copy all results to the folder ?
Yes , certainly there is a way.
You can pipe the grep output to copy command and provide required destination directory.
Here is a example,
find . -type f | xargs grep -l "textToSearch" | cpio -pV $destination_path
this script will copy files to destination path provided in destination_path variable
Best part with this is, it will copy the files while preserving the full path.

Resources