1

I'm looking to remove files from a tar archive which match a regex pattern in their filename. My current process is:

tar -tf LC080330242019031901T1-SC20190606111327.tar | grep _b[1-9] | 

Where I query the files in the tar. This gives the output:

LC08_L1TP_033024_20190319_20190325_01_T1_b10.tif
LC08_L1TP_033024_20190319_20190325_01_T1_b11.tif
LC08_L1TP_033024_20190319_20190325_01_T1_b2.tif
LC08_L1TP_033024_20190319_20190325_01_T1_b1.tif
LC08_L1TP_033024_20190319_20190325_01_T1_b6.tif
LC08_L1TP_033024_20190319_20190325_01_T1_b8.tif
LC08_L1TP_033024_20190319_20190325_01_T1_b5.tif
LC08_L1TP_033024_20190319_20190325_01_T1_b3.tif
LC08_L1TP_033024_20190319_20190325_01_T1_b4.tif
LC08_L1TP_033024_20190319_20190325_01_T1_b9.tif
LC08_L1TP_033024_20190319_20190325_01_T1_b7.tif

However I cannot for the life of me find the command to then remove this list of files from the tar file.

Harry Adams
  • 423
  • 5
  • 16

1 Answers1

1

As I mentioned in the comments, using tar -tf test.tar | grep <regex> | xargs --replace=FILES tar -f test.tar --delete FILES worked for me as I was checking if I was able to do it in my local environment.

I wasn't sure why exactly xargs was necessary for the command to work, as I had originally also though calling tar with ouput from grep would work, but consulting the man pages, it turns out that's exactly what xargs is for: building and executing command line commands from standard input.

Most of my use had previously come from when I needed to process arguments one by one using xargs -n 1, but this was pretty interesting to learn.