PDF download Download Article PDF download Download Article

This wikiHow teaches you how to save different types of files from the Linux command line. If you're using an app that has a graphical user interface (GUI), saving files is easy—you'll usually just need to click the File menu and select Save . Read on to learn how to save files in command line text editors, how to save the output of a command, and how to save an existing file to a new file.

How to Save Files from the Linux Command Line

  1. Open or create your file in Vi or Vim, depending on which one you have.
  2. Press i on your keyboard to enter insert mode.
  3. Make any changes to your file while you're in insert mode.
  4. Press Esc to exit insert mode.
  5. Type :w [filename] ; you can omit the filename if you're editing an existing file.
  6. Press Enter to save the file.
  7. Type :q and press Enter to quit Vi or Vim and return to the command line.
Method 1
Method 1 of 3:

Saving a Text File in Vi or Vim

PDF download Download Article
  1. If you want to edit an existing text file, just type vi filename at the prompt and press Enter . To create a new file, just type vi and press Enter . Vi and Vim both automatically open up in Command mode.
    • If you're using Vim, replace vi with vim .
    • The text editors Vi and Vim have most of the same functions, although vim is a bit more verbose and includes color highlighting.
  2. This puts you into Insert mode, which is how you can type into the file.
    Advertisement
  3. Make any changes you need while in insert mode.
  4. Now you'll be able to use Vi or Vim commands, including the command to save.
    • You can use this key to switch back and forth between Command and Input modes.
  5. You can skip entering the file name if you're editing an existing file and want to save your changes to that same file.
    • For example, if you're editing a file that already has a file name and want to save the changes you've made, type :w and press Enter . But if you're editing a brand new file and want to call it wikiHow , you'd use :w wikiHow instead.
  6. This exists Vi (or Vim) and returns you to the command line.
  7. Advertisement
Method 2
Method 2 of 3:

Saving the Output of a Command

PDF download Download Article
  1. Don't press Enter to run it just yet—simply type the command first.
    • For example, if you want to list the contents of the current directory and save the output to a new file, you could type ls -a now.
  2. Using the prior example, your command would now look like this: ls -a > .
    • If you want to append the output to an existing file, use >> instead of > .
  3. If you wanted to save the results to a file called filelist , the command would look like ls -a > filelist .
    • If you're appending the output to an existing file, you'd use ls -a >> filename .
  4. This creates a file in the current directory called filelist that contains the output of the ls -a command.
  5. Advertisement
Method 3
Method 3 of 3:

Copying a File to a New File

PDF download Download Article
  1. For example, if you want to copy a file from /home/wikiHow/personal to a new file, you'd type cd /home/wikiHow/personal and press Enter .
  2. This saves Just make sure a file with the new name doesn't already exist, as it will be automatically overwritten, if so.
    • For example, if the file you want to copy is called Staff.txt and you want to save it as a new file called Staff-old.txt , you'd type cp Staff.txt Staff-old.txt and press Enter .
    • If you want to keep the file name but save the file to a new folder (for example, /home/wikHow/backups ), you'd use cp Staff.txt /home/wikiHow/backups .
    • If you want to copy the file to another folder and give it a new name, you'd use cp Staff.txt /home/wikiHow/backups/Staff-old.txt .
  3. Advertisement

Expert Q&A

Ask a Question
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!

      About This Article

      Article Summary X

      1. Type "vi" and press Enter .
      2. Type i to enter Insert mode.
      3. Edit your file.
      4. Press Esc .
      5. Type ":w" and press Enter .

      Did this summary help you?
      Thanks to all authors for creating a page that has been read 59,752 times.

      Is this article up to date?

      Advertisement