Solved

Using tar unix command

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
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…
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…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now