Saving file in nano to specific folder

13,033

Solution 1

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

Solution 2

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
Share:
13,033

Related videos on Youtube

kitty
Author by

kitty

Updated on September 18, 2022

Comments

  • kitty
    kitty about 1 year

    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!

    • Hee Jin
      Hee Jin over 5 years
      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
      Hee Jin over 5 years
      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
      Hee Jin over 5 years
      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
  • steeldriver
    steeldriver over 5 years
    I think the OP is asking about setting the path interactively at the Ctrl-O save prompt