Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pipes with find command in linux

I would like to find files in my home directory that start with '~', sort them numerically, print the first five and delete them using find command and pipes in Linux. I have a bash script:

#!/bin/bash
find ~/ -name "~*" | sort -n | head -5 | tee | xargs rm

This works fine for deleting files, but I was expecting tee command to print deleted files to standard output. All this command does is delete files, but there in so output in terminal. What should I add/ change? Thank you.

like image 741
Luki Avatar asked Jun 24 '26 08:06

Luki


2 Answers

You could just use the verbose flag on rm and it will tell you what it's deleting

find ~/ -name "~*" | sort -n | head -5 |  xargs rm -v

Use man rm to see the docs

-v, --verbose

explain what is being done

like image 57
Danny Avatar answered Jun 25 '26 21:06

Danny


You can use rm -v to print each deleting filename:

find ~ -name '~*' -print0 | sort -zn | head -z -n 5 | xargs -0 rm -v

Also note use -print0 and all corresponding options in sort. head, xargs to address filenames with whitespace and glob characters.

like image 41
anubhava Avatar answered Jun 25 '26 22:06

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!