Often after unzipping a file I end up with a directory containing nothing but another directory (e.g.,
mkdir foo; cd foo; tar xzf ~/bar.tgz may produce nothing but a
bar directory in
foo). I wanted to write a script to collapse that down to a single directory, but if there are dot files in the nested directory it complicates things a bit.
Here's a naive implementation:
mv -i $1/* $1/.* . rmdir $1
The only problem here is that it'll also try to move
.. and ask
overwrite ./.? (y/n [n]). I can get around this by checking each file in turn:
IFS=$'\n' for file in $1/* $1/.*; do if [ "$file" != "$1/." ] && [ "$file" != "$1/.." ]; then mv -i $file . fi done rmdir $1
But this seems like an inelegant workaround. I tried a cleaner method using
for file in $(find $1); do mv -i $file . done rmdir $1
find $1 will also give
$1 as a result, which gives an error of
mv: bar and ./bar are identical.
While the second method seems to work, is there a better way to achieve this?