10

I believe there's a simple fix but cannot debug it out.

I have file called "file name with space" How do I cat this file from Linux bash ?

6
  • 4
    cat "file name with space" or cat file\ name\ with\ space
    – ctn
    Jun 12, 2013 at 18:22
  • 3
    cat "file with space" doesn't work!? Good luck.
    – shellter
    Jun 12, 2013 at 18:22
  • 2
    Sure: name your file file_name_with_underscore...
    – H2CO3
    Jun 12, 2013 at 18:27
  • @H2CO3 Why if it works if everythin is implemented well?
    – glglgl
    Jun 12, 2013 at 18:34
  • @glglgl Safe, convenient, cross-platform and idiomatic.
    – H2CO3
    Jun 12, 2013 at 18:35

5 Answers 5

14

Does putting quotation marks around the name not work?

cat "file name with space"
9

A third option would be

cat 'file name with space'

where the file name may contain everything but the '.

If it does, such as file n'ame, replace every ' with '\'':

cat 'file n'\''ame'
5

Use the escape character '\' like this

cat file\ name\ with\ space
3

Enclosing the file in double quotes should work i.e.

cat "file name with space" 
-1

I have run into this problem on Linux and in Cygwin. The one thing I found that works is to enclose the name in double-quotes and to replace non-traditional characters with asterisks. For example:

tail File.basename.Job With Spaces.log

becomes...

tail "File.basename.Job*With*Spaces.log"

I tried escaping the spaces with backslashes, whether unprotected, protected with double-quotes, or protected with single-quotes, and in all cases tail parsed the names at the spaces as though unprotected.

3
  • 2
    This is WRONG.  Three (slightly) different perfectly good (correct) answers have already been given; if you have a problem with them, try again (and make sure you do what they say).  But your answer will not work!! Feb 16, 2017 at 22:26
  • This IS a good answer. When you have filenames in a file and want to cp them in a for loop, you use the backquote and cat the files.(e.g. for i in cat bakuplocations; do cp -u "$i" .; done) Then, if the filenames have spaces it will not work any of the other suggestions except this one.
    – user890332
    May 15, 2019 at 19:20
  • @user890332 No, then you are doing it wrong as it might catch other, slightly differently named files. If you have file names in a file, you do while read i; do cp -u "$i" .; done < backuplocations.
    – glglgl
    Jul 21 at 7:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .