In tar how can I exclude dir contents but leave empty dir

I want to create a tar file excluding certains folders who have many sub-folders and files, but leaving only the main folders that's being excluded. Explaining:

-project
  |-dir1
  |-dir2
  |-cache
  |-logs
  |-dir3
  --dir4

I want my tar to exclude cache and logs content (files and folders) but mantain this empty folders in the tar file.

if I use --exclude=project/cache it excludes the folders from the file. How Can I do this ?


Solution 1:

You can exclude the files in the directories in question without excluding the directories themselves with --exclude=project/cache/*.

Be mindful that if there are dotfiles in those directories, they will still get picked up in the tar; if that's the case, you may need to work on a more sophisticated regexp.