mv (move and rename) in Linux CLI - Time & Space Complexity
When using the mv command to move or rename files/directories, it's helpful to understand how the time it takes grows as the number of files or size changes.
We want to know how the command's work changes when moving one file versus many files in a folder. Key factor: whether source and destination are on the same filesystem.
Analyze the time complexity of the following mv command usage (assuming same filesystem).
mv source.txt destination.txt
mv folder1/ folder2/
This moves or renames a single file or an entire folder to a new location or name.
Look at what the command does repeatedly.
- Primary operation: Single
rename(2)system call to update directory entry. - How many times: Once, regardless of the number of files inside directories.
Note: mv uses rename() if source and dest are on the same filesystem (fast). Otherwise, it copies all contents (slow, O(n)).
As the number of files grows, the work stays constant (for same filesystem).
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files | 1 rename operation |
| 100 files | 1 rename operation |
| 1000 files | 1 rename operation |
Pattern observation: The time is constant, independent of the number of files (same filesystem).
Time Complexity (same filesystem): O(1)
Time Complexity (different filesystems): O(n) where n is total data size.
This means the time is constant for typical same-disk moves.
[X] Wrong: "Moving a folder always takes time proportional to the number/size of files inside."
[OK] Correct: On the same filesystem, mv just updates the parent directory's entry (atomic rename), independent of contents.
Understanding mv's behavior (O(1) vs O(n)) shows knowledge of filesystems, syscalls, and efficiency in scripting/automation.
What if we moved files across different disks instead of within the same disk? How would the time complexity change?
Answer: Becomes O(n) as it recursively copies all contents then deletes source.
Practice
mv command do in Linux?Solution
Step 1: Understand the purpose of
Themvmvcommand is used to move files or folders from one location to another or to rename them.Step 2: Compare with other commands
Unlikecpwhich copies, orrmwhich deletes,mvchanges the location or name without duplicating.Final Answer:
Moves or renames files and folders -> Option DQuick Check:
mv= move or rename [OK]
- Confusing mv with cp (copy)
- Thinking mv deletes files
- Assuming mv lists files
old.txt to new.txt using mv?Solution
Step 1: Understand basic
The syntax ismvsyntaxmv source destination. To rename, source is old name, destination is new name.Step 2: Check each option
mv old.txt new.txt correctly usesmv old.txt new.txt. mv new.txt old.txt reverses source and destination. Options A and D use invalid options.Final Answer:
mv old.txt new.txt -> Option AQuick Check:
mv source destination = rename [OK]
- Swapping source and destination
- Using invalid options like -r or -n
- Confusing mv with cp syntax
mkdir testdirtouch file1.txtmv file1.txt testdir/What will be the result?
Solution
Step 1: Create directory and file
mkdir testdircreates a folder named testdir.touch file1.txtcreates an empty file named file1.txt.Step 2: Move file into directory
mv file1.txt testdir/moves file1.txt into the folder testdir, keeping the same name.Final Answer:
file1.txt is moved inside testdir folder -> Option BQuick Check:
mv file to folder = move inside folder [OK]
- Thinking mv renames file to folder name
- Assuming mv creates new files
- Believing error occurs if folder exists
file.txt to document.txt but get an error?mv file.txt document.txt/Solution
Step 1: Analyze the command
The command hasdocument.txt/with a trailing slash, which means it expects a directory named document.txt.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.Final Answer:
Trailing slash after document.txt treats it as a directory -> Option AQuick Check:
Trailing slash means directory, causes error if not directory [OK]
- Adding slash after new filename
- Thinking -r is needed to rename
- Assuming mv cannot rename files
.txt files from folder1 to folder2 but avoid overwriting existing files in folder2. Which command should you use?Solution
Step 1: Understand the goal
You want to move all .txt files but be prompted before overwriting existing files in folder2.Step 2: Choose correct option
The-ioption asks for confirmation before overwriting. mv -i folder1/*.txt folder2/ usesmv -icorrectly. mv folder1/*.txt folder2/ moves without prompt. mv -f folder1/*.txt folder2/ forces overwrite. mv -r folder1/*.txt folder2/ is invalid for files.Final Answer:
mv -i folder1/*.txt folder2/ -> Option CQuick Check:
Use -i to prompt before overwrite [OK]
- Using -f which forces overwrite
- Forgetting -i and overwriting silently
- Using -r which is for directories
Ready to prove it?
Pass the test and this pattern moves unknown
