Archive only Specific Files in a Dir

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
s243a
Posts: 2580
Joined: Tue 02 Sep 2014, 04:48
Contact:

Archive only Specific Files in a Dir

#1 Post by s243a »

I created a short script to archive only specific files in a directory. This keeps one from having to clean working files (and other examples) before archiving. It is also a work-around for annoying errors like being unable to unmount a directory.

The basic code is:

Code: Select all

echo_items(){
  for item in "${code_to_include}" "${directories_to_include}"; do
      if [ -d "$item" ]; then
        find . -wholename "./$item"'/*'
      else
        echo "./$item"
      fi
  done
}
echo_items | tar -czvf "$output_name" --files-from -
https://pastebin.com/AKZkViEx

And the above link is an example of how one might use it. I could make this code more robust by adding the " -print0" option to the above find statment, in which case one would need to add the "--null" option to tar.
See:
https://stackoverflow.com/questions/259 ... from-stdin

but I would probably need to do a few more changes to the above code for this to work.

Note that when I tried this it says that tar exited with an error but the code seems to work and the archive looks like it was created. Perhaps this is due to some strange file I have in one of my directories (or maybe I need to repair my file system).

Edit 1
Actually, I'm surprised this worked because I would think that an ampersand is missing and the above line should be:

Code: Select all

for item in "${code_to_include[@]}" "${directories_to_include[@]}"; do
maybe bash lets one be a bit sloppy but with out this fix the code would fail in ash.

Post Reply