0

Finding all directories with Gigabyte space usage. Extracting the directory name from xargs and then again xargs to remove them.

du -d2 -h ~/Downloads/ | grep -E '[0-9]G' | xargs -I{} sh -c "echo {} | cut -d' ' -f2"  | xargs rm -ri

I would like to interactively remove the directory, but I dont get a chance from the bash to enter my option.

2 Answers 2

0

You cannot answer interactively to rm -ri questions with the last piped xargs command: explanation here.

Instead, you can put the list of big directories as arguments to rm -ri:

rm -ri $(du -d2 -h ~/Downloads/* | grep -E '[0-9]G' | xargs -I{} sh -c "echo {} | cut -d' ' -f2")

Note that you can replace the xargs cut with awk as such:

rm -ri $(du -d2 -h ~/Downloads/* | grep -E '[0-9]G' | awk '{print $2}')

Or even shorter:

rm -ri $(du -d2 -h ~/Downloads/* | awk '$1 ~ /G$/ {print $2}')

Also note that the above commands won't work well if there are spaces in the filenames.

3
  • The only issue with this is that { print $2 } will print a partial path if the pathname contain spaces.
    – Kusalananda
    Jun 21, 2017 at 15:13
  • You are right, thx. In fact, the whole rm -ri $() won't work if the pathnames have spaces. And in fact, the original command in the question wouldn't work either due to the cut command. Updating answer.
    – Gohu
    Jun 21, 2017 at 15:19
  • @Gohu thanks for the alternative solution. I have found the right one and posted as an answer. Jun 22, 2017 at 17:18
0

It looks like there is indeed a method to delete directories interactively using xargs. After reading the man pages, I myself came out with a solution.

The -p option along with -n 1 option solves the problem.

du -d2 -h ~/Downloads/* | grep -E '[0-9]G' | xargs -I{} sh -c "echo {} | cut -d' ' -f2"  | xargs -p -n1 rm -r

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .