3

I'm trying to save a file in the nano text editor. I wrote a short test text and entered ctrl+O to save and name the file but I'm not sure how I can save file to a specific folder? and how I can later find the file I created. thanks!

  • Hi kitty. What is the file you're trying to save? Most people only use nano when they need to make small edits to some already existing file (for example a configuration file or other system file), simply because there are lots of limitations to what nano can do. By default, nano saves the file you're editing into the directory where the file lives. – Hee Jin May 4 '18 at 16:51
  • If you used nano to create a new file, it will be saved into whatever your current working directory was when you opened nano (this is displayed to the right of the semicolon after your username in Terminal/other CLI). You can cd into the directory where you want to save before running nano. Or, unless you have some specific reason you want to use nano, just use a GUI text editor instead. All versions and flavors of Ubuntu come with a standard text editor with a GUI component, e.g. for Ubuntu 16.04 it's gedit; for Lubuntu it's Leafpad. – Hee Jin May 4 '18 at 16:54
  • Alternatively, you could use vim, another console text editor--you don't need to cd into your desired directory to save there before running vi; you can specify where you want the file to go upon saving--see this question – Hee Jin May 4 '18 at 16:55
2

Nano by default saves to the current working directory (CWD) or to the relative path from your CWD to the file that is open. If you want to save to another directory you just prepend the filename with the path

For example

Ctrl+O opens the save prompt

File Name to Write: ~/test.txt

Will save test.txt to my home directory

File Name to Write: ../test.txt

Will save test.txt to one directory above my CWD

File Name to Write: /absolute/path/to/file/text.txt

Will save text.txt in /absolute/path/to/file/ directory

After you save a file nano will be using that path in the buffer. To see that path Ctrl+O opens the save prompt with the new relative path Ctrl+C to cancel saving

While in the save prompt Ctrl+T opens a handy file browser you can use

1

If the specific folder already exists, there are several ways to do this. First, let’s assume that the folder is in your own user directory. We’ll tell nano that’s where we want to create the test file. From the terminal:

nano /home/<your_user_name>/<some_folder>/test.txt

Or, the more convenient:

nano ~/<some_folder>/test.txt

You can also first navigate to the location you want the file to reside:

cd ~/<some_folder>
nano test.txt

If the folder doesn’t yet exist, create it:

mkdir ~/<some_folder>
cd ~/<some_folder>
nano test.txt
  • 3
    I think the OP is asking about setting the path interactively at the Ctrl-O save prompt – steeldriver May 4 '18 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.