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.

Method 1
Method 1 of 3:

Saving a Text File in Vi or Vim

  1. 1
    Open your file in Vi or Vim. 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. 2
    Press i on the keyboard. This puts you into Insert mode, which is how you can type into the file.
    Advertisement
  3. 3
    Edit your file. Make any changes you need while in insert mode.
  4. 4
    Press Esc to return to Command mode. 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. 5
    Type :w filename and press Enter. 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. 6
    Type :q and press Enter to quit. 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

  1. 1
    Type your command at the command prompt. 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. 2
    Type a space and then >. 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. 3
    Type a space and enter the name of the file you want to create. 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. 4
    Press Enter to run the command. 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

  1. 1
    Use the cd command to enter the directory of the file you want to copy. 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. 2
    Type cp filename newfilename and press Enter. 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

About This Article

Nicole Levine, MFA
Written by:
wikiHow Technology Writer
This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been viewed 21,210 times.
How helpful is this?
Co-authors: 3
Updated: February 10, 2021
Views: 21,210
Categories: Linux
Article SummaryX

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?
Advertisement