Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for name in `ls` and filenames with spaces

next code doesnt work because of spaces in file names, How to fix?

IFS = '\n' for name in `ls ` do     number=`echo "$name" | grep -o "[0-9]\{1,2\}"`     if [[ ! -z "$number" ]]; then         mv "$name" "./$number"     fi done 
like image 444
Yola Avatar asked Dec 27 '11 14:12

Yola


People also ask

How do you handle spaces in filename?

Use quotation marks when specifying long filenames or paths with spaces. For example, typing the copy c:\my file name d:\my new file name command at the command prompt results in the following error message: The system cannot find the file specified.

Can Linux file names have spaces?

It's not very common in Linux to handle filename with spaces but sometimes files copied or mounted from windows would end up with spaces. While it is not recommended to have file names with spaces, let's discuss how to manage filename with spaces in a Linux system.

Can a filename include spaces?

It's allowed but it's really, really annoying. There is no reason for it. Don't do it. You can also create a files named -rf ~ (use touch -- "-rf ~" ), but I wouldn't recommend it.


1 Answers

Just don't use command substitution: use for name in *.

like image 51
fge Avatar answered Oct 08 '22 04:10

fge