Bulk file rename not working as expected
Hello everyone,
Here comes a question that most likely than not is very simple, but I think I'm blocked and I don't see it. Y want to rename a bunch of files and directories to remove the space on the names, easy enough right? Code:
for source in $(find ./); do target=$(echo "$source"|sed -e 's/ /_/g'); mv -f "$source" $target; done Code:
In this case "$source"="This is the file I want to rename" Running Fedorea core 14 Bash version 4.1.7 gnu sed version 4.2.1 Thank you. |
Try this way:
Code:
find | while read _source; do mv "${_source}" "${_source// /_}"; done |
Thank you, that did the trick.
Seems to be that echo cannot carried the spaces, it changes them with \n even before the substitution. Thank you allez! |
A for loop simply breaks the input (the part after "in", or the positional parameters) up into separate "words" and processes them individually. If you want to take "per line" input from an external command or file then you should use a while+read loop instead.
Also, while the above suggestion works in this situation, using a pipe to feed the loop is not the best option because the loop will run in a subshell and any variables set inside it will be lost when the loop terminates. This is the recommended way: Code:
while read _source; do (Edit: That find command will also include directories in the input, so you may have problems if you're trying to do this recursively, as it will rename the directory before the files inside it, and so subsequent mv commands will fail. You should use the -type f option to limit it to files only, then run it again with -type d to process the directories ). Incidentally, if all you want to do is grab files from the current directory, then you don't need find at all, just globbing, and you can go back to using a for loop (globbing expansion takes place after word breaking, so filenames won't be broken up). Code:
for source in ./* ; do Code:
rename -v 's/ /_/g' ./* http://www.google.com/search?&q=linux+bulk+rename |
All times are GMT -5. The time now is 09:41 PM. |