Solved

Using tar unix command

Posted on 2007-04-09
5
570 Views
Last Modified: 2013-12-27
how do I create a tar file from files in a directory of the same filetype?
0
Comment
Question by:sikyala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 7

Expert Comment

by:nixfreak
ID: 18878060
if your files have an extension like file.pdf then you can use:
find /dir "*.pdf" -print | xargs tar cf mypdf.tar
0
 
LVL 7

Expert Comment

by:nixfreak
ID: 18878245
For finding files of type html without filename extension. You can run the below script.
script.sh dir | xargs tar czf myhtml.tgz

#!/bin/sh

filetype=html

cd $1
ls -1 | while [ true ] ; do
    read fl
    [ -z "$fl" ] && exit
    file $fl | grep $filetype 2>&1 > /dev/null && echo $fl
done
0
 
LVL 7

Expert Comment

by:nixfreak
ID: 18878408
Small modification:
Example: script.sh "html" dir | xargs tar czf myhtml.tgz

#!/bin/sh

cd $2 && find . -type f -print | while [ true ]
do
    read fl
    [ -z "$fl" ] && exit
    file "$fl" | grep $1 2>&1 > /dev/null && echo "$fl"
done
0
 
LVL 48

Accepted Solution

by:
Tintin earned 250 total points
ID: 18879322
If they are all in the same directory, then do

tar cvf /tmp/backup.tar *.txt

This will tar up all .txt files.
0
 
LVL 7

Assisted Solution

by:nixfreak
nixfreak earned 250 total points
ID: 18880221
I made a typo in the first post:
find /dir -name "*.pdf" -print | xargs tar cf mypdf.tar

BTW, find is useful if you want to archive files from subdirectories too (which tar cvf file.tar *.txt wont match).
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question