Cannot cat file which has space in name in linux

64,533

Solution 1

Does putting quotation marks around the name not work?

cat "file name with space"

Solution 2

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'

Solution 3

Use the escape character '\' like this

cat file\ name\ with\ space

Solution 4

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

cat "file name with space" 
Share:
64,533

Related videos on Youtube

Maulzey
Author by

Maulzey

Updated on September 18, 2022

Comments

  • Maulzey
    Maulzey over 1 year

    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 ?

    • Admin
      Admin almost 11 years
      cat "file name with space" or cat file\ name\ with\ space
    • shellter
      shellter almost 11 years
      cat "file with space" doesn't work!? Good luck.
    • H2CO3
      H2CO3 almost 11 years
      Sure: name your file file_name_with_underscore...
    • glglgl
      glglgl almost 11 years
      @H2CO3 Why if it works if everythin is implemented well?
    • H2CO3
      H2CO3 almost 11 years
      @glglgl Safe, convenient, cross-platform and idiomatic.
    • ganesh
      ganesh almost 11 years
      A fourth option would be to change the IFS.
  • Scott - Слава Україні
    Scott - Слава Україні over 7 years
    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!!
  • user890332
    user890332 about 5 years
    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.