List Files being deleted in a shell script

Good evening, i need your help please

I've got a file script thet deletes about half millions of files, and i want those files to be printed in a log file as an evidence those files were removed.

#/bin/sh
rm "/home/e-smith/files/users/bill/Maildir/cur/1392373930.28512.comp01:2,S"
rm "/home/e-smith/files/users/ted/Maildir/cur/1420726198.17690.comp01:2,S"
....

and execute this way:

./removefiles_2015.sh >> removefiles_2015.log 2>&1

I'm struggling with 2 problems:

  1. Log file removefiles_2015.log output is 0, so files being delete are not logged

  2. when deleting files takes about 20 minutes which is very slow

Is there any way to approach those things:

I appreciate your help in advanced

Invoking the rm utility once for each of half a million times is going to be a LOT slower than invoking the rm utility fifty thousand times with each invocation removing 100 files.

Although not in the standards, many implementations of the rm utility have a "verbose" option such that adding a -v option to each invocation of rm will print the name of each operand removed after it is removed. If you would tell us what operating system and shell you're using, we might be able to make an easy suggestion that would work in your environment.

How did you get the list of 500,000 files to be removed? If you create the list using find, you could have find remove the files instead of just list files to be removed. If the names of the files are in another file, xargs could be used to create groups of files to be processed by rm .

You have given us very little information about your environment. The less information we have, the fewer options we can suggest to solve your problem. Please help us help you.

Thank you very much for your support

First the enviroment is:

[pmba1@PRODPMBA3 shell]$ uname -s
Linux
[pmba1@PRODPMBA3 shell]$ uname -a
Linux PRODPMBA3 2.6.18-238.el5 #1 SMP Sun Dec 19 14:22:44 EST 2010 x86_64 x86_64 x86_64 GNU/Linux

2nd How did i get the list of 500,000 files to be removed?

grepping into a backup file (bplist)

grep  AB_restat_2015  home_Informe_Bplist-PRODPMBA3_15012017_Q626996.txt | grep log | grep 
'/home/pmba1/shell'

--Ouput

rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-22-01.log.gz
-rw-r--r-- pmba1     pmba1             136 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-23-01.log.gz
-rw-r--r-- pmba1     pmba1             138 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-24-02.log.gz
-rw-r--r-- pmba1     pmba1             139 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-25-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-26-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-27-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-28-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-29-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-30-01.log.gz
-rw-r--r-- root      root              138 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-30-02.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-31-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-32-01.log.gz
-rw-r--r-- pmba1     pmba1             136 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-33-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-34-01.log.gz
-rw-r--r-- pmba1     pmba1             140 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-35-02.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-36-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-37-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-38-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-39-01.log.gz
-rw-r--r-- pmba1     pmba1             140 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-40-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-41-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-42-01.log.gz
-rw-r--r-- pmba1     pmba1             136 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-43-01.log.gz
-rw-r--r-- pmba1     pmba1             137 Jan 15 00:25 /home/pmba1/shell/AB_restat_2015-01-03-01-44-01.log.gz

then construct the script extracting the names of files found in bplist, using vi editor and adding the rm command for each file name

Thanks for your help in advanced

As already suggested by Don Cragun, you might consider piping you list of files into xargs which will group them into fewer actual rm commands. Using the -t flag writes the command being executed to standard error before executing it so you can capture that to a file for review later. It might give you lots of output on few lines, but you can probably make that more readable later on.

You could limit the number of files it processes at once with the -n flag, but that might increase the number of rm command calls and therefore slow your execution. The extreme would be something like:-

list files here | xargs -tn 1 rm

Does that help?

Robin

Are there other gzipped log files in that quite specific directory that you want to retain? If not,

rm -v /home/pmba1/shell/AB_restat_2015-01-03-01-*.log.gz

could do the job. The file selection granularity might be adjusted by adaption of the wild card match.

Thanks once again for your help, Ive got a question: The number 1 as an argument in xargs command what is used for ?

list files here | xargs -tn 1 rm

One might expect that your xargs man page might say something like:

n number    Invoke utility using as many standard input arguments as possible, up to number (a
	     positive decimal integer) arguments maximum.  Fewer arguments shall be used if:
	     �	The command line length accumulated exceeds the size specified by the s
		option (or {LINE_MAX} if there is no s option).
	     �	The last iteration has fewer than number, but not zero, operands remaining.

Does that help?