Skip to main content

How do I convert spaces to underscores in file names and directory names on Linux?

Have you ever wanted to change all the spaces to underscores in a directory?  After going through various methods, the following is the most reliable way that I have found for bash users. Note, that there are many ways to accomplish this task. These examples should be typed in as a single line:



find . -depth -maxdepth 1 -name "* *" -exec sh -c 'mv "${0}" "${0// /_}"' {} \;


Note that '-maxdepth 1' changes spaces to underscores for files, directories, and links in the current directory. Changing that number to 2 or more will change spaces to underscores two or more levels deep in the current file system tree in addition to the current directory. Leaving out -maxdepth 1 will change spaces to underscores in the entire tree.

Note also that the '.' means start in the current directory. You can certainly put other paths in place of the dot.  You can even put what is called a globbing pattern:

find music* -depth -maxdepth 1 -name "* *" -exec sh -c 'mv "${0}" "${0// /_}"' {} \;

will process only files, directories, and links that begin with music.

Comments

Popular posts from this blog

Fixing SSH connection problems in EGit in Eclipse

Note: I posted a version of this on Stack Overflow. Errors can occur when there is an underlying SSH authentication issue, like having the wrong public key on the git remote server or if the git remote server changed its SSH host key. Often the an SSH error will appear as: " Invalid remote: origin: Invalid remote: origin" Eclipse will use the .ssh directory you specify in Preferences -> General -> Network Connections -> SSH2 for its ssh configuration. Set it "{your default user directory}.ssh\" . To fix things, first you need to determine which ssh client you are using for Git. This is stored in the GIT_SSH environmental variable. Right-click on "Computer" (Windows 7), then choose Properties -> Advanced System Settings -> Environment Variables. If GIT_SSH contains a path to plink.exe, you are using the PuTTY stack. To get your public key, open PuTTYgen.exe and then load your private key file (*.ppk). The listed public key sho...

How the find the Active Directory Domain Controllers listed in DNS on Linux...

Assumptions: You have the "host" utility from BIND. You can do a zone transfer from the local DNS server Your Active Directory admins have properly configured DNS for Active Directory If you have the above, use the following command: host -t srv -l your.active.directory.dns.domain | grep _kerberos._tcp.*._sites.dc._msdcs.your.active.directory.dns.domain Replace your.active.directory.dns.domain with your actual AD DNS domain.

How do I dual-boot Ubuntu 18.04 on a 2011 Mac Mini?

Machine Specs: Mac Mini (Mid 2011), Macmini5,2 Core i7 16 GB RAM (2x8 GB) AMD Radeon HD 6630M 256MB 27-inch Apple Thunderbolt Display 512 GB SSD Running macOS 10.13 High Sierra Caution: This process can result in data loss. If you have personal data on the Mac Mini before starting, back it up first. Twice! (One is none. Two is one. ~ U.S. Navy Seal saying).  Also, mistyping in the parts of this process may brick your Mac Mini. You have been warned. If in doubt, stop working and do research on Google until you understand what's going on in the step. If you find an error, please leave a comment. I will update. Note: This is process I figured out in lieu of booting the Ubuntu install with EFI via refind. Via the refind boot method, I would constantly get: 10:39:07 kernel: Couldn't get size: 0x800000000000000e 10:39:07 kernel: MODSIGN: Couldn't get UEFI db list 10:39:07 kernel: Couldn't get size: 0x800000000000000e The error seems to be relate...