Cannot cat file which has space in name in linux

5

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 ?

Maulzey

Posted 2013-06-12T18:21:36.870

Reputation: 193

2cat "file name with space" or cat file\ name\ with\ space – None – 2013-06-12T18:22:50.313

2cat "file with space" doesn't work!? Good luck. – shellter – 2013-06-12T18:22:51.777

Sure: name your file file_name_with_underscore... – H2CO3 – 2013-06-12T18:27:12.927

@H2CO3 Why if it works if everythin is implemented well? – glglgl – 2013-06-12T18:34:48.303

@glglgl Safe, convenient, cross-platform and idiomatic. – H2CO3 – 2013-06-12T18:35:50.127

A fourth option would be to change the IFS. – Hennes – 2013-06-12T20:21:03.567

Answers

10

Does putting quotation marks around the name not work?

cat "file name with space"

Phil Perry

Posted 2013-06-12T18:21:36.870

Reputation: 254

7

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'

glglgl

Posted 2013-06-12T18:21:36.870

Reputation: 1 327

5

Use the escape character '\' like this

cat file\ name\ with\ space

scarecrow

Posted 2013-06-12T18:21:36.870

Reputation: 159

2

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

cat "file name with space" 

Afsal

Posted 2013-06-12T18:21:36.870

Reputation:

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

Scot Harkins

Posted 2013-06-12T18:21:36.870

Reputation: 11

1This 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 – 2017-02-16T22:26:19.643

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 incat 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 – 2019-05-15T19:20:59.860