Question

I started using "sudo rm -r" to delete files/directories. I even put it as an alias of rm.

I normally know what I am doing and I am quite experience linux user.

However, I would like that when I press the "ENTER", before the execution of rm, a list of files will show up on the screen and a prompt at the end to OK the deletion of files.

Options -i -I -v does not do what I want. I want only one prompt for all the printed files on screen.

Thank you.

Was it helpful?

Solution

##
# Double-check files to delete.
delcheck() {
  printf 'Here are the %d files you said you wanted to delete:\n' "$#"
  printf '"%s"\n' "$@"
  read -p 'Do you want to delete them? [y/N] ' doit
  case "$doit" in
    [yY]) rm "$@";;
    *) printf 'No files deleted\n';;
  esac
}

This is a shell function that (when used properly) will do what you want. However, if you load the function in your current shell then try to use it with sudo, it won't do what you expect because sudo creates a separate shell. So you'd need to make this a shell script…

#!/bin/bash

… same code as above …

# All this script does is create the function and then execute it.
# It's lazy, but functions are nice.
delcheck "$@"

…then make sure sudo can access it. Put it in some place that is in the sudo execution PATH (Depending on sudo configuration.) Then if you really want to execute it precisely as sudo rm -r * you will still need to name the script rm, (which in my opinion is dangerous) and make sure its PATH is before /bin in your PATH. (Also dangerous). But there you go.

OTHER TIPS

Here's a nice option

Alias rm to echo | xargs -p rm

The -p option means "interactive" - it will display the entire command (including any expanded file lists) and ask you to confirm

It will NOT ask about the recursively removed files. But it will expand rm * .o to:

rm -rf * .o
rm -rf program.cc program.cc~ program program.o backup?... # NO NO NO NO NO!

Which is much nicer than receiving the error

rm: .o file not found

Edit: corrected the solution based on chepner comment. My previous solutions had a bug :(

This simple script prompts for a y response before deleting the files specified.

rmc script file:

read -p "ok to delete? " ans
case $ans in
    [yY]*) sudo rm "$@" ;;
    *) echo "Nothing deleted";;
esac

Invoke thus

./rmc *.tmp

I created a script to do this. The solution is similar to @kojiro's.

Save the script with the filename del. Run the command sudo chmod a=r+w+x del to make the script an executable. In the directory in which you want to save the script, export the path by entering export PATH=$PATH:/path/to/the/del/executable in your '~/.bashrc' file and run source ~/.bashrc.

Here, the syntax of rm is preserved, except instead of typing rm ..., type del ... where del is the name of the bash script below.

#! /bin/bash
# Safely delete files

args=("$@") # store all arguments passed to shell
N=$#        # number of arguments passed to shell

#echo $@ 
#echo $#
#echo ${args[@]:0}

echo "Files to delete:"
echo

n=`expr $N - 1`
for i in `seq 0 $n`
do
    str=${args[i]}
    if [ ${str:0:1} != "-" ]; then
        echo $str
    fi
done
echo 

read -r -p "Delete these files? [y/n] " response
case $response in
    [yY][eE][sS]|[yY])

    rm ${args[@]:0}
esac
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top