Avatar Hund

Linux, plaintext & minimalism

February 9, 2019

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

There’s a saying that it’s sometimes hard to see the forest for the trees. The flag -i, --interactive for the commands cp and mv and -I, --interactive=<never, once or always> for rm is one of those occurrences.

It has been lurking in front of my face for all these years and yet I took me forever before noticing it. The flag simply makes cp and mv ask for confirmation before they overwrite any files or folders. Like this:

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 have 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'

You might have notice that the flag for rm have a capital “i”. This means that it will prompt once before removing more than three files or when removing recursively. If you want to prompt for every single file you just replace it with the flag -i instead.

Tags: Shell | Safety

Feedback

I don't have a commenting system on my blog because I haven't found one that I like. All feedback and hellos can be done via either e-mail, XMPP or Mastodon.