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

babakks

Babak K. Shandiz

Posted on December 11, 2021

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
šŸ’– šŸ’Ŗ šŸ™… šŸš©
babakks
Babak K. Shandiz

Posted on December 11, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related