Introduction
This tutorial provides an overview of the mv command in Linux. The mv command is a versatile tool used for moving or renaming files and directories within a Linux environment. Understanding its basic usage and various options is essential for efficient file management.
In this lab, you will take on the role of a junior system administrator tasked with organizing files for a small web development company. You'll use the mv command to manage project files, update content, and maintain an organized file structure.
Exploring the Project Directory
Let's start by examining the contents of our project directory.
Open your terminal. You should be in the
/home/labex/projectdirectory by default. To confirm this, you can use thepwdcommand, which you've learned previously. It will display your current working directory.Use the
lscommand to list the contents of the current directory:
ls
You should see several files and directories related to web development projects. The output might look something like this:
index.html styles.css script.js utils.js images
Don't worry if you see additional files or if some of these are missing. The important thing is to understand what files and directories are present in your working environment.
Moving a File
Now, let's move a file to organize our project structure better. We'll move the styles.css file into a new directory called css.
- First, we need to create the
cssdirectory. Use themkdircommand, which you've learned previously:
mkdir css
This command creates a new directory named css in your current location.
- Now, let's use the
mvcommand to movestyles.cssinto thecssdirectory:
mv styles.css css/
Let's break down this command:
mvis the command we're using to move filesstyles.cssis the source file we want to movecss/is the destination directory where we want to move the file
The forward slash after css indicates that it's a directory. Including the slash is optional but can help clarify that we're moving the file into a directory.
- To verify that the file has been moved, we can use the
lscommand again, this time to look inside thecssdirectory:
ls css
You should see styles.css listed in the output. If you don't see it, don't worry - we'll check this in our verification step.
Renaming a File
Sometimes, we need to rename files to follow naming conventions or update versions. The mv command can also be used to rename files.
- Let's rename
index.htmltohome.html. We'll use themvcommand again, but this time both the source and destination will be in the same directory:
mv index.html home.html
In this command:
index.htmlis the current name of the file (the source)home.htmlis the new name we want to give the file (the destination)
When the source and destination are in the same directory, mv understands that we want to rename the file rather than move it.
- To verify the change, use the
lscommand:
ls
You should see home.html in the list, but index.html should no longer be present. If both files are present, or if you only see index.html, don't worry - our verification step will help us check this.
Moving Multiple Files
Often, you'll need to move multiple files at once. Let's organize our JavaScript files by moving them into a scripts directory.
- First, create a new directory for scripts using the
mkdircommand:
mkdir scripts
- Now, we'll use the
mvcommand to move all.jsfiles into thescriptsdirectory:
mv *.js scripts/
Let's break down this command:
mvis our command to move files*.jsis a pattern that matches all files ending with.js. The*is a wildcard that means "match any characters"scripts/is our destination directory
This command will move all JavaScript files (files ending with .js) into the scripts directory.
- To verify the move, use the
lscommand to check the contents of thescriptsdirectory:
ls scripts
You should see all the JavaScript files listed. If you don't see any files, or if you see some .js files still in the main project directory, don't worry - our verification steps will help us check this.
Using the -i Option for Safe Moves
When moving files, it's good practice to use the -i option to prevent accidental overwrites. The -i option stands for "interactive" and will prompt you before overwriting an existing file.
Let's create a test file to experiment with. We'll use a new command called
echoto do this. Don't worry about understandingechofully - we're just using it to create a file with some content:echo "Test content" > test.txtThis command creates a new file named
test.txtwith the content "Test content".Make sure
home.htmlexists before running the next command. You can check with:ls home.htmlIf it doesn't, create it with the following command:
echo "Home page" > home.htmlNow, let's try to move this file to overwrite an existing file, using the
-ioption:mv -i test.txt home.htmlYou will be prompted with a message similar to:
mv: overwrite 'home.html'?
This is asking if you want to overwrite the existing home.html file with test.txt. Type n and press Enter to cancel the operation.
The -i option is very useful when you're not sure if you might be overwriting important files. It gives you a chance to reconsider before making changes.
Summary
In this lab, you've learned how to use the mv command to organize files in a web development project. You've practiced moving files, renaming them, working with multiple files, and using the -i options for safer file operations.
Here are some additional mv command options not covered in the lab:
-f: Force move without prompting for confirmation-n: Do not overwrite an existing file-v: Verbose mode, explaining what is being done



