Removal of colons in directory name

Currently the directory getting automatically created for a program I am working with includes colons (:), creating issues when I try manipulate the directory. Could anyone help me first remove this colon, and then be able to cd into the renamed directory? (So I guess I would have to equal the new directory name to a variable?)

Example:

  • current directory name: Lang-32b-Branch:Line
  • desired directory name: Lang-32b-BranchLine

To achieve this manually, an in a simple manner you could use the mv command with some strong quoting to ensure that the shell doesn't interpret the colon as a special character:

mv 'Lang-32b-Branch:Line' 'Lang-32b-BranchLine' && cd 'Lang-32b-BranchLine'

An alternate approach is to use a variable and the one of the features of Bash parameter expansion, which may be more useful if you wish to automate this directory name change into a script:

dir=Lang-32b-Branch:Line
mv "$dir" "${dir//:/}"

The expansion of "${dir//:/}" will replace any occurrence of the colon character with nothing, giving the expected result of Lang-32b-BranchLine. Using "${dir/:/}" would result in only the first occurrence of the colon being removed, though that would still work for your given example.

As a one-liner to move and cd into the directory:

dir=Lang-32b-Branch:Line ; mv "$dir" "${dir//:/}" && cd "${dir//:/}"

Or if you wished to capture the modified directory name into a variable named new_dir:

dir=Lang-32b-Branch:Line ; new_dir="${dir//:/}"; mv "$dir" "$new_dir" && cd "$new_dir"

A great guide to Bash parameter expansion is available here.


If you want a general command to rename several files and/or directories with one or more colons, you can use rename,

rename 's/://g' *

After that you can cd into any of the directories (that you modified) as usual (without any quotes).


Alternate command line according to the tips in comments by @DavidFoerster and @dessert,

rename 's/://g' *\:*/
  • If you want to limit the rename actions to directories, you should add a slash after the wild-card star.
  • You can also put a colon in the wild-card expression to limit the selection of files to those, that contain a colon in the file name, which might make the action faster, if a huge amount of directories are to be searched,

Edit:

@EliahKagan suggests to start with a 'dry run' with the option -n,

rename -n 's/://g' *

which just prints out what renaming operations would be performed. Then it can be run again with the -n option removed to actually rename the files. This helps avoid mistakes such as accidentally renaming a bunch of files one actually wanted to leave alone.


If it's only the one directory you can use mv and cd. To make use of the power of bash History Expansion and the special parameter $_:

mv Lang-32b-Branch:Line !#:$:s/:/ && cd $_

!#:$:s/:/ is replaced with the last word ($) of the current command line (!#), where the first : is substituted by nothing. If you want every colon to be removed, change it to !#:$:gs/:/ (globally). $_ is simply replaced with the last command's last argument.