What’s the standard way to rename files from the command line?
Asked by
yield82pct
I'm comfortable using a terminal file manager, but I'm tired of relying on it whenever I need to rename something. What commands or tools do you usually use to rename a single file or rename multiple files in a shell?
Answers
A complicated script that copies file contents, reconstructs the destination, verifies everything, and then deletes the original is unnecessary here. A rename within the same filesystem normally just changes the directory entry, so `mv` is faster and preserves the file’s contents and metadata.
Reply to this answer
For batches of files, many Linux systems also provide a `rename` command. Its syntax and implementation can vary between distributions, so check `man rename` first. For simple one-off changes, though, `mv` is usually clearer.
Reply to this answer
For a single file, the usual command is simply `mv old-name new-name`. Despite the name, `mv` works for both moving and renaming files. Quote the paths if they contain spaces or special characters.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Transit pass holder+: Adding `-i` is a good safety improvement: `mv -i old-name new-name`. It asks before overwriting an existing destination.