Renaming a file in Linux usually means moving it to a new path with mv. That works for a simple name change, but it also means the original path disappears—and an existing destination file may be replaced unless you take care.
Rename one file
Use the current name first and the new name second:
mv report-draft.txt report-final.txt
Because both paths are in the current directory, this renames the file in place. mv does not leave a second copy behind.
If you’re unsure where your terminal is, check with pwd, then list the current directory with ls. The ls command’s options can help you inspect names and file details before making a change.
Filenames with spaces or leading hyphens
Quote names containing spaces so the shell passes each filename as one argument:
mv 'quarterly report.txt' 'quarterly report final.txt'
Without quotes, the shell splits a name at its spaces, and mv receives extra arguments.
A filename that starts with - can be mistaken for an option. Prefix it with ./ to make it clear that it’s a path:
mv ./-draft.txt final.txt
On GNU/Linux, you can also use -- to mark the end of options:
mv -- -draft.txt final.txt
The ./ form is a straightforward way to avoid the ambiguity.
Avoid an accidental replacement
If the destination name already exists, common Linux mv implementations may replace it without prompting. Use -i to ask for confirmation before an overwrite:
mv -i old-name.txt new-name.txt
Review the prompt before answering. If you decline, the existing destination remains and the source is not moved over it.
You can check whether a destination currently exists with:
ls -l -- new-name.txt
Treat this as a quick inspection, not a guarantee: another process could create the destination after the check. If replacing a file would be especially harmful, check your system’s mv manual for the stronger protections available in that implementation.
Move and rename in one step
A destination path can include both a different directory and a new filename:
mv ./report-draft.txt ~/Documents/report-final.txt
This moves the file into ~/Documents and gives it the name report-final.txt. If you want to keep the original name, use the destination directory alone:
mv report-draft.txt ~/Documents/
In either case, mv relocates the original; it does not create a copy. If you need the old file to remain where it is, use cp instead. This guide to copying files with cp covers creating a copy under a different name.
Preview a batch rename before changing anything
For a repeatable batch operation, a Bash loop can show the proposed names before it renames files. This example previews adding archived- to .txt files in the current directory:
for file in ./*.txt; do
[ -e "$file" ] || continue
printf '%s -> %s\n' "$file" "./archived-${file#./}"
done
Review the output. If the paths look right, the same loop can perform the rename with GNU/Linux mv -n, which skips existing destinations:
for file in ./*.txt; do
[ -e "$file" ] || continue
mv -n -- "$file" "./archived-${file#./}"
done
These examples assume Bash and GNU coreutils. Try batch commands in a small test directory first, especially if files could produce conflicting destination names. The -n option and exact mv behavior can vary across implementations.
There is also a separate utility called rename, but multiple incompatible programs use that name. Its syntax depends on which implementation is installed, so check the local manual before using an example from another system.
One last distinction
Changing a filename extension changes only the name. Renaming report.txt to report.csv does not convert the file’s contents into CSV. Use mv when you intend to change a path or name; use a conversion tool when you need to change the file format.
I originally published a more detailed version of this guide on the SSHFlow blog.
I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.
Top comments (0)