Moving Files & Directories
5 min read·Jan 1, 2025
In Unix-like operating systems, moving and renaming are essentially the same operation, as renaming a file or directory simply consists in moving it to a specified location in the filesystem under a different name.
Moving files and directories
To move a single file or directory into another directory, you can use the mv command — which stands for move — as follows:
$ mv source target
Where:
sourceis the path to the file or directory you want to move.targetis the path to the directory you want to move it into.
Note: When moving a directory into another directory, all its content including its subdirectories will be moved with it.
Example
Let's consider this directory:
.
├── index.js
└── src
This command will move the index.js file into the src directory:
$ mv index.js src
Resulting in this directory structure:
.
└── src
└── index.js
Moving multiple files and directories
To move multiple files and directories at once into another directory, you can use the mv command as follows:
$ mv source ... target
Where:
source ...are the paths to the files and/or directories you want to move.targetis the path to the directory you want to move them into.
Example
Let's consider this directory:
.
├── index.js
├── src
└── utils
├── math.js
└── string.js
This command will move the index.js file and the utils directory (including its content) into the src directory:
$ mv index.js utils src
Resulting in this directory structure:
.
└── src
├── index.js
└── utils
├── math.js
└── string.js
Renaming files and directories
To rename a file, you can use the mv command as follows:
$ mv source target
Where:
sourceis the path to the file or directory you want to rename.targetis the path to the directory you want to move it into, followed by its new name.
Notes:
When renaming a file, if a file with the same name already exists in the target directory, it will be overwritten by the source file.
When renaming a directory, if the target directory already exists, the source directory will not be renamed but moved into the target directory.
Example
This command will rename the index.js file into server.js in the current working directory:
$ mv index.js server.js
This command will move the login.js file into the controllers directory and rename it to login.controller.js:
$ mv login.js controllers/login_controller.js
This command will rename the controllers directory to services:
$ mv controllers services
Summary
Here's a summary of what you've learned in this lesson:
- The
mvcommand is used to move or rename files and directories. - The
mv source targetcommand is used to move (and optionally rename) a file or directory into another directory. - The
mv source ... targetcommand is used to move multiple files and directory into another directory.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery