tar: Directory: Cannot rmdir: Directory not empty

Viewed 529

I'm trying to understand what's the proper way of using tar. I have a directory called main and in the sub directory it contains some files and directories I want to exclude. All others I want to remove. The command I use:

tar --remove-files --exclude main/sub/.runtime --exclude main/sub/.startTime --exclude main/sub/subsubdir -zcvf main.tar.gz main

But I get:

tar: main/sub: Cannot rmdir: Directory not empty
tar: Exiting with failure status due to previous errors

Now of course it the main/sub directory is not empty (because of the excluded files/directory). What's the proper way to resolve it without redirecting output to /dev/null or some other way? I want to still get errors but just not that one.

2 Answers

How about first:

cd main

and then

tar --remove-files --exclude sub/.runtime --exclude sub/.startTime --exclude sub/subsubdir -zcvf main.tar.gz *

?

That should prevent the subdirectory errors, also if you are using it in a script or with an alias:

( cd main;tar --remove-files --exclude sub/.runtime --exclude sub/.startTime --exclude sub/subsubdir -zcvf main.tar.gz * )

The ( prevents it from doing anything to the parent shell, so you stay in your directory

It may be a bug for --remove-files option.

Instead of excluding files, you can include files :

#!/usr/bin/env bash

shopt -s dotglob extglob nullglob
tar -zcvf main.tar.gz main/!(sub) main/sub/!(*@(.runtime|.startTime|subsubdir))

Add --remove-files once you are satisfied with result.

Related