2

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.

6
  • Welcome! You can't identify those ten files? Commented Sep 10, 2020 at 15:11
  • Yes, I can identify those 10 files. Commented Sep 10, 2020 at 15:18
  • So just move the 10 files to a temp directory, delete everything else and move them back. Isn't that a decent solution for you? You can also read the tar file, collect the names of extracted files and delete those, but simply moving the files you want to keep will be easier. Commented Sep 10, 2020 at 15:21
  • Sometimes, doing it by hand with mc will be faster than automating it – Ins marks files, F8 deletes them. You can even navigate into the tar file in one panel so you can compare what came from there. Commented Sep 10, 2020 at 15:32
  • Are you certain that none of the10 files were overwritten by any files in the archive when you unpacked the archive? Commented Sep 10, 2020 at 16:54

1 Answer 1

2

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
7
  • 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. Commented 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. Commented Sep 10, 2020 at 22:40
  • Be careful, as tar -t may output extra text. The only clean way to get pure file names is to call star -t -tpath < file.tar. Commented Sep 11, 2020 at 10:48
  • @schily This is one reason I listed rm -i Another 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 same tar that was originally used, as a different tar might extract things differently. Commented 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 star comes with a built-in find since it uses libfind, you may use star ... -find ... -exec ... to remove even files with spaces or newlines in the filename. Commented Sep 11, 2020 at 12:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.