Exercise 0.4: Moving and Renaming

Organizing Your Files

Author

Chuck Nelson

Published

October 28, 2025

Purpose

As you work on projects, you will constantly need to reorganize your work. Files need to be renamed for clarity, and moved into different directories to keep your project tidy. This exercise covers the commands used for moving and renaming files and directories.

What You’ll Accomplish

By the end of this exercise, you will have successfully:

  • Renamed a file using the command line.
  • Moved a file into a different directory.
  • Understood how a single command can often perform both actions.

The Dual-Purpose Command

On macOS and Linux, a single command, mv (move), is used for both renaming and moving. The command’s behavior depends on its last argument:

  • If the last argument is an existing directory, mv moves the source file/folder into it.
  • If the last argument is a new name, mv renames the source file/folder.

Windows uses separate commands for these actions, which can be clearer for beginners.

Your Task 1: Rename a File

First, let’s create a file and then change its name.

  1. Navigate to your cli-practice directory.
  2. Create a new file. Let’s call it draft.txt.
  3. Now, use the appropriate command for your OS to rename draft.txt to final.txt.

Use the mv command. The syntax is mv <old_name> <new_name>.

touch draft.txt
mv draft.txt final.txt
ls

Use the Rename-Item cmdlet. The syntax is Rename-Item -Path <old_name> -NewName <new_name>.

New-Item draft.txt
Rename-Item -Path draft.txt -NewName final.txt
ls

Use the ren (or rename) command. The syntax is ren <old_name> <new_name>.

echo. > draft.txt
ren draft.txt final.txt
dir

After running the commands, you should see final.txt in your directory, and draft.txt will be gone.

Your Task 2: Move a File

Now let’s move a file into a directory.

  1. While still in your cli-practice directory, create a new subdirectory called archive.
  2. Create a new file to be moved, called report.docx.
  3. Use the appropriate command to move report.docx into the archive directory.

Use the mv command again. The syntax is mv <file_to_move> <destination_directory>.

mkdir archive
touch report.docx
mv report.docx archive/
ls
ls archive

Use the Move-Item cmdlet. The syntax is Move-Item -Path <file_to_move> -Destination <destination_directory>.

mkdir archive
New-Item report.docx
Move-Item -Path report.docx -Destination archive/
ls
ls archive

Use the move command. The syntax is move <file_to_move> <destination_directory>.

mkdir archive
echo. > report.docx
move report.docx archive\
dir
dir archive

After running the commands, you will see that report.docx is no longer in cli-practice, but is now inside archive.

Reflect and Review

ImportantReflection

In your Microsoft Teams Student Notebook, answer the following:

  • On macOS/Linux, what is the single command used for both renaming and moving?
  • On Windows, what are the two separate commands for renaming and moving?
  • If you wanted to rename a directory from stuff to important-stuff, what command would you use on your OS?
Back to top