How to Rename a Directory in Linux

Renaming a directory in Linux is a straightforward task. The mv command, short for “move,” is used not only for moving files and directories but also for renaming them. Whether you’re working in a terminal or using a script, understanding how to change directory names efficiently is an essential skill for any Linux user.


Basic Command to Rename a Directory

The simplest way to rename a directory in Linux is by using the mv command with the following syntax:

mv [current_directory_name] [new_directory_name]
  • [current_directory_name]: The name of the directory you want to rename.
  • [new_directory_name]: The new name you want to assign to the directory.

Example:

If you have a directory named old_folder and you want to rename it to new_folder, you can run:

mv old_folder new_folder

This changes the directory’s name without altering its contents or location.


Options for Renaming a Directory

The mv command can be used with several options that modify its behavior when renaming directories:

1. Interactive Mode (-i)

If you want to be prompted before overwriting an existing directory with the same name, use the -i option. This is useful if you’re unsure whether the new name already exists and want confirmation before proceeding.

mv -i old_folder new_folder
  • If new_folder already exists, the terminal will prompt you with mv: overwrite 'new_folder'?. You can respond with y to confirm or n to cancel.

2. Verbose Mode (-v)

For better visibility of what the command is doing, you can use the -v option. This will print the action of renaming the directory to the terminal.

mv -v old_folder new_folder
  • The terminal will display: ‘old_folder’ -> ‘new_folder’, showing you the renaming action.

3. Force Overwrite (-f)

If you want to force the renaming even if the target directory already exists, use the -f option. This will automatically overwrite the existing directory without asking for confirmation.

mv -f old_folder new_folder
  • This option should be used with caution as it will replace any existing directory with the new name.

4. Backup (-b)

If you want to back up the target directory before overwriting it, you can use the -b option. This is helpful when you want to preserve the existing directory under the new name, just in case something goes wrong.

mv -b old_folder new_folder
  • The -b option will create a backup of the target directory, appending a ~ symbol to the original directory name.

Additional Considerations

  1. Renaming a Directory Across Different Locations:
    The mv command can also move directories across different locations while renaming them. For example:
   mv /home/user/old_folder /home/user/new_folder

This moves old_folder from its current location to the new location and renames it to new_folder.

  1. Renaming Multiple Directories:
    The mv command only works for one directory at a time. If you need to rename multiple directories, consider writing a script or using a loop to automate the process.
  2. Using Wildcards:
    You can also use wildcards to rename directories with similar names. For example, renaming all directories starting with “project_” to “archive_”:
   for dir in project_*; do
     mv "$dir" "${dir/project_/archive_}"
   done

This command will rename directories like project_2021 to archive_2021.


Practical Example

Let’s say you have a directory structure like this:

/home/user/documents/project

You want to rename the directory project to final_project. You would use the following command:

mv /home/user/documents/project /home/user/documents/final_project

After executing this, the directory structure would be:

/home/user/documents/final_project

Conclusion

Renaming a directory in Linux is simple with the mv command. By understanding the available options, such as -i, -v, and -f, you can have better control over the renaming process, ensuring no files or directories are accidentally overwritten.


Code Summary

mv [current_directory_name] [new_directory_name]        # Basic rename command
mv -i [current_directory_name] [new_directory_name]     # Interactive mode, prompts before overwriting
mv -v [current_directory_name] [new_directory_name]     # Verbose mode, displays actions being taken
mv -f [current_directory_name] [new_directory_name]     # Force mode, overwrites without confirmation
mv -b [current_directory_name] [new_directory_name]     # Backup mode, creates a backup of the original directory
Edvaldo Guimrães Filho Avatar

Published by

Categories: ,

Leave a comment