How to copy, move and delete files in your shell the safe way

by Hund | February 9, 2019

If you copy, move or remove files in the shell using cp, mv and rm you don’t get a confirmation for your action. This can be a awfully bad thing if you happen to make a mistake like slipping on a key and accidentally deleting the wrong file. No ones want that.

There’s a pretty simple way to prevent an accident like that, and that’s using the flag -i, --interactive with those commands. It will then ask for your confirmation every time you’re about to overwrite or delete files:

Move:

$ mv -i dog.txt cat.txt
mv: overwrite 'cat.txt'?

Copy:

$ cp -i cat.txt dog.txt
cp: overwrite 'dog.txt'?

Delete:

$ rm -i *
zsh: sure you want to delete all 2 files in /home/johan/test [yn]?

This flag has actually saved me a few times. I highly recommend adding aliases for them so you never forget to use the flags:

alias cp='cp -i'
alias mv='mv -i'
alias rm='mv -i'

By default, rm will ask for confirmation for every single file, if you batch delete files and only want to confirm once, you can change the flag to a capital “i” like this: -I. It will then only ask you when you remove three files or more and when you’re removing files and folders recursively.

Meta

Comments

There's no comments for this post. Use this e-mail form if you would like to leave a /public/ comment on this post. Or simply send me a private e-mail message if you have any feedback, or just want to say hello.