1

I do know that we can use

cat > fire

to make a file with the name "fire". But if the name of the file contains 2 words like "Grocery list", how does one make that?

Since

cat > Grocery list

gives the weird message cat: list: No such file or directory.

Please help me out.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • Also beware that > will overwrite the file; if you want to add more lines (append), use >> instead. – derobert Sep 30 '16 at 18:00

2 Answers2

6

Many ways:

  • Using single quotes around name:

    cat > 'Grocery list'
    
  • Using double quotes around name:

    cat > "Grocery list"
    
  • Escape the whitespace with \:

    cat > Grocery\ list
    

When you do:

cat > Grocery list

Grocery and list are taken as two words.

The output direction > Grocery is done by shell, and it happens first, so the file Grocery is created, and then cat list is run i.e. list is taken as an argument to cat, as presumably there is no file as list present in the current directory, hence the error about missing list file.

So, in this case, essentially you are doing:

cat list > Grocery
heemayl
  • 56,300
0

You need to escape the space somehow. heemayl's given you a good answer, but I recommend reading this one for details also: Why does my shell script choke on whitespace or other special characters?

As for creating the file itself, you don't need to use cat.

> file

is enough.