Skip to content

About

A smart wrapper for `mv` that automatically creates numbered backups when overwriting files inside your home directory.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MIT License Bash Version Stars


📦 Bash MV Auto Backup

A smart wrapper for mv that creates timestamped backups whenever you overwrite existing files or directories.
Explore the docs »

Report Bug · Request Feature


📑 Table of Contents


✨ Features

  • Timestamped Backups
    Existing files or directories are renamed to name.ext.YYYY-MM-DD--HH-MM-SS.bak before being overwritten.
    Example: report.docx.2026-04-27--15-30-22.bak

  • Handles Both Files and Directories
    Unlike many backup tools, this wrapper also protects entire directories from accidental overwrites.

  • Safe by Default
    Only activates inside your home directory ($HOME). Outside of it, mv behaves normally – no unwanted side effects.

  • Python Virtual Environment Friendly
    Automatically disables backup when the destination resides inside an active virtual environment ($VIRTUAL_ENV).

  • Preserves All Standard Options
    -i (interactive), -f (force), -n (no-clobber), -v (verbose), --, etc. – all work as expected.

  • Collision‑Proof Naming
    If two moves happen within the same second, a counter (_1, _2, …) guarantees unique backup names.

  • No Octal Interpretation Bug
    Because timestamps are pure strings, you’ll never see “value too great for base” errors.


🚀 Installation

Prerequisites

  • Bash 4.0 or later (standard on modern Linux, macOS, and WSL)
  • coreutils (usually pre-installed)

Option 1: Clone the Repository

git clone https://github.com/setuju/bash-mv-auto-backup.git
cd bash-mv-auto-backup

Option 2: Direct Download

curl -o mv-auto-backup.sh https://raw.githubusercontent.com/setuju/bash-mv-auto-backup/main/mv-auto-backup.sh

Activate the Wrapper

Add the following line to your ~/.bashrc, ~/.bash_profile, or ~/.zshrc:

source /path/to/mv-auto-backup.sh

Replace /path/to/ with the actual location of the script. Then reload your shell:

source ~/.bashrc

…or simply open a new terminal window.


🛠 Usage

Once activated, the mv command works exactly as before, but with extra safety.

Basic Example – File

$ mkdir ~/archive
$ echo "First version" > ~/archive/doc.txt
$ echo "Second version" > doc.txt
$ mv doc.txt ~/archive/
$ ls ~/archive/
doc.txt
doc.txt.2026-04-27--09-15-30.bak

The old doc.txt was renamed to a timestamped backup before the new file was moved in.

Directory Example

$ mv my_project ~/archive/

If ~/archive/my_project already exists, it becomes my_project.2026-04-27--23-59-59.bak, and the new my_project is placed in ~/archive/.

Multiple Backups

Every collision creates a unique backup, so your history is always preserved:

$ echo "Third version" > doc.txt
$ mv doc.txt ~/archive/
# → doc.txt.2026-04-27--09-15-45.bak

Using Standard mv Options

$ mv -v file.txt ~/destination/       # verbose
$ mv -i important.txt ~/backup/       # prompt before overwrite
$ mv -f data.log ~/logs/              # force (backup still happens)

📍 Where the Backup Does NOT Activate

  • Outside $HOME – e.g., sudo mv /etc/config /etc/config.bak uses the original mv behavior.
  • Inside a Python virtual environment (when $VIRTUAL_ENV is set) – moves inside myenv/ are not backed up.
  • When the destination is a regular file (not a directory) – the original mv logic applies.

⚙️ Customization

You can easily modify or add exclusion rules by editing the _is_in_venv function or the allow_backup block. For example, to also exclude a specific folder:

if [[ "$target_dir_abs" ## "/path/to/exclude"* ]]; then
    allow_backup#false
fi

❓ FAQ

Why not just use mv -b?

The standard mv -b creates a backup with a tilde suffix (e.g., file~), but it does not support timestamped backups for multiple collisions. This wrapper gives you clean, chronologically sortable backups and integrates seamlessly with your daily workflow.

What happens if I move a file/folder outside of $HOME?

Nothing special. Outside $HOME the original mv command is called directly, so your system behaves exactly as it did before.

Can I disable the backup temporarily?

Yes. Just call the original mv via command mv ... or \mv ....


📄 License

This project is licensed under the MIT License – see the LICENSE file for details.


👤 Author

Jack – MainHack BrotherHood


🤝 Contributing

Contributions, issues, and feature requests are welcome!
Feel free to check the issues page.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

⚠️ Disclaimer

This script overrides the default mv command. While it is designed to be safe, always keep backups of critical files. The author is not responsible for any data loss. Use at your own risk.

About

A smart wrapper for `mv` that automatically creates numbered backups when overwriting files inside your home directory.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages