I have extracted a tar file in the wrong directory, and this tar file extracted 200 files. I cannot remove everything from the directory because there are 10 files that are sitting in the directory, and are needed.
1 Answer
Personally, I would extract the tar file into an empty directory, diff --report-identical-files --recursive --brief the new directory to the erroneous directory, use some sed scripts to extract the wrong names and turn it into a series of rm commands, and run that.
My sed script:
#!/bin/sh
stdbuf -oL sed -n \
-e 's/^Files ..*\(\/..*\) and \(..*\1\) are identical$/\2/p' \
-e 's/^File ..*\(\/..*\) is a socket while file \(..*\1\) is a socket$/\2/p' \
-e 's/^File ..*\(\/..*\) is a fifo while file \(..*\1\) is a fifo$/\2/p' \
| stdbuf -oL sed \
-e s/"'"/"'"'"'"'"'"'"'"/g \
-e 's/.*/rm '"'"'&'"'"'/' \
;
An easier solution might be to use tar -t to generate the list of files from the .tar file, and remove those.
Perhaps:
tar tf tarfilename.tar | xargs rm -i
-
Risk factor: if the tar contains a file of the same name but not the same actual file. For 10 files, I would tar those 10 for security (into the directory above), and also mv them elsewhere, before clearing the corrupted directory. I'm not very brave once I make a mistake -- too easy to compound it.Paul_Pedant– Paul_Pedant2020-09-10 17:20:18 +00:00Commented Sep 10, 2020 at 17:20
-
1@Paul_Pedant That is only a risk for the "easier solution". Personally, for something like this, I always use the diff technique. But, yes, backing up the 10 files is also a good idea.David G.– David G.2020-09-10 22:40:26 +00:00Commented Sep 10, 2020 at 22:40
-
Be careful, as
tar -tmay output extra text. The only clean way to get pure file names is to callstar -t -tpath < file.tar.schily– schily2020-09-11 10:48:21 +00:00Commented Sep 11, 2020 at 10:48 -
@schily This is one reason I listed
rm -iAnother is that it will go significantly wrong if whitespace or any special characters xargs interprets is in the filenames. I do think one is better off with the clean extraction and diff the directories method. Finally, you really should use the sametarthat was originally used, as a different tar might extract things differently.David G.– David G.2020-09-11 12:03:50 +00:00Commented Sep 11, 2020 at 12:03 -
I'm pretty sure only a broken tar would extract things differently - well as long as the archive was not created with a broken tar implementation. BTW: since
starcomes with a built-infindsince it useslibfind, you may usestar ... -find ... -exec ...to remove even files with spaces or newlines in the filename.schily– schily2020-09-11 12:57:42 +00:00Commented Sep 11, 2020 at 12:57
mcwill be faster than automating it –Insmarks files,F8deletes them. You can even navigate into the tar file in one panel so you can compare what came from there.