Jimmy

💕 Jimmy 💗

Delete and Move Files Safely from CLI, and Remedial Actions

technical
exprience
Fairly Confident - 4/5
edited Sep 22, 2023 (created Jan 13, 2023)

I think myself circumspect enough to avoid deleting files by mistake. However, I am wrong. Neither did I synchronize my codes to remote, nor did I safely manipulate them on my laptop. The story is that there are two identical directories, so I want to delete one of them using rm -rf, but the other vanishes simultaneously. Why? Ah, the other directory is a soft link to the one I deleted. Boom…
One never knows what serious fault he can make. Just imagine this command in a script rm -rf ${SOME_DIR}/${SOME_FILE}, it may collapse to rm -rf / if both variables are empty (but may not actually execute because option --preserve-root option is added by default to prevent removing root recursively); but it may also collapse to sort of things like rm -rf /usr, which is indeed a nightmare.

Remediation: Data Recovery

My solution is only examined on a Windows-Linux dual-boot laptop, and the affected partition is of the ext4 file system.
Good news: files deleted will not be erased immediately from the disk.
Bad news: various processes are writing data to the disk, which may overwrite my deleted files.
If possible, unmount the partition immediately, or switch off your laptop.
However, I did not do either, because the data I deleted was located on /usr which is on the root partition where my OS lies upon. I tried to use testdisk but fails. It seems no easy solution to unmount it. A potential solution is to plug it into another PC or OS and use disk recovery software.
Luckily, my laptop is dual boot (i.e. I have both Windows and Linux on it), so I neither have to plug the disk into another PC, nor use more advanced solutions.
After trials, R-Linux is the solution:
  • Power off immediately; Power on, and use Windows
  • Install R-Linux (for Windows version)
  • Select the affected partition, and scan it
  • Search keywords to find deleted files, and recover them
It may or may not work with solely Linux without dual boot, which I do not examine.

Optimize Workflow: Delete Files Safely

The usual way is to move deleted files to a Trash folder instead of deleting them permanently, which is also used by Windows, GUI actions of both macOS and Linux.
trash is such a command tailored to my need.
  • Simply use trash, do not use any command containing rm, such as trash-rm will delete files in the trash permanently, which is also dangerous.
  • Deleted files are moved to ~/.local/share/Trash, so locations may vary for different users.
For macOS, one can try the command above or rmtrash or another trash-cli implementation (as I haven’t tried them so far).

Optimize Workflow: Avoid an Accidental Overwrite with mv command

A simple method is to use -i option when using mv to work in interactive mode. Option -v to trigger verbose mode is also helpful. Option -b to create backup for any overwritten files.
Therefore, we can also create an alias for mv. In particular add alias mv="mv -ibv" to /etc/bash.bashrc, /etc/zsh/zprofile and /etc/zsh/zshrc (as mentioned above).
You are encouraged to add -ibv options manually when using mv, because you may forget to configure this command in a new environment, which may lead to catastrophes. Then, you can choose to remove the backup using trash mentioned above.
Disclaimer: even though I have tested these methods and use them as routine, my solutions may not be elegant and 100% safe to prevent from accidents, please always pay attention to removing or moving files, and try to create remote backup as possible.
☝ Top