0

Specifically mac, but for future reference for those that come across this post, this could be on any OS.

So am learning the command line, and one utility (perhaps the only one) which seems to be absent is being able to write to a file. I've done a search on google of course, as well as SU, though there doesn't even seem to be anything related to what am seeking, so now am here.

To be honest am quite surprised, I would of though that this would of been quite rudimentary.

2
  • It is definitely rudimentary. A bit more context might help--you seem to have one answer so far assuming you're looking for a command-line text editor, and another assuming you know how to use echo and might want to write to a file as part of a script or the like. What are you trying to ultimately use this for?
    – Kev
    May 3, 2015 at 20:16
  • Hello, am not using it for anything at the moment, though I have a strong feeling this will be a requirement for me as I advance in the world of computing. Right now am just seeking out as much knowledge as I can! May 4, 2015 at 13:09

3 Answers 3

2

This will append to the file: echo "some text" >> someFile.txt

This will overwrite to the file: echo "some text" > someFile.txt

This will append text from one file to another: cat someFile.txt >> someOtherFile.txt

This will overwrite text from one file to another: cat someFile.txt > someOtherFile.txt

1
  • Thank you Dean, this answer is most concise, it works right of the bat. May 4, 2015 at 13:14
1

On most OS's this is done with redirect commands, almost always these are > to over-write any existing file, or >> to append to a file.

Lookup pipes and redirects for more info.

Not a Mac expert but since MacOS is largely unix these days I imagine it will be the same.

1
  • I never heard of piping, though I looked into it because you mentioned it, although echo really serves the purpose much better. However, piping allows me to write things to a file which would be to say the least tedious to do so by hand, so thank you very much! May 4, 2015 at 13:17
0

Most *nix systems support a few CLI text editors, the most common of which is vi and vim. See here (and google for about a thousand others) for a tutorial on vi. http://www.howtogeek.com/102468/a-beginners-guide-to-editing-text-files-with-vi/

Another really common one, especially for programmers is emacs. Tutorial here: https://github.com/rnkn/fountain-mode/wiki/Absolute-Beginner%27s-Guide-%28OS-X%29

Personally, I prefer pico or nano because they don't use mode sequences, but I'm not certain that they are available on Macs by default.

CLI Text editors are kind of strange when you are used to GUI tools, so take your time in learning them, and focus on when to use each one. emacs (or even vi for that matter) is often overkill for editing simple configuration files.

2
  • Am afraid the knowledge you provided is above my current level of knowledge, but am sure this will soon be a valuable resources, thank you :) May 4, 2015 at 13:15
  • Keep in mind, my answer is for when you want to interactively edit a file (like you would with notepad), perhaps to configure your network settings or something like that. the other answers related to how you would write strings to a file, perhaps within a script. Not really sure which you needed. good luck. May 4, 2015 at 20:57

You must log in to answer this question.

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