سؤال

When doing tar extract operations sometimes the contents extract directly to the parent folder which is awful since they get all disordered. By example:

tar -xzf foo1.tar.gz

Extracts:

./file1
./file2
./file3

The solution to that is to create an specific folder for the tar.gz By example:

tar -C foo -xzf foo1.tar.gz

Extracts:

./foo1/file1
./foo1/file2
./foo1/file3

So if I have many .tar.gz to extract I will just do:

find -name \*.tar.gz | xargs -n 1 basename -s .tar.gz | xargs -I {} tar -C {} -xzf {}.tar.gz

To extract them safely, but with that I'll endup with things like this:

./foo3/foo3/file1
./foo3/foo3/file2
./foo3/foo3/file3

Is there an automatic way to remove the duplicated child folder with bash for those cases that need it?

هل كانت مفيدة؟

المحلول 2

This is a possible solution

To move files from the child to the toplevel directory:

find ! -name . -maxdepth 1 -type d | xargs -I {} sh -c "find {}/{} -maxdepth 1 | xargs -I [] echo \"mv [] {}\""

This works because if the child folder doesnt exists find would do nothing:

find foo4/foo4 
find: 'foo4/foo4': No such file or directory

To remove reapted child directory

find ! -name . -maxdepth 1 -type d | xargs -I {} find {}/{} -type d -maxdepth 1 | xargs rmdir

I think it did what it was intended for. Disadvantage:

It shows many harmless errors.

نصائح أخرى

Count the number of entries in the directory (hint: dotglob, (*) and ${#var[@]}), and if the single directory entry has the same name then copy the attributes and move the contents (beware that you may have something in that directory may also have the same name), then remove the now-empty directory.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top