0

I'm struggling with this task:

Write a script that takes as input a directory (path) name and a filename base (such as ".", "*.txt", etc). The script shall search the given directory tree, find all files matching the given filename, and bundle them into a single file. Executing the given file as a script should return the original files.

Can anyone help me?

First i tried to do the find part like this:

#!/bin/bash

filebase=$2
path=$1

find $path \( -name $base \)

Then i found this code for bundle, but I dont know how to combine them.

for i in $@; do
 echo "echo unpacking file $i"
 echo "cat > $i <<EOF"
 cat $i
 echo "EOF"
 done
avasal
  • 14,350
  • 4
  • 31
  • 47
sinber91
  • 1
  • 1

1 Answers1

1

Going on tripleee's comment you can use shar to generate a self extracting archive. You can take the output of find and pass it through to shar in order to generate the archive.

#!/bin/bash

path="$1"
filebase="$2"
archive="$3"

find "$path" -type f -name "$filebase" | xargs shar > "$archive"

The -type f option passed to find will restrict the search to files (i.e. excludes directories), which seems to be a required limitation.

If the above script is called archive_script.sh, and is executable, then you can call it as below for example:

./archive_script.sh /etc '*.txt' etc-text.shar

This will create a shar archive of all the .txt files in /etc.

imp25
  • 2,327
  • 16
  • 23