How to: replace spaces in file names with underscores

Linux shell

In Unix environments, a lot of work is done with the command interpreter, in fact, many systems do not even have a desktop environment installed. Therefore, in Linux you usually work a lot from the terminal. When working with file and directory names it is sometimes difficult when they have spaces in their names. Knowing how to avoid these little problems would not be a problem, but it is true that it is more comfortable when they do not have those spaces ...

If you do not know how to use names with spaces from the terminal or if you want to remove these spaces easilyIn this article you will learn everything you need to make the user experience as comfortable as possible when you work on GNU / Linux on a day-to-day basis. From now on, the spaces of the names will not be an obstacle for you ...

How to work with spaces

Linux namespaces

Working with spaces in names is straightforward, so first I'm going to show how you can use directories or files with spaces in their names. Thus, all those who have problems with this can learn to use it and lose the fear of coming across names with spaces.

  • For, indicate that there is a space in the name, you can use the slash or backslash, that is, \. For example, if the name you are trying to write is the one you see in the image "RISC-V docs", you can write "RISC-V \ docs" (without quotes). In this way you will be telling Bash that it is a space and not that you have entered several parameters for that command separated by a space.
  • You can also use the double quotes «» to enclose the name with spaces, indicating to the shell that they are not separate parameters, but that it is the same name. For example, in the previous example with cd, if you wanted to go to that directory with spaces you could use:
cd "RISC-V docs"

  • Another trick is to start typing the first letters of the name and press the Tab key (the one just above Caps Lock). That way the name is auto-completed. This will not work if there are several names that start the same, for example if there is a "RISC-V docs" and a "RISC-V isa" would not work. Yes, it would work in the case of having a "RISC-V docs" and "RISC-I docs", since as soon as you enter the character I, it will know the name of which it is ...

Now this you may not find it practical or you just don't like it having to be doing this every time you work with spaces. Therefore, if you want to eliminate them, you can read the following section ...

How to replace spaces

replace spaces with underscores Linux

It is true that for graphic environments, names with spaces are more comfortable and pleasant to name files. But it is not so practical for the terminal. Therefore, if you do not want to follow the steps in the previous section and want get rid of the blissful spaces once and for all, you can follow these steps:

  • Go to the directory where the names with spaces are found that you want to replace with any character. In this case I have used an underscore, but you can substitute that symbol for any other, you just have to modify the command.
  • Once inside the directory, you can use this command that will change all namespaces to _. Be careful, because if there are names in which you want to keep the spaces, you should consider that this command will change them all:
for file in *; do mv "$file" `echo $file | tr ' ' '_'`; done

  • For, go one by oneAlthough this is not practical, you can use the following command in cases where you only want to replace the spaces for a single name and not for all automatically:
mv nombre\ con\ espacios nombre_sin_espacios

  • Alternatively use a script that you can invoke whenever you want to replace spaces with underscores. So you can run it in any directory without having to write the command every time you need it. For that you have to use your favorite text editor and create a file called, for example, spaceskiller.sh, save the content, give it execution permissions, and enter it in a path of those in the $ PATH environment variable (eg : / usr / bin) and thus you will only have to invoke its name, without having to indicate the path where the .sh is. Ok, having said that, the content of the script would be the following:
<pre>#!/bin/bash

for f in *
do
  new="${f// /_}"
  if [ "$new" != "$f" ]
  then
    if [ -e "$new" ]
    then
      echo no se renombró \""$f"\" porque \""$new"\" ya existe
    else
      echo renombrando "$f" a "$new"
    mv "$f" "$new"
  fi
fi
done</pre>

If you have opted for the script and entered it in any of the $ PATH directories, every time you are inside a directory and you want to replace the spaces with underscores (or any other character if you modify it), you will only have to run:

./spaceskiller.sh

I hope I have helped you with this tutorial, you can leave your comments with any questions or contributions ...


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

Your email address will not be published.

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.