0
0
Linux CLIscripting~20 mins

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

Choose your learning style9 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.