1

Is there an elegant one-liner to tar/archive the whole directory and delete it afterwards? So far I came up with bulky solutions.

from:

# ls -la
drwxr-xr-x 3 root root 4096 Jul  4 15:01 .
drwxr-xr-x 6 root root 4096 Jul  4 14:54 ..
drwxr-xr-x 4 root root 4096 Jul  4 14:59 stresstest

to:

# ls -la
drwxr-xr-x 3 root root 4096 Jul  4 15:01 .
drwxr-xr-x 6 root root 4096 Jul  4 14:54 ..
drwxr-xr-x 4 root root 4096 Jul  4 14:59 stresstest.tar.gz

EDIT #1

The problem is I do not know the name of the directory in advance (it is dynamically created, so next time it will be different), but it will always be the only directory here.

1
  • You say you have a solution (but it is too big). But you don't show it. Jul 5, 2018 at 10:16

2 Answers 2

2
$ for i in *;do tar zcvf ${i}.tgz $i; rm -fr $i;done

Example

$ for i in *;do tar zcvf ${i}.tgz $i; rm -fr $i;done
stresstest/
stresstest/file1
stresstest/file2
stresstest/file3

$ ll
total 4
-rw-r--r-- 1 root root 162 Jul  5 05:18 stresstest.tgz
0
0
(dir="stresstest"; tar zcvf ${dir}.tgz ${dir}; rm -rf $dir)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .