1

I want to create tb2 files from bunch of text files like this. tar cfj $(basename ./2014_04_09.txt .txt).tb2 $(basename ./2014_04_09.txt)

With this command I can create a archive file 2014_04_09.tb2 that doesn't contain a directory.

But when the command is applied with -exec option of find like this,

find . -name "*.txt" -exec tar cfj $(basename "{}" .txt).tb2 $(basename "{}") \;

This command create archive files with original extension (2014_04_09.txt.tb2) and with a directory inside the archive.

How can I use tar command with find like when be applied only a single file.

  • 1
    Why create one archive per file? For a single file, you are better to just compress it with bzip2 (or gzip) rather than create an unnecessary archive which is mean for multiple files. – Graeme Apr 9 '14 at 10:03
  • I only knew bzip2 and gzip command with tar. Of course I'll use if I can simply compress a file by bzip2. Thanks! – ironsand Apr 9 '14 at 10:44
2

You need to -exec a shell command instead:

find . -name "*.txt" -exec sh -c 'tar cfj $(basename "{}" .txt).tb2 "{}"' -- {} \;

Moreover, note that your find command is finding the files at all levels. As such, using basename to pass the list of files wouldn't work if the files do not exist in the current directory.

To elaborate,

tar cfj foo.tb2 foo.txt

would result into errors if foo.txt doesn't exist in the current directory. As such, you need to pass the path to foo.txt.

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.