How do I rename a file under Linux operating systems using command line (bash shell prompt)?
You need to use the mv command to rename files or directories under Linux operating systems. The same command is also used to move files to different directories.
The mv Command Syntax
The syntax is as follows:
mv source target mv [Options] source target
Take: Rename a File Under Linux
Rename a file called curriculum-vitae.txt as resume.txt, enter:
$ mv curriculum-vitae.txt resume.txt
If the target file (resume.txt) is located in the same directory as the source file (curriculum-vitae.txt), then the source file (curriculum-vitae.txt) can only be renamed.
Rename / Move Confirmation (Interactive) Option
You can force to display prompt before overwriting files. The -i option enables interactive option. So if file or directories with the same name already exists in the destination directory, mv will prompt the user:
$ mv -i file2.txt /tmp/
mv: overwrite `/tmp/file2.txt'?
Move / Rename Verbose Option
Pass the -v option to display the name of each file before renaming and/or moving it:
$ mv -v file3.txt /tmp/
`file3.txt' -> `/tmp/file3.txt'
To rename file3.txt as file10.bak, enter:
$ mv -v file3.txt file10.bak
`file3.txt' -> `file10.bak'
Backup a File
To make a backup of each existing destination file pass the -b option. This option will tell mv to make a backup copy of each file that may be overwritten or removed:
$ touch file10.txt
$ mv -v -b file10.txt /tmp
`file10.txt' -> `/tmp/file10.txt'
To view a backup file called file10.bak, enter:
Moving A file
In this example, move a file called file1.txt to /tmp/ directory, enter:
$ mv file1.txt /tmp/
In this example, move all files and directories, including all the contents of those directories, from the current directory to the directory /home/newdir:
# cd /home/olddir/
# mv * /home/newdir/
Please note that the asterisk (symbol) is nothing but a shell wildcard character that represents all files.
Other mv Command Options
From the mv command man page:
--backup[=CONTROL] make a backup of each existing destination file -f, --force do not prompt before overwriting -i, --interactive prompt before overwrite -n, --no-clobber do not overwrite an existing file If you specify more than one of -i, -f, -n, only the final one takes effect. --strip-trailing-slashes remove any trailing slashes from each SOURCE argument -S, --suffix=SUFFIX override the usual backup suffix -t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY -T, --no-target-directory treat DEST as a normal file -u, --update move only when the SOURCE file is newer than the destination file or when the destination file is missing -v, --verbose explain what is being done --help display this help and exit --version output version information and exitTwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!
- 30 Cool Open Source Software I Discovered in 2013
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop