Intern
How to rename a file in Linux?
sobes.tech AI
Answer from AI
In Linux, the mv command is used to rename a file or move it to another location.
Syntax of the command:
# Rename a file
mv <old_name> <new_name>
# Move a file to another directory while keeping the name
mv <file_name> <target_directory>/
# Move a file to another directory and rename it
mv <file_name> <target_directory>/<new_name>
Examples:
- Rename the file
old_file.txttonew_file.txtin the current directory:mv old_file.txt new_file.txt - Move the file
my_document.pdffrom the current directory to/home/user/documents/:mv my_document.pdf /home/user/documents/ - Move the file
report.csvfrom the current directory to/archive/and rename it tofinal_report_2023.csv:mv report.csv /archive/final_report_2023.csv
If the specified <new_name> or <target_directory>/<new_name> already exists, the file will be overwritten by default.
Useful options for mv:
-i(interactive): Prompts for confirmation before overwriting an existing file.-f(force): Forces overwriting of existing files, suppressing prompts (-i).-u(update): Moves/renames the file only if the source file is newer than the destination or if the destination does not exist.-v(verbose): Outputs detailed information about the actions being performed.
Examples of using options:
# Prompt for confirmation before overwriting
mv -i my_file.txt existing_file.txt
# Force overwrite
mv -f important.log /var/log/important.log