6

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 ?

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

Does putting quotation marks around the name not work?

cat "file name with space"
| improve this answer | |
8

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'
| improve this answer | |
5

Use the escape character '\' like this

cat file\ name\ with\ space
| improve this answer | |
2

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

cat "file name with space" 
| improve this answer | |
-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.

| improve this answer | |
  • 1
    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!! – Scott Feb 16 '17 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 '19 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.