2

I want to count number of interactively removed files and directories:

for f in /tmp/mydir/* ; do
  rm -ir "$f"
done

How to do it in most concise/elegant way?


Example:

abc@def:/tmp/mydir$ tree
.
├── 1
├── 2
├── 3
├── 4
├── A
│   ├── 1
│   ├── 2
│   └── 3
├── B
│   ├── 1
│   └── 2
└── C

3 directories, 9 files

If all answers are y (yes), then I expect answer:

  • 7 (count: 1, 2, 3, 4, A, B, C) or:
  • 10 (count: 1, 2, 3, 4, A/1, A/2, A/3, B/1, B/2, C).

Both versions are welcome.

I know that I can count files/directories before and after running interactive rm, but this is not the case because in fact I iterate through files stored in text file.

5

simply use:

rm -vri files | wc -l

will include dirs, too (i.e. the removal of A). This will work as -v will only send successful removed ’file’ (or dir) output to stdout, while all others go to stderr.

In your example the output will be 12, as there are 3 dirs and 9 files.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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