Linux / How to Rename a File or Directory
To rename a file or directory in Linux, specify the existing path and the new path in order with the mv command. If the target name already exists, it may be overwritten, or the source may be moved to an unexpected location, so it should be checked before execution.
Basic format for renaming
The mv used in the guide to moving files with mv is also used for renaming. If the two paths are in the same directory, only the name changes, and if they are in different directories, the location changes as well.
mv -i -- source destination
-i checks before overwriting an existing target file. -- indicates the end of options, so names beginning with a hyphen are not interpreted as options. The examples below are based on GNU coreutils mv and the Bash shell.
Preparing files and directories for practice
Practice in a temporary directory so as not to touch existing data. Execute the commands below in order in the same shell. The path of the newly created temporary directory will be stored in the variable workdir.
workdir=$(mktemp -d)
printf 'example\n' > "$workdir/report.txt"
mkdir "$workdir/docs"
ls -l -- "$workdir"
If report.txt and docs appear in the output, you are ready. To check the path, run printf '%s\n' "$workdir". Since the commands use variables from the current shell, if you open a new terminal, you need to prepare again.
Renaming files
Rename the practice file from report.txt to summary.txt and check the results. Make sure the new target does not already exist before executing.
ls -la -- "$workdir"
mv -i -- "$workdir/report.txt" "$workdir/summary.txt"
ls -l -- "$workdir"
cat -- "$workdir/summary.txt"
If successful, the list will show summary.txt instead of report.txt, and the last command will output example. When renaming within the same file system, the content remains the same. To revert to the original name, make sure the original name is not in use, then swap the two paths and run mv -i.
Renaming directories
Directories are renamed in the same way. The following command renames docs to archive in a practice environment where the target does not exist.
mv -i -T -- "$workdir/docs" "$workdir/archive"
ls -l -- "$workdir"
If successful, archive will be displayed instead of docs. -T is an option in GNU mv that does not treat the last path as the target directory specially. Without this option, if you specify an already existing directory as the target, you can move into it instead of renaming it. Even if you use -T, replacement or errors may occur depending on the type and status of the existing target, so check first. Non-empty target directories cannot be replaced.
Checking overwrite and path
If you specify an existing file as the target, the file may be overwritten. Back up important files first, and answer y only when overwriting in response to the confirmation question from -i. To avoid overwriting, enter n. The confirmation text may vary depending on the system and language settings.
If the filename contains spaces, surround the entire path with double quotes. The following is an example of creating a separate practice file and then renaming it.
printf 'notes\n' > "$workdir/old notes.txt"
mv -i -- "$workdir/old notes.txt" "$workdir/new notes.txt"
cat -- "$workdir/new notes.txt"
If notes is displayed at the end, the file has been read under a new name. In actual work, if the path is specified incorrectly or there is no permission, check the error message and the write permissions of the source and target directories. When moving to another file system, copying followed by deletion of the original may occur, so you need to check the space and the result. For more details on options and behavior, refer to the mv manual of GNU coreutils .




