replace spaces in filenames with underscores

replace spaces in filenames with underscores
Name: replace spaces in filenames with underscores
Submitted: 17 Aug 2012
Submitter: NickTheGreek
Category: YourForum Tutorials
Views: 366
Rating: 0
Description: using SSH


Directions

well, unusual but can happen to anyone, we need to replace all spaces in filenames with underscores for easier link sharing !

Solution(s)

1.
CODE
$rename 'y/ /_/' *


replace spaces in filenames with underscores

This command will replace all the spaces in all the filenames of the current directory with underscores. There are other commands that do this here, but this one is the easiest and shortest.

2.
CODE
$
ls -1 | while read file; do new_file=$(echo $file | sed s/\ //g); mv "$file" "$new_file"; done


Renames all files in the current directory such that the new file contains no space characters.

File names with spaces may cause problems, this command could help to avoid that.

3.
CODE
$ rename 's/ /_/g' *


Renames all files in the current directory such that the new file contains no space characters.