Using tar unix command

how do I create a tar file from files in a directory of the same filetype?
sikyalaSenior Database AdministratorAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
TintinConnect With a Mentor Commented:
If they are all in the same directory, then do

tar cvf /tmp/backup.tar *.txt

This will tar up all .txt files.
0
 
nixfreakCommented:
if your files have an extension like file.pdf then you can use:
find /dir "*.pdf" -print | xargs tar cf mypdf.tar
0
 
nixfreakCommented:
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
 
nixfreakCommented:
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
 
nixfreakConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.