08. Moving and Renaming files mv

Moving and renaming are performed with the same command, mv.

Moving

To move a file, use the mv command.

Our last argument should be the target path to that directory.

$ mv file1 file2 file3 dir1/

Creating good habits

A good habit to form is to make sure that your target path ends with a / if you are using a directory. Having the / at the end tells the shell that the destination is a directory. Check out how the shell notifies you that the directory does not exist when we accidently commit a typo.

$ mv file1 /home/JohnDoe/diretcry/
mv: rename file1 to /home/JohnDoe/diretcry/: No such file or directory.

If we were to omit the /, the shell would assume that we are trying to move our file to /home/JohnDoe and rename our file to diretcry.

Renaming

We can use mv command to move a file to your current directory with a new name (in other words, rename it).

$ mv oldFileName newFileName

Extra options

mv shares common options with cp.

-i
Interactive mode. Before overwriting a file, prompt the user.
-u
Update. Copy only files that either don't exist or are newer than the existing corresponding files in the destination directory.
-v
Verbose. Ouput messages as they are being copied.

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

System Admin Handbook

Take your Linux skills to the next level! Try Linux & UNIX

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources

Ad