How to Save Linux Files
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.
The wikiHow Tech Team also followed the article’s instructions and verified that they work.
This article has been viewed 27,073 times.
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.
Saving a Text File in Vi or Vim
- 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.
- 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.
Saving the Output of a Command
- 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.
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.
Copying a File to a New File
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.
- 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 .
Expert Q&A
Tips
You Might Also Like
Can Linux Run .exe Files? How to Run Windows Software on Linux
How to Open Linux Firewall Ports: Ubuntu, Debian, & More
How to Run an INSTALL.sh Script on Linux in 4 Easy Steps
Use Ping in Linux: Tutorial, Examples, & Interpreting Results
How to Delete Read-Only Files in Linux
How to Install Linux on Your Computer
How to Install Software on Linux: Packages, Compiling, & More
Is there a way to save a command in Ubuntu terminal if I haven’t entered it?
I want to be able to save the command for later use in an easy way that’s not just putting a # before it and putting it in the up and down key history. Optimally saving it directly to a register or the clipboard.
I forgot to mention that I didn’t want to echo, either.
With bash in Ubuntu, I can hit Meta+Enter (=Alt+Enter) which places the # in front of the command and give a newline in one go, without moving the cursor, just a half second faster than POS1 (HOME)-key for movement. Of course, you need to try it out with some simple, harmless commands.
@userunknown The standard shortcut is alt + # which is referred to as the ‘insert-comment’ command in the Bash docs (their notation is M-#). gnu.org/software/bash/manual/bash.html#Command-Line-Editing
10 Answers 10
This is not your terminal, this is your shell.
The name for the shell mechanism that you are looking for is a kill buffer. People forget that shell command line editors have these. ZLE in the Z shell has them, as have GNU Readline in the Bourne Again shell, libedit in the (FreeBSD) Almquist shell, the Korn shell’s line editor, and the TENEX C shell’s line editor.
In all of these shells in emacs mode, simply go to the end of the line to be saved, kill it to the head kill buffer with ⎈ Control + U , type and run the intermediate command, and then yank the kill buffer contents with ⎈ Control + Y . Ensure that you do not do anything with the kill buffer when entering the intermediate command.
In the Z shell in vi mode, you have the vi prefix sequences for specifying a named vi -style buffer to kill the line into. You can use one of the other buffers instead of the default buffer. Simply use something like » a d d (in vicmd mode) to delete the whole line into buffer «a», type and run the intermediate command, and then put that buffer’s contents with » a p .
In their vi modes, the Korn shell, GNU Readline in the Bourne Again shell, and libedit in the (FreeBSD) Almquist shell do not have named vi -style buffers, only the one cut buffer. d d to delete the line into that buffer, followed by putting the buffer contents with p , will work. But it uses the same vi -style buffer that killing and yanking will while entering the intermediate command.
How can I save the last command to a file?
When I am running my analyses using the bash shell, I often want to save the commands I’ve used that gave me good results to a file in the same directory (my «LOGBOOK», as its called) so that I can check what I did to get those results. So far this has meant me either copy.pasting the command from the terminal or pressing «up» modifying the command to an echo»my command» >> LOGBOOK , or other similar antics. I found there was a history tool the other day, but I can’t find a way of using it to get the previously executed command so that I can do something like getlast >> LOGBOOK . Is there a nice easy way to do this. Alternatively, how do others deal with saving the commands for results they have?
5 Answers 5
If you are using bash , you can use the fc command to display your history in the way you want:
That will print out your last command. -l means list, -n means not to prefix lines with command numbers and -1 says to show just the last command. If the whitespace at the start of the line (only the first line on multi-line commands) is bothersome, you can get rid of that easily enough with sed . Make that into a shell function, and you have a solution as requested ( getlast >> LOGBOOK ):
That should function as you have asked in your question.
I have added a slight variation by adding «$1» «$1» to the fc command. This will allow you to say, for example, getlast mycommand to print out the last command line invoking mycommand , so if you forgot to save before running another command, you can still easily save the last instance of a command. If you do not pass an argument to getlast (i.e. invoke fc as fc -ln «» «» , it prints out just the last command only).
[Note: Answer edited to account for @Bram’s comment and the issue mentioned in @glenn jackman’s answer.]