I have to delete the files which are zipped and then stored in .tar format. I Have to untar the files then delete the files which name includes XXX, YYY or ZZZ. There are more than thousand of .tar files and each .tar file contains hundred of .zip files.I need a script which can do this.

  • tar has an option to delete files, without you having to untar then tar. – ctrl-alt-delor Feb 28 '17 at 21:51

I think this would best be solved with a recursive program, unfortunately I am bad at creating recursive shell scripts, so I'm going to recommend a C++ program. The formatting is not standard, uses goto statements, and it makes a lot of system calls, but it will get the job done. As the information was not provided this assumes all tarballs are using .tar as their suffix, all tarballs are gzip format (easily correctable if this is untrue), and that the name of the file which needs to be deleted is the same in all of the tarballs.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
system("touch ~/tarlist; list=$(find /path/to/tarballs/ type -f); echo $list > ~/tarlist; touch ~/currentTarline; touch ~/currentTarcolumn; touch currentTar");
system("touch ~/numoflinesTMP1; touch ~/numoflines; num=$(wc -l ~/tarlist); echo $num > ~/numoflinesTMP; num1=$(cut -c 1 ~/numoflinesTMP); echo $num1 > ~/numoflines"); mkdir ~/tmp/
int maxlines;
ifstream MAXLINES("~/numoflines");
int currentline=1;
ofstream CURRENTLINE("~/currentTarline");
system(line=$(cat ~/currentTarline); nameoftar=$(cat ~/tarlist | sed '$line!d'); echo $nameoftar > ~/currentTar; extract=$(cat ~/currentTar); cd ~/tmp/; tar -xzf $extract; rm *nameoffile*; tar czf $extract ./*; newline=$(( $line + 1 )); echo $newline > ~/currentTarline");
ifstream NEWLINE("~/currentTarline");
return 0;
else if(currentline<maxlines)
goto GETTAR;

I apologize for the lack of horizontal formatting, but I couldn't find a way to do that without giving it a list structure. Several variables used in this code are placeholders, and you may need to adjust certain lines to account for the working directory you want the program to use, the name of the file that needs to be deleted from the tarballs, and the actual parent directory of the tarballs. Hopefully this will work as a general guideline. If you truly want to you could also take most of the system calls and drop them into a recursive shell script, but I personally think that taking the time to make a shell script recursive in the first place is more of a hassle than simply making a C++ wrapper for it. Good luck!

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.