LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-08-2010, 02:22 PM   #1
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,771
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
HOWTO: find {conditions} then create tar-ball {matching files}


I feel certain there is command line magic that will enable me to
  • use find to identify a set of files
  • use tar to create an archive of all matching files

I get find to create a list of files that I really want, but
I'm dashed if I can discover how to tell tar to roll-up
all of those files. If I use find's -exec option, it runs tar for each
filename. Ugh! Telling tar that input is $(find ...) results in a command line that is correct but too long.

I think that I want to tell tar to read the list of files from STDIN,
but I cannot discover how to do that if it is possible. I thought that the option "--" double-dash but having that right-most on the tar command did not work. (I likely scrambled it somehow.)

~~~ 0;-Dan
 
Old 07-08-2010, 03:00 PM   #2
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,771

Original Poster
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
I've discovered that there is a --files-from=filename option to tar. This expects a list of files to store or extract.

This option permitted the following:
Code:
user@host:/path/ $ find ... >someFiles.log ; tar ... --files-from=someFiles.log
Notice the command is really two commands with semicolon separator.
They could be two separate lines.

Does someone know a better way?

~~~ 0;-Dan

Last edited by SaintDanBert; 07-08-2010 at 04:21 PM.
 
Old 07-08-2010, 03:23 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
No need for the log file if you can pipe the results of find through xargs and tar, e.g.
Code:
find <arguments> -print0 | xargs -0 tar --no-recursion -zcvpf archive.tar.gz
the --no-recursion option ensures that tar does not archive directories twice.
 
Old 07-10-2010, 11:29 AM   #4
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,771

Original Poster
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by colucix View Post
No need for the log file if you can pipe the results of find through xargs and tar,
...
Aarg! (sorry) xargs is one of those shell commands that I always forget about until a colleage shows me how it is exactly what I needed.

Thanks,
~~~ 0;-Dan
 
Old 07-10-2010, 11:54 AM   #5
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,771

Original Poster
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
A couple of WARNINGS about doing things this way.

1. Most likely, some of the found files will contain blanks within the filename or path. Those blanks cause confusion to xarg's blank-based delimiters. You can alter how xargs behaves with the --null or -0 option. It tells xargs to expect arguments separated with \000 instead of whitespace.

2. If you are feeding xargs from find, as I want to do, find has the -print0 option. Using this option, find reports items that match separated by \000 instead of whitespace.

As presented on the xargs man-page, an example use might be
Code:
find /tmp -name core -type f -print0 | xargs --null /bin/rm -f
This example will find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing spaces or newlines are correctly handled.

I think I can safely mark this issue solved.
Thanks to all,
~~~ 0;-Dan
 
Old 07-10-2010, 02:41 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by SaintDanBert View Post
1. Most likely, some of the found files will contain blanks within the filename or path.
This is exactly the reason why I suggested -print0 and -0 in my example. Glad to see the problem is solved. Cheers.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 05:30 PM
Create a file from two files with specific search conditions.(a bit Difficult) vysakh@gmail.com Linux - Server 4 05-06-2010 09:18 AM
Help with tar ball noelc Linux - Software 6 09-26-2009 04:06 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
Updating files with a tar ball... Arty Ziff Linux - Newbie 3 02-06-2009 11:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration