Move a File into a Directory
Move a File into a Directory
The report is ready, and the teacher wants it out of the working folder and inside an archive folder. A move changes the report's location. Unlike a copy, it does not leave a second report at the old path just to preserve the source.
We will use mv with an existing directory as its destination. That gives you a useful way to read the command: take this source entry and place it inside that directory. The full path you use afterward must include the new location.
What you’ll learn
- Move a report into an existing archive directory.
- Distinguish moving the source from copying it.
A report changes location
Before
The report is outside the existing archive directory.
report.txtMove
mv places the report in the selected directory.
mv report.txt archive/After
The usable path is now inside archive; the old path no longer names it.
archive/report.txt
Move a report into the archive folder
Prepare the example
Start in a fresh folder. The example creates the archive directory and a small report before moving anything. Because the destination directory exists, archive/ clearly means a folder to move into rather than a proposed new filename.
Bash · Linux terminal
mkdir archive
printf 'Ready\n' > report.txt
mv report.txt archive/
cat archive/report.txtExpected result
Readymkdir archive prepares the destination. The printf line creates the source report, then mv report.txt archive/ relocates it. Afterward, the old relative path report.txt is no longer the way to reach this report.
We check archive/report.txt instead and see the text Ready. If the destination already contained a file with that name, an overwrite decision would be involved, so an interactive option can help. Also note that a move across filesystems may involve copying data and removing the source rather than the simple directory-entry change used within one filesystem.
Moving, renaming and choosing the destination path
The mv command changes where a file lives. Unlike cp, it does not leave the original file behind after a successful move.
The mv Command
The mv command stands for "move". It’s used to move files from one place to another—or even rename them!
mv syntax for moving a file
mv source_file target_directory
Example 1: Moving a File to Another Folder
Let’s say you have a file called notes.txt in your home folder, and you want to move it into a folder called Documents.
mv notes.txt Documents/
If the move is successful, there will be no message. That’s normal!
notes.txt is now inside the Documents folder.
Example 2: Moving and Renaming a File
You can also move a file and rename it at the same time.
mv notes.txt Documents/my_notes.txt
File has been moved and renamed to Documents/my_notes.txt
Example 3: Using Absolute Paths
If you’re not in the same folder as the file, use absolute paths:
mv /home/user/notes.txt /home/user/Documents/
If mv cannot find the source path
If you try to move a file that doesn’t exist, Linux will give you an error:
mv random.txt Documents/
mv: cannot stat 'random.txt': No such file or directory
Make sure the file name is correct and that you're in the right directory.
Protect destination files while moving them
- Use
lsto list files before you move them. - Use
mv -ito prompt before overwriting existing files. - Use
mv -vto see what’s happening (verbose mode).
mv -iv notes.txt Documents/
Move files and protect existing destinations
To move a file in Linux, just use mv followed by the file name and the destination folder. That’s it!
Try it, then check the worked answer
Your task: Move report.txt inside an existing archive directory.
Start in a fresh folder. The example creates the archive directory and a small report before moving anything. Because the destination directory exists, archive/ clearly means a folder to move into rather than a proposed new filename.
Bash · complete example for this question
mkdir archive
printf 'Ready\n' > report.txt
mv report.txt archive/
cat archive/report.txtExpected result
ReadyWorked answer
mv report.txt archive/
Put the source first: mv report.txt archive/. Reversing the operands asks for a different operation. Using cp would preserve the source as well, which does not meet the task of relocating the report.
Check your decision
Check the destination and confirm the source moved
After a move, verify the report at its new path and check that the old path is no longer the intended location. Read the two operands as source then destination. Pay particular attention to whether the destination is an existing directory or another file.
Reaching the recap and completing the quizzes are separate milestones.