How do you pass a file or folder containing whitespace as an argument to a command-line program in a GNU/Linux or Cygwin environment?

211

Solution 1

You would be better off using the -exec action (option) of find and quoting your arguments.

Example:

find . -type f -exec grep stuff '{}' \;

The quotes will keep the spaces from being interpreted and you don't have to pipe everything through xargs unnecessarily.

From the find man page:

-exec command ;

    Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of  ‘;’  is encountered. The string  ‘{}’  is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find.  Both of these constructions might need to be escaped (with a  ‘\’) or quoted to protect them from expansion by the shell. See the EXAMPLES section for examples of the use of the -exec option. The specified command is run once for each matched file. The command is executed in the starting directory.

    There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.

Solution 2

Pipe in your arguments using xargs by delimiting your command line arguments in the preceding program with a null character passing the "-0" option to xargs, such as follows:

find . -type f -print0 | xargs -0 grep -l "some text"

This command will pass in any files / folders with whitespaces in them as single arguments rather than separate arguments.

Solution 3

Use the backslash character '\' preceeding the whitespace:

[randerson@localhost ~]$ mkdir Folder\ With\ Spaces
[randerson@localhost ~]$ ls | grep Folder\ With\ Spaces
Folder With Spaces
[randerson@localhost ~]$ 

Solution 4

Enclose the file names in (single or double) quotation marks:

$ mkdir "Folder With Spaces"
$ ls | grep "Folder With Spaces"
Folder With Spaces
$ ls | grep 'Folder With Spaces'
Folder With Spaces

The shell will expand variables inside double quotation marks:

$ FOO=With
$ ls | grep "Folder $FOO Spaces"
Folder With Spaces

You can also quote $(...) expansions if the result is a single filename:

$ ls -d "$(echo -n Folder With Spaces)"
Folder With Spaces

This will only work for single file names; for multiple files, use find -exec or xargs as suggested in the other answers.

Share:
211

Related videos on Youtube

Jahnvi
Author by

Jahnvi

Updated on September 18, 2022

Comments

  • Jahnvi
    Jahnvi over 1 year

    We are using programmable video javascript SDK for video calling application. We use H264 video codec.

    In Android Chrome v80 local video and remote video works fine currently.

    But in some android devices like Samsung Galaxy 9+, Realme, OnePlus having Chrome version 81 are facing issue of remote user's video not playing. Local video plays but when remote user turns ON his video, it shows "No supported codec" in SDK errro and ERROR: chromium : [ERROR:internal_decoder_factory.cc(56)] Trying to create decoder for unsupported format.

    I checked that Chrome version > 57 supports H264 video codec. It is still working in Chrome version 80. Then why it stopped working in Android Chrome v81.x?

  • rmiesen
    rmiesen almost 11 years
    I use -exec on a routine basis, but for some reason it never occurred to me to simply put quotation marks around the brackets!
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' almost 9 years
    Actually, putting the {} in quotes has nothing to do with handling names that have spaces in them.