Jacob Dorman
Jacob Dorman

Reputation: 341

delete files extracted from tar archive

Occasionally I extract a tar archive into entirely the wrong directory. Usually because some archives have all files one directory down and some don't.

tar -xvf archive.tar

What's the best way to undo/reverse this?

List the files contained in archive.tar and pipe them to rm ...?

Extra internet points for handling files that may have been overwritten by the extract.

Upvotes: 1

Views: 2274

Answers (1)

Jacob Dorman
Jacob Dorman

Reputation: 341

My google-fu was weak.

From the comments on this [http://www.commandlinefu.com/commands/view/2573/remove-all-files-previously-extracted-from-a-tar.gz-file.](commandlinefu entry)

tar tf <file.tar.gz> | sort -r | while read file; do if [ -d "$file" ]; then rmdir "$file"; else rm -f "$file"; fi; done

Hopefully this will help someone else.

If someone knows a way to handle overwritten files I will mark that as the correct answer.

Upvotes: 1

Related Questions