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! Using tar -zxv -f a.tgz -f b.tgz or tar -zxv -all-args-are-archives *.tar.gz would break no existing syntax, imho.This can be piped to xargs directly, but beware: do the deletion very carefully. Please don’t reply with tar -zxvf *.tar.gz (because that does not work) and only reply with “doesn’t work” if you’re absolutely sure about it (and maybe have a good explanation why, too).Įdit: I was pointed to an answer to this question on Stack Overflow which says in great detail that it’s not possible without breaking current tar syntax, but I don’t think that’s true. we’re all blind and it’s totally easy to do - but I couldn’t find any hint in the web that didn’t utilize for or find or xargs or the like.someone knows how to use the -M parameter that tar suggested to me when I tried tar -zxv -f a.tgz -f b.tgz.there’s a strange fork of tar somewhere that supports this.I’m asking this question rather out of curiosity, maybe (And no, there’s nothing wrong with for, I’m merely asking whether it’s possible to do without.) I’m an experienced Unix user for several years and of course I know that you can use for or find or things like that to call tar once for each archive you want to extract, but I couldn’t come up with a working command line that caused my tar to extract two. I was wondering whether (and, of course, how) it’s possible to tell tar to extract multiple files in a single run.
0 Comments
Leave a Reply. |