Solved

Using tar unix command

Posted on 2007-04-09
5
564 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
  • 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

828 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