1

How can I compress files with the output name same as parent folder?

For example, I have a the folders:

ubup@neter:/NeuroNER-master/output$ tree
.
├── en_2017-04-28_20-26-59-439552
│   ├── 000_test.txt
│   ├── 000_test.txt_conll_evaluation.txt
│   ├── 000_train.txt
│   ├── 000_train.txt_conll_evaluation.txt
│   ├── 000_valid.txt
│   ├── 000_valid.txt_conll_evaluation.txt
│   ├── 001_test.txt
│   ├── 001_test.txt_conll_evaluation.txt
│   ├── 001_train.txt
│   ├── confusion_matrix_for_epoch_0005_in_valid_binary_evaluation.df
│   ├── confusion_matrix_for_epoch_0005_in_valid_bio_evaluation.df
│   ├── confusion_matrix_for_epoch_0005_in_valid_token_evaluation.df
│   ├── model
│   │   ├── checkpoint
│   │   ├── dataset.ickle
│   │   ├── tensorboard_metadata_characters.sv
│   ├── results.son
│   └── tensorboard_logs
│       ├── test
│       │   └── events.ut.fevents.493425627.er

I would like to compress the folder en_2017-04-28_20-26-59-439552 into en_2017-04-28_20-26-59-439552.tar.bz2

I could use with the free, open source program lbzip2:

tar -c -I lbzip2 -f en_2017-04-28_20-26-59-439552.tar.bz2 *

However I don't want the command to contain the name of the archive file (en_2017-04-28_20-26-59-439552.tar.bz2), because in my application I don't know it ahead of time.

1

I eventually used the following bash script:

for d in */ ; do
   tar -c -I lbzip2 -f "${d%/}.tar.bz2" "$d"
done

Some comments:

  • for d in */ ; do loop through directories (How do I loop through only directories in bash?)
  • ${d%/} removes the trailing slash of the folder name (it would return $d unchanged if there was no trailing slash).
  • This creates an archive for both subdirectories of the current directories, and symbolic links in the current directory that point to an existing directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.