1

I want to delete all the .png files in a directory and save the delete files names in a text file. I managed to do this with the following command:

find . -name "*.png" -delete -print >> log.txt

Everything works fine and in the log I get the entire the path of the file deleted. But if there are multiple files to be deleted, the names placed in the log are all on the same line.

C:/Users/Dragos.Cazangiu/Desktop/TesteCyg/CMDER/New Bitmap Image3.pngC:/Users/Dragos.Cazangiu/Desktop/TesteCyg/CMDER/2.pngC:/Users/Dragos.Cazangiu/Desktop/TesteCyg/CMDER/New Bitmap Image.png

How can I make it put each file on a new line and also how can I add a message before the path, something like "The deleted file is: "

Thank you!

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Dragos Cazangiu
  • 207
  • 1
  • 3
  • 14

4 Answers4

2

Since you are on Windows (for whatever reason... ;) ) you may want to use -printf in order to produce Windows style line endings:

find . -name '*.png*' -delete -printf "%p\r\n" >> log.txt

Alternatively just configure the text editor you are using to view the output file to UNIX line endings.

To add a message:

find . -name '*.png*' -delete -printf "Deleted: %p\r\n" >> log.txt
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
1

find . -name "*.png" -delete -print "\n" >> log.txt

Adaleni
  • 966
  • 7
  • 15
1

You can replace the -print predicate with this predicate:

-exec printf 'The deleted file is: %s\n' {} \;

so that the command looks like:

find . -name "*.png" -delete -exec printf 'The deleted file is: %s\n' {} \; >> log.txt

If you have GNU find, then you can use the -printf predicate as so:

find . -name "*.png" -delete -printf 'The deleted file is: %p\n' >> log.txt
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
  • I don't see the point of explicitly printf `\n`. This is actually the default. Also the `{} \;` is wrong here. It is a syntax specific to the `-exec` option. Wonder why this has been accepted. – hek2mgl Sep 15 '17 at 11:44
  • I admit that I missed that you `-exec printf`.. But that's more or less the same. You are still outputting `\n` which is the default. – hek2mgl Sep 15 '17 at 11:52
  • @hek2mgl: what else would you want instead of `\n`? nothing? or `\r\n` since OP seems to be on a retarded OS? – gniourf_gniourf Sep 15 '17 at 14:14
  • I recommended to use `\r\n` in my answer and this is what makes sense for me. Don't get me wrong, I'm just curious why the OP is fine with your answer. Actually I don't understand the question then. – hek2mgl Sep 15 '17 at 14:15
  • @hek2mgl: I guess OP was more interested in knowing how to print a message around the file name (the `-exec printf`/`-printf` part)... go figure why mine got accepted and not yours. SO is a bit weird sometimes — look at Adelani's answer that got an upvote. – gniourf_gniourf Sep 15 '17 at 14:17
  • Yeah, that for sure but still I'm asking why OP missed the newlines (Look at the sample output in the question). At the end we'll probably never know... :) – hek2mgl Sep 15 '17 at 14:39
1

A much more intuitive (TGIF :) way by using bash, rm, yes and awk. First, test stuff:

$ touch 1.png 2.png 3.png

Then:

for i in *.png ; do yes | rm -i $i 2>&1 >/dev/null | awk '{gsub(/^.|..$/,"",$NF);print $NF}' >> file ; done
$ cat file
1.png
2.png
3.png

or:

for i in *.png                                       # daloop
do                                                   # yes sir
  yes |                                              # nod to pipe
  rm -i $i 2>&1 >/dev/null |                         # rm interactively piping the stderr
  awk '{ gsub(/^.|..$/,"",$NF); print $NF }' >> file # ... to awk
done                                                 # phew

Quote $i if needed.

James Brown
  • 36,089
  • 7
  • 43
  • 59