Wednesday, 15 May 2013

linux - remove files and prompt directories only -



linux - remove files and prompt directories only -

as deleting many obsolete file trees on linux machine wondering if there easy way remove files recursively while prompting on directories.

i utilize rm -ri there much files annoying reply every 1 of them. matter me beingness prompted on folders have more command on happens.

i not bash expert asking if there simple way this.

here effort long bash script:

#!/bin/bash promptremovedir() { filecount=$(ls -1 $1 | wc -l) prompt=1 while [ $prompt == 1 ] read -p "remove directory: $1($filecount files) ? [yl]: " reply case $answer in [yy]) rm -r $1 prompt=0 ;; l) echo $(ls -a $1) ;; *) prompt=0 ;; esac done } removedir() { if [ "$(ls -a $1)" ] dirs=$(find $1/* -maxdepth 0 -type d) fi if [[ -z $dirs ]] promptremovedir $1 else dir in $dirs removedir $dir done promptremovedir $1 fi } in $* if [ -d $i ] removedir $i else rm $i fi done

if understand question should work

dirs=$(find . -type d)

removes files in directories specified

for in "$dirs"; read -p "delete files in "$i": ";if [[ $reply == [yy] ]]; find $i -maxdepth 1 -type f | xargs -0 rm ; fi ;done

if want delete folders well, read lowest directory(none below it) upwards.

for in $(echo "$dirs" | sed '1!g;h;$!d' ); read -p "delete files in $i: ";if [[ $reply == [yy] ]]; rm -r "$i"; fi ;done

linux bash shell rm

No comments:

Post a Comment