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.

1
  • Also beware that > will overwrite the file; if you want to add more lines (append), use >> instead.
    – derobert
    Commented Sep 30, 2016 at 18:00

2 Answers 2

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

You must log in to answer this question.

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