I have a list of files I need to tar. Typically, I would use

tar -cvf allfiles.tar -T mylist.txt

But I discovered that the files I need are scattered into subdirectories and my list only has the file names not the paths.

Is there a way to combine "find" with the tar command to find the files on my list wherever they are filed in subdirectories?

migrated from stackoverflow.com Jan 3 '13 at 16:50

This question came from our site for professional and enthusiast programmers.


You can find the paths using your list of names and find, but you may be caught by a little corner case, given when you have the files with the same name in different directories.

If it does not really matter, you can do the following:

find <main_directory> | grep -f mylist.txt

And then, use the list to your tar command. The complete command may look like:

tar cvf allfiles.tar -T <(find <main_directory> |
    grep -f mylist.txt --color=never)

Notice you must use --color=never, otherwise, if colors are active for your grep, you'd be sending invalid input for tar.

To ignore case distinctions, in this case, in grep, which is the one actually filtering files, you can simply add the flag -i:

tar cvf allfiles.tar -T <(find <main_directory> |
    grep -i -f mylist.txt --color=never)
  • Makes sense. Is there any way to tell tar to parse the list case-insensitive? People weren't very careful with directory naming since this is a windows server. – Steve Jan 3 '13 at 15:11
  • @Steve see if the last command line works for you. – Rubens Jan 3 '13 at 15:16
  • What does the < do before the (find...)? – Steve Jan 3 '13 at 15:16
  • 2
    @Steve when you execute a command, bracing it with <(), you're telling the shell to consider the output of the command being executed as a file itself, and it returns you a file descriptor -- which is, then, given as the <file> parameter to tar ... -T <file>. Please, notice the line break is simply in order to avoid the scroll bar up there; there's no line break in the command itself. – Rubens Jan 3 '13 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.