Bird
Raised Fist0
Linux CLIscripting~20 mins

mv (move and rename) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
mv Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this mv command?
You have a file named report.txt in your current directory. You run the command:

mv report.txt archive/

What happens after this command?
AThe command fails because <code>archive</code> is not a file.
BThe file <code>report.txt</code> is moved into the <code>archive</code> directory.
CA new file named <code>archive</code> is created with the contents of <code>report.txt</code>.
DThe file <code>report.txt</code> is renamed to <code>archive</code> in the current directory.
Attempts:
2 left
💡 Hint
Think about how mv works when the destination is a directory.
💻 Command Output
intermediate
2:00remaining
What is the result of renaming a file with mv?
You have a file named data.csv. You run:

mv data.csv data_backup.csv

What is the state of the files after this command?
AThe file <code>data.csv</code> is renamed to <code>data_backup.csv</code> in the same directory.
BThe command copies <code>data.csv</code> to <code>data_backup.csv</code> but keeps the original.
CBoth <code>data.csv</code> and <code>data_backup.csv</code> exist with the same content.
DThe command fails because <code>data_backup.csv</code> does not exist.
Attempts:
2 left
💡 Hint
mv can rename files by changing the filename in the same directory.
💻 Command Output
advanced
2:00remaining
What error does this mv command produce?
You run the command:

mv file1.txt /root/

as a normal user (not root). What happens?
APermission denied error because the user cannot write to /root directory.
BThe file <code>file1.txt</code> is moved successfully to /root.
CThe file is renamed to /root in the current directory.
DThe command creates a copy of <code>file1.txt</code> in /root.
Attempts:
2 left
💡 Hint
Consider user permissions for system directories.
💻 Command Output
advanced
2:00remaining
What is the output of this mv command with a wildcard?
You have files: img1.png, img2.png, and img3.png in your current directory. You run:

mv img*.png images/

What happens?
AThe command fails because wildcards are not supported by mv.
BOnly <code>img1.png</code> is moved to <code>images</code> directory.
CThe command renames the first matching file to <code>images</code>.
DAll files starting with 'img' and ending with '.png' are moved into the <code>images</code> directory.
Attempts:
2 left
💡 Hint
mv supports wildcards expanded by the shell.
🚀 Application
expert
3:00remaining
How to move and rename multiple files with a script?
You want to move all .log files from logs/ directory to archive/ directory and rename them by adding the prefix old_ to each filename.

Which bash script snippet correctly does this?
Amv logs/*.log archive/old_*.log
Bfor f in logs/*.log; do mv "$f" "archive/$(basename "$f")_old"; done
Cfor f in logs/*.log; do mv "$f" "archive/old_$(basename "$f")"; done
Dmv logs/*.log archive/old_log
Attempts:
2 left
💡 Hint
You need to loop over files and rename each individually.

Practice

(1/5)
1. What does the mv command do in Linux?
easy
A. Copies files and folders
B. Lists files and folders
C. Deletes files and folders
D. Moves or renames files and folders

Solution

  1. Step 1: Understand the purpose of mv

    The mv command is used to move files or folders from one location to another or to rename them.
  2. Step 2: Compare with other commands

    Unlike cp which copies, or rm which deletes, mv changes the location or name without duplicating.
  3. Final Answer:

    Moves or renames files and folders -> Option D
  4. Quick Check:

    mv = move or rename [OK]
Hint: Remember: mv = move or rename files [OK]
Common Mistakes:
  • Confusing mv with cp (copy)
  • Thinking mv deletes files
  • Assuming mv lists files
2. Which of the following is the correct syntax to rename a file old.txt to new.txt using mv?
easy
A. mv old.txt new.txt
B. mv new.txt old.txt
C. mv -r old.txt new.txt
D. mv old.txt -n new.txt

Solution

  1. Step 1: Understand basic mv syntax

    The syntax is mv source destination. To rename, source is old name, destination is new name.
  2. Step 2: Check each option

    mv old.txt new.txt correctly uses mv old.txt new.txt. mv new.txt old.txt reverses source and destination. Options A and D use invalid options.
  3. Final Answer:

    mv old.txt new.txt -> Option A
  4. Quick Check:

    mv source destination = rename [OK]
Hint: Syntax: mv oldname newname to rename [OK]
Common Mistakes:
  • Swapping source and destination
  • Using invalid options like -r or -n
  • Confusing mv with cp syntax
3. Given the commands:
mkdir testdir
touch file1.txt
mv file1.txt testdir/
What will be the result?
medium
A. Error: testdir does not exist
B. file1.txt is moved inside testdir folder
C. A new file named testdir is created
D. file1.txt is renamed to testdir

Solution

  1. Step 1: Create directory and file

    mkdir testdir creates a folder named testdir. touch file1.txt creates an empty file named file1.txt.
  2. Step 2: Move file into directory

    mv file1.txt testdir/ moves file1.txt into the folder testdir, keeping the same name.
  3. Final Answer:

    file1.txt is moved inside testdir folder -> Option B
  4. Quick Check:

    mv file to folder = move inside folder [OK]
Hint: mv file folder/ moves file inside folder [OK]
Common Mistakes:
  • Thinking mv renames file to folder name
  • Assuming mv creates new files
  • Believing error occurs if folder exists
4. What is wrong with this command if you want to rename file.txt to document.txt but get an error?
mv file.txt document.txt/
medium
A. Trailing slash after document.txt treats it as a directory
B. Missing -r option to rename files
C. Source file does not exist
D. mv cannot rename files

Solution

  1. Step 1: Analyze the command

    The command has document.txt/ with a trailing slash, which means it expects a directory named document.txt.
  2. Step 2: Understand error cause

    If document.txt is not a directory, mv will error because it cannot move file.txt into a non-existent directory.
  3. Final Answer:

    Trailing slash after document.txt treats it as a directory -> Option A
  4. Quick Check:

    Trailing slash means directory, causes error if not directory [OK]
Hint: No slash after filename when renaming [OK]
Common Mistakes:
  • Adding slash after new filename
  • Thinking -r is needed to rename
  • Assuming mv cannot rename files
5. You want to move all .txt files from folder1 to folder2 but avoid overwriting existing files in folder2. Which command should you use?
hard
A. mv -f folder1/*.txt folder2/
B. mv folder1/*.txt folder2/
C. mv -i folder1/*.txt folder2/
D. mv -r folder1/*.txt folder2/

Solution

  1. Step 1: Understand the goal

    You want to move all .txt files but be prompted before overwriting existing files in folder2.
  2. Step 2: Choose correct option

    The -i option asks for confirmation before overwriting. mv -i folder1/*.txt folder2/ uses mv -i correctly. mv folder1/*.txt folder2/ moves without prompt. mv -f folder1/*.txt folder2/ forces overwrite. mv -r folder1/*.txt folder2/ is invalid for files.
  3. Final Answer:

    mv -i folder1/*.txt folder2/ -> Option C
  4. Quick Check:

    Use -i to prompt before overwrite [OK]
Hint: Use mv -i to confirm before overwrite [OK]
Common Mistakes:
  • Using -f which forces overwrite
  • Forgetting -i and overwriting silently
  • Using -r which is for directories
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard