How would I create a .txt file, customize what it says, and open it. All with Terminal. [OSX Yosemite]

  • if you get answers, choose the right answer & kindly up-vote them if they are correct. – Ani Menon Apr 22 '16 at 19:16

For personal use (ie, not in scripts) I recommend nano. A somewhat controversial choice among frequent terminal users, but unlike viand vim, nano isn't modal (ie, it's going to work the way you would expect a graphical text editor to work). Using nano will let you perform all three actions (create the file, edit the text, and view it) all with one command. Your syntax will be nano /path/to/file.txt. Of course, you can also use vi or vim, but if you choose to you definitely need to read about how first (if you're coming from a graphical background you've almost certainly never even heard of a modal editor before).

To create the file without opening the editor, use touch. This creates an empty file. The syntax would be touch /path/to/file.txt. If you need to make a new folder to put the file in, use mkdir. The syntax is mkdir /path/to.

To view the file without opening the editor, use cat (syntax cat /path/to/file.txt) if it's short. This will just print the whole file to the terminal. If the file is longer, use less or more, which will each allow some navigation (eg, they'll let you scroll). By this point, you can probably guess the syntax. They're very similar commands: more or less, less is more, but more (less lets you scroll backward, more does not). Of course, you can also just look at the file in nano and then not write any changes.

Finally, if you cd into your working directory first, you can just refer to the file with its name without the path (eg, nano file.txt, and similar for the other commands I mentioned). It stands for "current directory" and the syntax is cd /path/to.

Hope this helps!


There are several text editors that come with terminal on OS X. VI, emacs, and nano are probably the most well known.

Try running man nano, man vi, and man emacs or just googling those three items as well as 'OS X Terminal Text Editor' to learn more about them.

They will give you the ability to create files, name files, save files, and edit existing files.


Using Vim editor:

`vi text.txt` would create a file.
You may edit it and save it use `[Esc]:wq`

Using cat command:

cat > file.txt <<EOF
>some text
>to save

Using redirection:

echo "Some text here." > file.txt
  • 1
    it even makes scripts more readable to have a bunch of echo commands, one per line of the txt file of echo "this is the next line in the file" >> file.txt – infixed Apr 22 '16 at 19:00
  • @infixed yes. But I like cat the most.. fastest one for small files. – Ani Menon Apr 22 '16 at 19:15

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.