Linux create file with path

7 Ways to Create a File in Linux Terminal

In this tutorial, I will show you how to create a file from a Linux terminal. There are many text editors like (vim, nano, vi) and many commands like (cat, echo, printf, touch) to create a file in the Linux operating system via command line. Here will explain the following linux tools.

1) Create a file with touch command

We will use touch command with any extension to create file, this command will create an empty file touch.txt in your current directory as an example below.

To see the file type command below.

2) Create a file with cat command

We will use cat command to create file, this command will create an empty file cat.txt in your current directory as an example below, but you must add text in the file.

Add the text below.

To save the file hit Ctrl + d , and to see the file type command below.

To open the file, we will use cat command to open it.

3) Create a file with echo command

We will use echo command to create file, this command will create a file echo.txt in your current directory as an example below, but you should add text in the line command.

To see the file,type command below.

To open the file, we will use cat command to open it.

4) Create a file with printf command

We will use printf command to create file, this command will create a file printf.txt in your current directory as an example below, but you should add text in the line command.

To see the file type command below.

To open the file, we will use cat command to open it.

5) Create a file with nano text editor

To create a file using nano text editor, first install it, after that type command below and the text editor will be opened to adding text.

Add the text below.

To save the file type Ctrl + x and type y , to see the file type command below.

To open the file, We will use nano command to open it.

6) Create a file with vi text editor

To create a file using vi text editor, type command below and the text editor will open the file, but you can’t add any text before converting it to insert mode by typing i character.

Add the text below.

To save the file and exit hit Esc after that :wq , To see the file type command below.

To open the file, we will use vi command to open it.

7) Create a file with vim text editor

To create a file using vim text editor, type command below and the text editor will open the file, but you can’t add any text before converting it to insert mode by typing i character.

Add the text below.

To save the file and exit hit Esc after that :wq , to see the file type command below.

To open the file, we will use vim command to open it.

Conclusion

In this tutorial, we learned the different ways to create a file from Linux terminal. Hope you enjoyed reading and please leave your comments in the below comment section.

Источник

How to Create a File in Linux Using Terminal/Command Line

Home » SysAdmin » How to Create a File in Linux Using Terminal/Command Line

Creating a new file in Linux is straightforward, but there are also some surprising and clever techniques.

In this tutorial learn how to to create a file from a Linux terminal.

  • Access to a command line/terminal window (CtrlAltF2 or CtrlAltT)
  • A user account with sudo privileges (optional for some files/directories)

Creating New Linux Files from Command Line

Linux is designed to create any file you specify, even if it doesn’t already exist. One smart feature is that you can create a file directly, without needing to open an application first.

Читайте также:  Как выключить защитник windows 10 домашняя навсегда

Here are a few commands for creating a file directly from the command line.

Create a File with Touch Command

The easiest way to create a new file in Linux is by using the touch command.

In a terminal window, enter the following:

This creates a new empty file named test.txt. You can see it by entering:

The ls command lists the contents of the current directory. Since no other directory was specified, the touch command created the file in the current directory.

If there’s already a file with the name you chose, the touch command will update the timestamp.

Create a New File With the Redirect Operator

A redirection operator is a name for a character that changes the destination where the results are displayed.

Right angle bracket >

This symbol tells the system to output results into whatever you specify next. The target is usually a filename. You can use this symbol by itself to create a new file:

This creates a new empty file.
Use the ls command to list the contents of the current directory and find the file test2.txt.

Create File with cat Command

The cat command is short for concatenate. It can be used to output the contents of several files, one file, or even part of a file. If the file doesn’t exist, the Linux cat command will create it.

To create an empty file using cat , enter the following:

Note the redirection operator. Typically, the command displays the contents of test2.txt on the screen. The redirection operator > tells the system to place it in the test2.txt file.

Verify that the file was created:

The system should now have test.txt, test2.txt, and test3.txt in the list.

Create File with echo Command

The echo command will duplicate whatever you specify in the command, and put the copy into a file.

Enter the following:

Verify that the file was created:

You should see the test4.txt file added to the list. Use the cat command to display the contents of the new file:

The system should display Random sample text (or whatever you entered with the echo command.)

Create File with printf Command

The printf command works like the echo command, and it adds some formatting functionality. To add a single line of text, enter:

To add two lines of text, separate each line with the \n option:

You can use the cat command on either of these files to display their contents.

Note: To use several terminal instances in a single window manager, consider using Linux screen. It enables additional features and an enhanced command line for working with Linux files.

Using Text Editors to Create a Linux File

All Linux distributions have at least one text editor. Some have multiple editors. Each editor has different strengths and features. This will show you three of the most popular.

Vi Text Editor

Vi is the oldest text editor in Linux. It was created alongside the Linux operating system for directly editing text files. Since it’s unlikely you’ll see a Linux distribution without it, it’s a safe editor to know.

To create a file using Vi, enter the following:

Your screen will change. Now you’re in the text editor. Press the letter i to switch to insert mode, then type a few words to try it out.

To save and exit press Esc 😡 and hit Enter .

Vim Text Editor

You may have noticed that the Vi editor wasn’t very user-friendly. Vim is a newer version, which stands for Vi editor, Modified.

Use vim to create a new text file:

This screen will look similar to the Vi editor screen. Press i to insert text, and type a few words. Save file and exit by entering:

(Escape, colon wq, then Enter.)

Nano Text Editor

Nano is a newer and much easier text editor to navigate.

Create a new file by entering the command:

By default, Nano puts you directly into editing mode. It also displays a helpful list of commands at the bottom of the screen.

Enter some text, then press Ctrl+O to save the changes.

Press Ctrl+X to exit the editor.

Note: Learn all you need about Nano in the Install and Use Nano in Linux article.

Now you have several options to create new files in Linux from the command line. Next, learn how to copy files and directories in Linux to manage your files more efficiently.

Источник

Unix — create path of folders and file

I know you can do mkdir to create a directory and touch to create a file, but is there no way to do both operations in one go?

i.e. if I want to do the below when the folder other does not exist:

Has anyone come up with a function as a workaround for this?

11 Answers 11

Use && to combine two commands in one shell line:

Note: Previously I recommended usage of ; to separate the two commands but as pointed out by @trysis it’s probably better to use && in most situations because in case COMMAND1 fails COMMAND2 won’t be executed either. (Otherwise this might lead to issues you might not have been expecting.)

Читайте также:  Windows boot time что это

You need to make all of the parent directories first.

If you want to get creative, you can make a function:

And then use it like any other command:

Do it with /usr/bin/install:

when you don’t have a source file:

This is what I would do:

mkdir -p /my/other/path/here && touch $_/cpredthing.txt

Here, the $_ is a variable that represents the last argument to the previous command that we executed in line.

As always if you want to see what the output might be, you can test it by using the echo command, like so:

echo mkdir -p /code/temp/other/path/here && echo touch $_/cpredthing.txt

Which outputs as:

As a bonus, you could write multiple files at once using brace expansion, for example:

Источник

How to create a file in Linux from terminal window? [closed]

Want to improve this question? Add details and clarify the problem by editing this post.

Closed last year .

What’s the easiest way to create a file in Linux terminal?

17 Answers 17

Depending on what you want the file to contain:

    touch /path/to/file for an empty file

somecommand > /path/to/file for a file containing the output of some command.

nano /path/to/file or vi /path/to/file (or any other editor emacs,gedit etc )
It either opens the existing one for editing or creates & opens the empty file to enter, if it doesn’t exist

Create the file using cat

Now, just type whatever you want in the file:

CTRL-D to save and exit

There are several possible solutions:

Create an empty file

The echo version will work only if your version of echo supports the -n switch to suppress newlines. This is a non-standard addition. The other examples will all work in a POSIX shell.

Create a file containing a newline and nothing else

This is a valid «text file» because it ends in a newline.

Write text into a file

These are equivalent. The $EDITOR command assumes that you have an interactive text editor defined in the EDITOR environment variable and that you interactively enter equivalent text. The cat version presumes a literal newline after the \ and after each other line. Other than that these will all work in a POSIX shell.

Of course there are many other methods of writing and creating files, too.

Источник

Linux: copy and create destination dir if it does not exist

I want a command (or probably an option to cp) that creates the destination directory if it does not exist.

23 Answers 23

(there’s no such option for cp ).

If both of the following are true:

  1. You are using the GNU version of cp (and not, for instance, the Mac version), and
  2. You are copying from some existing directory structure and you just need it recreated

then you can do this with the —parents flag of cp . From the info page (viewable at http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation or with info cp or man cp ):

Short Answer

To copy myfile.txt to /foo/bar/myfile.txt , use:

How does this work?

There’s a few components to this, so I’ll cover all the syntax step by step.

The mkdir utility, as specified in the POSIX standard, makes directories. The -p argument, per the docs, will cause mkdir to

Create any missing intermediate pathname components

meaning that when calling mkdir -p /foo/bar , mkdir will create /foo and /foo/bar if /foo doesn’t already exist. (Without -p , it will instead throw an error.

The && list operator, as documented in the POSIX standard (or the Bash manual if you prefer), has the effect that cp myfile.txt $_ only gets executed if mkdir -p /foo/bar executes successfully. This means the cp command won’t try to execute if mkdir fails for one of the many reasons it might fail.

Finally, the $_ we pass as the second argument to cp is a «special parameter» which can be handy for avoiding repeating long arguments (like file paths) without having to store them in a variable. Per the Bash manual, it:

expands to the last argument to the previous command

In this case, that’s the /foo/bar we passed to mkdir . So the cp command expands to cp myfile.txt /foo/bar , which copies myfile.txt into the newly created /foo/bar directory.

Note that $_ is not part of the POSIX standard, so theoretically a Unix variant might have a shell that doesn’t support this construct. However, I don’t know of any modern shells that don’t support $_ ; certainly Bash, Dash, and zsh all do.

A final note: the command I’ve given at the start of this answer assumes that your directory names don’t have spaces in. If you’re dealing with names with spaces, you’ll need to quote them so that the different words aren’t treated as different arguments to mkdir or cp . So your command would actually look like:

Читайте также:  Lenovo windows 10 drivers download

Such an old question, but maybe I can propose an alternative solution.

You can use the install programme to copy your file and create the destination path «on the fly».

There are some aspects to take in consideration, though:

  1. you need to specify also the destination file name, not only the destination path
  2. the destination file will be executable (at least, as far as I saw from my tests)

You can easily amend the #2 by adding the -m option to set permissions on the destination file (example: -m 664 will create the destination file with permissions rw-rw-r— , just like creating a new file with touch ).

Shell function that does what you want, calling it a «bury» copy because it digs a hole for the file to live in:

Here’s one way to do it:

dirname will give you the parent of the destination directory or file. mkdir -p `dirname . ` will then create that directory ensuring that when you call cp -r the correct base directory is in place.

The advantage of this over —parents is that it works for the case where the last element in the destination path is a filename.

And it’ll work on OS X.

install -D file -m 644 -t /path/to/copy/file/to/is/very/deep/there

with all my respect for answers above, I prefer to use rsync as follow:

Just to resume and give a complete working solution, in one line. Be careful if you want to rename your file, you should include a way to provide a clean dir path to mkdir. $fdst can be file or dir. Next code should work in any case.

or bash specific

This does it for me

Simply add the following in your .bashrc, tweak if you need. Works in Ubuntu.

E.g If you want to copy ‘test’ file to destination directory ‘d’ Use,

mkcp will first check if destination directory exists or not, if not then make it and copy source file/directory.

I wrote a support script for cp, called CP (note capital letters) that’s intended to do exactly this. Script will check for errors in the path you’ve put in (except the last one which is the destination) and if all is well, it will do an mkdir -p step to create the destination path before starting the copy. At this point the regular cp utility takes over and any switches you use with CP (like -r, -p, -rpL gets piped directly to cp). Before you use my script, there are a few things you need to understand.

  • all the info here can be accessed by doing CP —help. CP —help-all include’s cp’s switches.
  • regular cp won’t do the copy if it doesn’t find the destination path. You don’t have such a safety net for typos with CP. You’re destination will be created, so if you misspell your destination as /usrr/share/icons or /usr/share/icon well that’s what’s going to be created.
  • regular cp tends to model it’s behavior on the existing path: cp /a/b /c/d will vary on whether d exists or not. if d is an existing folder, cp will copy b into it, making /c/d/b. If d doesn’t exist, b will be copied into c and renamed to d. If d exists but is a file and b is a file, it will be overwritten by b’s copy. If c doesn’t exist, cp doesn’t do the copy and exits.

CP doesn’t have the luxury of taking cues from existing paths, so it has to have some very firm behavior patterns. CP assumes that the item you’re copying is being dropped in the destination path and is not the destination itself (aka, a renamed copy of the source file/folder). Meaning:

  • «CP /a/b /c/d» will result in /c/d/b if d is a folder
  • «CP /a/b /c/b» will result in /c/b/b if b in /c/b is a folder.
  • If both b and d are files: CP /a/b /c/d will result in /c/d (where d is a copy of b). Same for CP /a/b /c/b in the same circumstance.

This default CP behavior can be changed with the «—rename» switch. In this case, it’s assumed that

  • «CP —rename /a/b /c/d» is copying b into /c and renaming the copy to d.

A few closing notes: Like with cp, CP can copy multiple items at a time with the last path being listed assumed to be the destination. It can also handle paths with spaces as long as you use quotation marks.

CP will check the paths you put in and make sure they exist before doing the copy. In strict mode (available through —strict switch), all files/folders being copied must exist or no copy takes place. In relaxed mode (—relaxed), copy will continue if at least one of the items you listed exists. Relaxed mode is the default, you can change the mode temporarily via the switches or permanently by setting the variable easy_going at the beginning of the script.

Источник

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