Download Article
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
- Open or create your file in Vi or Vim, depending on which one you have.
- Press i on your keyboard to enter insert mode.
- Make any changes to your file while you're in insert mode.
- Press Esc to exit insert mode.
- Type :w [filename] ; you can omit the filename if you're editing an existing file.
- Press Enter to save the file.
- Type :q and press Enter to quit Vi or Vim and return to the command line.
Steps
-
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.
-
Press i on the keyboard. This puts you into Insert mode, which is how you can type into the file.Advertisement
-
Edit your file. Make any changes you need while in insert mode.
-
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.
-
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.
-
Type :q and press ↵ Enter to quit. This exists Vi (or Vim) and returns you to the command line.
Advertisement
-
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.
-
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 > .
-
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 .
-
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.
Advertisement
-
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 .
-
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 .
Advertisement
Expert Q&A
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
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.
Advertisement