Please help me with tar commands

Alexooisq

New Member
Joined
Feb 19, 2022
Messages
2
Reaction score
0
Credits
23
Hi guys, I'm new to linux.org. I had a scripting problem hope you guys can share your knowledge with me. Thanks in advance.
I'm creating a script.sh to archive (files only) in /var/log using tar command and save the file as log.tar in Archive folder in home directory. I also need to remove path names from the files that are archived. I should be able to list the contents of the archive without extracting and extract the files in log.tar to a backup folder in home directory.

I used to below command to find and tar all *.log files and save it as log.tar and save it in Archive folder in home.
It works but seems like it tar directories also. I need only files with *.log.

$find /var/log -name \*.log | tar -cvf ~/Archive/log.tar /var/log/

1645251061823.png


Please assist.
I'm using RHEL 8.5.
 
Last edited:


While waiting for my post to get approve, i managed to remove the path names but i think it includes directories as well. I only need files with *.log to be archive.

1645261819792.png
 
If it's GNU tar, then:

Code:
find /var/log -name \*.log -type f | tar -cvf mytar.tar --files-from=-

Otherwise:

Code:
find /var/log -name \*.log -type f | xargs tar -cvf mytar.tar

If there are spaces in any file or folder names, then this would handle it:
Code:
find /var/log -name \*.log -type f -print0 | xargs -0 tar -cvf xx

Note that the paths to each file is still saved in the tar, I presume that is OK (I don't know of a way to remove them)
 

Members online


Latest posts

Top