Linux create file with folder

Create file in subdirectories that doesn’t exist (../new_folder/new_folder/new_file.ext)

Please do not close this question; it’s not a made up problem — otherwise the /usr/bin/install utility wouldn’t have been invented, 30 or 35 years ago.

3 Answers 3

install -D src_file /tmp/a/b/c/d/e/f/g/h/i/dst_file install -Dt /tmp/a/b/c/d/e/f/g/h/i your_file 

If you want just to touch an empty file:

install -D /dev/null /tmp/a/b/c/d/e/f/g/h/i/empty_file 

Look at the install(1) manpage; it has options for setting permissions, preserving timestamps, etc.

In terms of tools used: no. touch will fail (rightly) if you are trying to operate in a directory that does not exist, and mkdir does precisely one thing: create directories, not normal files. Two different jobs mandate two different tools.

That said, if you’re talking about efficiency in terms of the number of lines in a script, or the readability of one, you could put it into a function:

seedfile() < mkdir -p "$(dirname "$1")" touch "$1" >seedfile /path/to/location/one/file.txt seedfile /path/to/somewhere/else/file.txt seedfile local/paths/work/too/file.txt 

Try this: mkdir -p a/b/c/d/e && touch $_/file.abc Always work for me.

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to Create a File in a Directory in Linux

This article was co-authored by wikiHow staff writer, Travis Boylls. Travis Boylls is a Technology Writer and Editor for wikiHow. Travis has experience writing technology-related articles, providing software customer service, and in graphic design. He specializes in Windows, macOS, Android, iOS, and Linux platforms. He studied graphic design at Pikes Peak Community College.

This article has been viewed 63,242 times.

This wikiHow article teaches you how to create a file in a directory in Linux. There are many ways you can create files using the Terminal in Linux. You can create simple text files using short Terminal commands, or you can use one of Linux’s built-in text editors to create complex documents.

Using the «Touch» Command

Image titled Create a File in a Directory in Linux Step 1

Press Ctrl + Alt + T to open the Terminal. The Terminal is a command-line interface that is available on most Linux distributions. It typically has an icon that resembles a black screen with a white text cursor. You can either click the Terminal icon in the Apps menu, or press Ctrl + Alt + T at the same time to open the Terminal.

Читайте также:  Linux lamp установка настройка

Image titled Create a File in a Directory in Linux Step 2

  • Alternatively, you can type cd / to navigate to the Root directory, or type cd ~ to navigate to your Home/User directory. [1] X Research source
  • You can also type cd ~ followed by the path relative to your Home/User folder to navigate to a directory. For example, type cd ~/Documents and press enter to navigate to «/home/user/Documents» directory.
  • Type pwd and press Enter to see what directory you are currently in.
  • Type mkdir followed by the directory name to create a new directory in the directory you are currently in.

Image titled Create a File in a Directory in Linux Step 3

Type touch . The «touch» command is used to create an empty file in Linux. This is the easiest way to create a file in Linux.

Image titled Create a File in a Directory in Linux Step 4

  • Add multiple filenames after the «touch» command to create multiple files (i.e. «touch file_1 file_2 file_3»).
  • Alternatively, you can create a file in any directory you want by adding the directory path after the «touch» command (i.e «touch /home/username/Documents/blank_file.txt»)
  • You can create a file in multiple directories by adding the directory names after the touch command. For example touch category/file1.txt subcategory/blank_file.txt or touch /blank_file.txt . [2] X Research source
  • If the file you are trying to create already exists in the directory, the time stamp for that file will be updated instead.

Image titled Create a File in a Directory in Linux Step 5

Type ls and press ↵ Enter . This displays a list of all files in your current directory and allows you to verify that the file you just created is present.

Using the «Cat» Command

Image titled Create a File in a Directory in Linux Step 6

Press Ctrl + Alt + T to open the Terminal. The Terminal is a command-line interface that is available on most Linux distributions. It typically has an icon that resembles a black screen with a white text cursor. You can either click the Terminal icon in the Apps menu, or press Ctrl + Alt + T at the same time to open the Terminal.

Image titled Create a File in a Directory in Linux Step 7

  • Alternatively, you can type cd / to navigate to the Root directory, or type cd ~ to navigate to your Home/User directory. [3] X Research source
  • You can also type cd ~ followed by the path relative to your Home/User folder to navigate to a directory. For example, type cd ~/Documents and press enter to navigate to «/home/user/Documents» directory.
  • Type pwd and press Enter to see what directory you are currently in.
  • Type mkdir followed by the directory name to create a new directory in the directory you are currently in.

Image titled Create a File in a Directory in Linux Step 8

  • Alternatively, you can use the command cat >> to append an existing file. If no file exists with the specified file name, a new file will be created. If a file by that name already exists, then the text you add will be appended to the end of the file instead of replacing the existing text. [4] X Research source

Image titled Create a File in a Directory in Linux Step 9

Type the text you want to go in the file. After creating a file using the «cat» command, the Terminal enters write mode allowing you to add text to the file. Type the text you want the file to contain on the next line.

Image titled Create a File in a Directory in Linux Step 10

  • To view the text in the file, simply type «cat» (without the «>» symbol) followed by the file name and press Enter (i.e. cat README.txt ). This displays the text in the Terminal.
Читайте также:  Linux узнать расположение python

Image titled Create a File in a Directory in Linux Step 11

Type ls and press ↵ Enter . This displays a list of all files in your current directory and allows you to verify that the file you just created is present.

Using the Nano Text Editor

Image titled Create a File in a Directory in Linux Step 12

Press Ctrl + Alt + T to open the Terminal. The Terminal is a command-line interface that is available on most Linux distributions. It typically has an icon that resembles a black screen with a white text cursor. You can either click the Terminal icon in the Apps menu, or press Ctrl + Alt + T at the same time to open the Terminal.

Image titled Create a File in a Directory in Linux Step 13

  • Alternatively, you can type cd / to navigate to the Root directory, or type cd ~ to navigate to your Home/User directory. [5] X Research source
  • You can also type cd ~ followed by the path relative to your Home/User folder to navigate to a directory. For example, type cd ~/Documents and press enter to navigate to «/home/user/Documents» directory.
  • Type pwd and press Enter to see what directory you are currently in.
  • Type mkdir followed by the directory name to create a new directory in the directory you are currently in.

Image titled Create a File in a Directory in Linux Step 14

  • Nano may not be available on all Linux distributions. To install Nano, type sudo apt install nano and press Enter to install Nano on Ubuntu/Debian. Type sudo yum install nano and press Enter to install Nano on CentOS/Fedora.
  • You can also specify which directory you want to create the file in by entering the path of the directory after the «vi» command (i.e nano /home/username/Documents/website/main.php ).

Image titled Create a File in a Directory in Linux Step 15

Type the text you want your file to contain. As soon as Nano opens, you can begin entering your text.

Image titled Create a File in a Directory in Linux Step 16

Press Ctrl + X . Use this keyboard shortcut to save and exit Nano when you are done editing your text.

Image titled Create a File in a Directory in Linux Step 17

Press y followed by ↵ Enter . This confirms that you want to save your file and exits Nano. You will be returned to the standard Terminal command-line.

Using the «Echo» Command

Image titled Create a File in a Directory in Linux Step 18

Press Ctrl + Alt + T to open the Terminal. The Terminal is a command-line interface that is available on most Linux distributions. It typically has an icon that resembles a black screen with a white text cursor. You can either click the Terminal icon in the Apps menu, or press Ctrl + Alt + T at the same time to open the Terminal.

Image titled Create a File in a Directory in Linux Step 19

  • Alternatively, you can type cd / to navigate to the Root directory, or type cd ~ to navigate to your Home/User directory. [6] X Research source
  • You can also type cd ~ followed by the path relative to your Home/User folder to navigate to a directory. For example, type cd ~/Documents and press enter to navigate to «/home/user/Documents» directory.
  • Type pwd and press Enter to see what directory you are currently in.
  • Type mkdir followed by the directory name to create a new directory in the directory you are currently in.

Image titled Create a File in a Directory in Linux Step 20

Type echo followed by some text in quotations. The text you enter in quotations will be saved to the file. Use the «echo» command to create files with short lines of text.

Читайте также:  Linux синхронизировать время консоль

Image titled Create a File in a Directory in Linux Step 21

  • Alternatively, you can add >> at the end of the text. This will create a new file if no file by the file name specified exists. If a file by that name already exists, the text will be appended to the end of the existing file text.

Image titled Create a File in a Directory in Linux Step 22

Type ls and press ↵ Enter . This displays a list of all files in your current directory and allows you to verify that the file you just created is present.

Using the VI and VIM Text Editor

Image titled Create a File in a Directory in Linux Step 23

Press Ctrl + Alt + T to open the Terminal. The Terminal is a command-line interface that is available on most Linux distributions. It typically has an icon that resembles a black screen with a white text cursor. You can either click the Terminal icon in the Apps menu, or press Ctrl + Alt + T at the same time to open the Terminal.

Image titled Create a File in a Directory in Linux Step 24

  • Alternatively, you can type cd / to navigate to the Root directory, or type cd ~ to navigate to your Home/User directory. [7] X Research source
  • You can also type cd ~ followed by the path relative to your Home/User folder to navigate to a directory. For example, type cd ~/Documents and press enter to navigate to «/home/user/Documents» directory.
  • Type pwd and press Enter to see what directory you are currently in.
  • Type mkdir followed by the directory name to create a new directory in the directory you are currently in.

Image titled Create a File in a Directory in Linux Step 25

  • Alternatively, you can use the vim in place of the <[kbd|vi>> command. This opens the VIM text editor, which is a newer and more user-friendly version of VI. VIM may not be available on all Linux distributions. If VIM is not installed on your Linux distribution, type sudo apt install vim and press Enter to install VIM on Ubuntu/Debian. Type sudo yum install vim and press Enter to install VIM on CentOS/Fedora. .
  • You can also specify which directory you want to create the file in by entering the path of the directory after the «vi» command (i.e., «vi /home/username/Documents/Website/index.html»).

Image titled Create a File in a Directory in Linux Step 26

Press i to switch to Insert mode. VI has two modes, Command mode and Insert mode. Command mode allows you to perform various tasks using the keyboard keys. Insert mode allows you to enter text. Press I to switch to Insert mode.

Image titled Create a File in a Directory in Linux Step 27

Type the text you want to add. Now that you are in «Insert» mode, you can type the text the way you would using any other text editor. Enter the text you want the file to contain.

Image titled Create a File in a Directory in Linux Step 28

Image titled Create a File in a Directory in Linux Step 29

Type the command to save and exit VI. To do so, type :wq and press Enter. This saves the file and returns to the standard Terminal command-line.

Expert Q&A

Tips

You Might Also Like

Install Google Chrome Using Terminal on Linux

Can Linux Run Exe

Can Linux Run .exe Files? How to Run Windows Software on Linux

Add or Change the Default Gateway in Linux

Open Ports in Linux Server Firewall

How to Open Linux Firewall Ports: Ubuntu, Debian, & More

Become Root in Linux

Take a Screenshot in Linux

Execute INSTALL.sh Files in Linux Using Terminal

How to Run an INSTALL.sh Script on Linux in 4 Easy Steps

Ping in Linux

Use Ping in Linux: Tutorial, Examples, & Interpreting Results

Boot Linux from a USB on Windows 10

Delete Read Only Files in Linux

How to Delete Read-Only Files in Linux

Use Wine on Linux

Run Files in Linux

Install Linux

How to Install Linux on Your Computer

Install Puppy Linux

How to Install Puppy Linux

Источник

Оцените статью
Adblock
detector