Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash and filenames with spaces

Tags:

The following is a simple Bash command line:

grep -li 'regex' "filename with spaces" "filename" 

No problems. Also the following works just fine:

grep -li 'regex' $(<listOfFiles.txt) 

where listOfFiles.txt contains a list of filenames to be grepped, one filename per line.

The problem occurs when listOfFiles.txt contains filenames with embedded spaces. In all cases I've tried (see below), Bash splits the filenames at the spaces so, for example, a line in listOfFiles.txt containing a name like ./this is a file.xml ends up trying to run grep on each piece (./this, is, a and file.xml).

I thought I was a relatively advanced Bash user, but I cannot find a simple magic incantation to get this to work. Here are the things I've tried.

grep -li 'regex' `cat listOfFiles.txt` 

Fails as described above (I didn't really expect this to work), so I thought I'd put quotes around each filename:

grep -li 'regex' `sed -e 's/.*/"&"/' listOfFiles.txt` 

Bash interprets the quotes as part of the filename and gives "No such file or directory" for each file (and still splits the filenames with blanks)

for i in $(<listOfFiles.txt); do grep -li 'regex' "$i"; done 

This fails as for the original attempt (that is, it behaves as if the quotes are ignored) and is very slow since it has to launch one 'grep' process per file instead of processing all files in one invocation.

The following works, but requires some careful double-escaping if the regular expression contains shell metacharacters:

eval grep -li 'regex' `sed -e 's/.*/"&"/' listOfFiles.txt` 

Is this the only way to construct the command line so it will correctly handle filenames with spaces?

like image 765
Jim Garrison Avatar asked Oct 15 '09 20:10

Jim Garrison


People also ask

How do you handle spaces in file names?

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 filename 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.


1 Answers

Try this:

(IFS=$'\n'; grep -li 'regex' $(<listOfFiles.txt)) 

IFS is the Internal Field Separator. Setting it to $'\n' tells Bash to use the newline character to delimit filenames. Its default value is $' \t\n' and can be printed using cat -etv <<<"$IFS".

Enclosing the script in parenthesis starts a subshell so that only commands within the parenthesis are affected by the custom IFS value.

like image 188
Stephan202 Avatar answered Oct 04 '22 04:10

Stephan202