How to Rename a Directory with Spaces in Linux

Written by: Linuxopsys   |   Last updated: December 26, 2022

Here we learn how to rename a Linux directory whose names contain special characters such as space. Generally, we use the mv command to rename a directory. We will make use of quotation marks, escape characters, and a couple of alternative methods to handle it.

1. Using quotation marks to rename directories with spaces

When mv command encounters a directory with spaces it will be treated as a separator and the next text you type will be considered as the destination folder name. To overcome this problem we need to use source and destination directory names enclosed in quotes.

Example:

I have a folder named "I am a Linux User" in my current working directory.

ls

Now, I want to rename the directory as "I am a Ubuntu User".

mv 'I am a Linux User' 'I am a Ubuntu User'
example rename directory with spaces using quotes

The quotes prevent the shell from interpreting the special character as a separator or as a command.

2. Using Escape Characters to rename directories with spaces

The backslash tells the shell to treat the space as a regular character and not as a separator between two separate arguments. ie use a backslash ( \ ) before each special character to escape it.

Syntax:

mv source\ directory\ name destination\ directory\ name

Example:

mv I\ am\ a\ Linux\ User I\ am\ a\ Ubuntu\ User
example rename directory with spaces using Escape Characters

3. Alternative solutions

Alternatively, you can also use the rename command to rename directories with special characters. Often comes to help to rename multiple directories or files.

Here's an example of how to use rename to rename all directories in the current directory that have a space in their name:

rename 's/ /_/g' *
example rename directory with spaces using rename command

The above command will replace all spaces in the names of the directories with underscores in the current directory.

To perform recursively for all directories with spaces:

find . -type d | sort -r | rename -d 's/\s+/_/g'

The command uses a regular expression to search for one or more consecutive whitespace characters (represented by \s+) and replaces them with an underscore (represented by _). The g at the end of the command stands for "global", which means that all occurrences of the pattern will be replaced, not just the first one.

Note: The rename command function differs in different Linux Distributions. The above examples are from Ubuntu Distro but may differ on Redhat.

Bash script

The following bash script renames all directories with spaces in directory names to underscores.

#!/bin/bash
for d in *\ */
do
 mv "$d" "${d// /_}"
done

Here we simply used a for loop to achieve the task.

Conclusion

In Unix and Linux systems, It is a bad idea to use or manage directories or files with names containing space. This is because, in Linux systems, the space character is used to separate arguments in the command line.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment