Move File To Another Directory in Linux: Expert Approaches
Managing file movement in Linux is fundamental—yet frequently mishandled when scaling up from simple personal scripts to automation at sysadmin scale. Data loss, inconsistent permissions, unexpected overwrites: each can result from an unconsidered mv
.
1. Handling Multiple Files, Shell Expansion, and Directory Existence
Suppose you need to move all .log
files from /var/tmp
to an archive. The naive approach:
mv /var/tmp/*.log /data/archive/
This only works if /data/archive/
exists; otherwise:
mv: cannot move '/var/tmp/example.log' to '/data/archive/example.log': No such file or directory
Recommendation: Always pre-create target directories in scripts:
mkdir -p /data/archive/
mv /var/tmp/*.log /data/archive/
Tip: Shell expansion (*
) doesn't match hidden files (.log1
isn’t matched). If including dotfiles is essential, use shopt -s dotglob
.
2. Protecting Data: Overwrite Controls
By default, mv
on GNU coreutils (tested with v8.32) will silently overwrite destination files. For many operational scripts, this creates risk.
-
Interactive confirmation:
mv -i data.db /backups/
Prompts if a file already exists.
-
No overwrite (safe for automation):
mv -n data.db /backups/
Note: Mixing -i
and -n
—the last flag wins. Always clarify operator precedence in scripts to avoid ambiguity.
3. Metadata, Filesystems, and Non-Obvious Rsync
Moving files across filesystems (/mnt/slowdisk
to /fastssd
) exposes a known issue: mv
copies, then deletes, transferring the file inode. File attributes, extended ACLs, and xattrs may get lost in uncontrolled moves.
Best practice: Use rsync
for integrity and auditability.
rsync -aAXH --remove-source-files --progress \
/mnt/slowdisk/project/ /fastssd/project/
-aAXH
ensures permissions, ACLs, xattrs, and hard links are preserved (supported since rsync 3.0).--progress
gives transfer feedback.- After directory transfer:
Gotcha:find /mnt/slowdisk/project/ -type d -empty -delete
--remove-source-files
leaves empty directories behind—clean them as shown.
4. Script Automation: Exit Codes and Error Handling
Consider automated ingestion scripts. A minimal approach:
#!/bin/bash
set -euo pipefail
src="/var/input/report.csv"
dst="/srv/warehouse/"
mv -n "$src" "$dst" \
&& logger -p local0.notice "Moved $src to $dst" \
|| { logger -p local0.err "Failed to move $src"; exit 1; }
Use set -euo pipefail
for strict bash error handling—common in controlled production environments.
Known issue: mv
does not atomic-move across filesystems; a partial copy may exist if the process is interrupted.
5. Pattern Moves: Find, Modification Time, and Automation
Organizing files by modification date is routine in logging and compliance pipelines:
mkdir -p /archive/$(date +%Y-%m-%d)
find /var/log/myapp -maxdepth 1 -type f -mtime -1 -exec mv {} /archive/$(date +%Y-%m-%d)/ \;
- Groups files modified in the last 24 hours.
- Potential race condition if files are written during the operation—schedule move jobs during low activity windows if consistency is necessary.
6. Non-Obvious Workflow: Transactional Moves with tmpfs
For heavy-duty automation (think: CI pipelines processing thousands of files), use a directory in tmpfs
to stage moves and minimize disk I/O:
mv /build/output/*.bin /mnt/tmpfs/staging/
# run post-processing, then persistent move:
rsync -aA --remove-source-files /mnt/tmpfs/staging/ /deploy/releases/
Benefit: If post-processing fails, the original artifacts remain untouched on persistent storage.
Summary Table
Method | Use Case | Preserves Metadata | Safe for Automation | Notes |
---|---|---|---|---|
mv | Same filesystem, simple moves | Yes | With care | Overwrites without warning |
mv -n/-i | Overwrite control | Yes | Yes | Use in scripts |
rsync -a --remove-source-files | Cross-filesystem, complex data | Yes | Yes | Removes only files, not dirs |
find ... -exec | Patterned/batched moves | Yes | Yes | For non-glob/complex filters |
Practical Takeaway
File relocation under Linux is not just a matter of mv
. In production—where automation, metadata, and data safety intersect—select tools and flags deliberately. Simple solutions work until they don’t; monitor for edge cases: partial moves, permission drifts, and non-globbed files.
For deeper control, audit with rsync
and tailor error handling in scripts. Always validate workflows on non-production datasets before integrating into a larger CI/CD or backup flow. Sometimes, avoiding a single misplaced file is worth the extra effort.