Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using tar unix command

Posted on 2007-04-09
5
Medium Priority
?
582 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 1000 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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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 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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

722 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