If not, then just rename your working directory (the one that was empty) and move that to the desired location. If it includes a top level directory, then you just move that to the desired destination. The easiest (laziest) way to do that is to always unpack a tar archive into an empty directory. The best "solution", however, is to prevent the problem in the first place. Once one of these "explodes" on you, the solutions in the other answers are way better than what I would have suggested. That kind of (antisocial) archive is called a tar bomb because of what it does. One other thing: you may need to use the tar option -numeric-owner if the user names and/or group names in the tar listing make the names start in an unpredictable column. The $perms variable is used only for the dry run so really only the $dirent substring needs to be proper. Note: if that initial dry run output doesn't look right, you should be able to fiddle with the numbers in the two substr function calls until they look proper. I prefer this kind of cleanup because it doesn't destroy anything that isn't already destroyed by being overwritten by that initial tar xv. If it is, you can now clean up your mess with: $ sh fixup.sh Have a peek through this script to confirm that it's all kosher. The fixup.sh script will be the shell commands that will move the top-level files and directories into a "clean" folder (in this instance, the folder called cleanup). $ tar tvf myarchive.tar | perl -clean=cleanup > fixup.sh If that looks good, then run it again like this: $ mkdir cleanup You should get output like: -rw-rw-r-|batch This will confirm that your tar list is like mine. Save this to the file and then execute it like this: $ tar tvf myarchive.tar | perl -dry Print "mv -i '$dirent' '$clean_folder'/.\n" # Emit the shell code to clean up the folder # If we're in "dry run" mode, just list the permissions and the directory # Drop entries that are in subdirectories # Strip out permissions string and the directory entry from the 'tar' list # Process the "tar tv" listing and output a shell script. # Protect the 'clean_folder' string from shell substitution Here's a possibility that will take the extracted files and move them to a subdirectory, cleaning up your main folder. And have backups, eat your breakfast, brush your teeth, etc. And perhaps try rm -i to confirm everything. Shut this up with 2>/dev/null if it annoys you, but I'd prefer to keep as much information on the process as possible.Īnd don't do it until you are sure that you match the right files. Rmdir: failed to remove `file': Not a directory This will generate a lot of rm: cannot remove `dir/': Is a directoryĪnd rmdir: failed to remove `dir/': Directory not empty Sort -r (glennjackman suggested tac instead of sort -r in the comments to the accepted answer, which also works since tar's output is regular enough) is needed to delete the deepest directories first otherwise a case where dir1 contains a single empty directory dir2 will leave dir1 after the rmdir pass, since it was not empty before dir2 was removed. To first remove all files that were in the archive, and then the directories that are left empty. Tar tf archive.tar | sort -r | xargs -d'\n' rmdir -v You could do tar tf archive.tar | xargs -d'\n' rm -v You don't want to just rm -r everything that tar tf tells you, since it might include directories that were not empty before unpacking! This can be piped to xargs directly, but beware: do the deletion very carefully.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |