DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Living in the Shell #12; mv (Move/Rename Files/Directories)
Babak K. Shandiz
Babak K. Shandiz

Posted on • Originally published at babakks.github.io on

Living in the Shell #12; mv (Move/Rename Files/Directories)

mv 🧳

Moves/renames files or directories.

⚠️ Default behavior is to overwrite destination files.

Rename a single file

cd ~ && mv .bashrc .bashrc-renamed
Enter fullscreen mode Exit fullscreen mode

Move a single file into a directory

cd ~ && mv .bashrc Documents
Enter fullscreen mode Exit fullscreen mode

Note that the Documents directory should exist.

Move a single file onto a specific new path (filename)

cd ~ && mv .bashrc Documents/.bashrc-moved
Enter fullscreen mode Exit fullscreen mode

Move a whole directory

mv my-src-dir ~/Documents/my-dest-dir
Enter fullscreen mode Exit fullscreen mode

⚠️ Note that if my-dest-dir already exists, my-new-src will be moved under it; i.e., you would have your files under Documents/my-dest-dir/my-src-dir

Create backup for existing destination files -b

cd ~ && mv -b .bashrc Documents/.bashrc-moved
Enter fullscreen mode Exit fullscreen mode

Move by wildcard selection -t

cd ~ && mv -t target-dir *.zip *.txt
Enter fullscreen mode Exit fullscreen mode

Moves all .zip and .txt files to target-dir directory.

Set to ask for overwriting -i

cd ~ && mv -i .bashrc Documents
Enter fullscreen mode Exit fullscreen mode

Set to keep existing files (no overwrite) -n

cd ~ && mv -n .bashrc Documents
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: