Creating bash file linux

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

  • touch /path/to/file for an empty file
  • somecommand > /path/to/file for a file containing the output of some command.
 eg: grep --help > randomtext.txt echo "This is some text" > randomtext.txt 

UNIX is not a command line environment, but a family of (very different) OSes. That said: Yes, this should work on most Unices

touch will work in UNIX, because it’s a standard tool. The somecommand example will work because it uses standard syntax. The nano sample may not work because an editor named nano may not be installed (nano is not standardized). The standard editor is ed and could be used in place of nano , or you could use $EDITOR to use your user- or system-configured default text editor, if there is one.

Additionally, you could simply say >/path/to/file to create an empty file, even if you don’t have touch .

Create the file using cat

Now, just type whatever you want in the file:

When I tried cat /etc/systemd/system/sample.service , it said «no such file or directory» rather than creating a new sample.service file.

@TylerH cat /etc/systemd/system/sample.service prints the file to the console cat > /etc/systemd/system/sample.service redirects standard input to the file (which is why you need to close standard input by pressing control-d.

There are several possible solutions:

Create an empty file

touch file >file echo -n > file printf '' > 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

echo '' > file printf '\n' > file 

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

Write text into a file

"$EDITOR" file echo 'text' > file cat > file 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.

Источник

How to Create a New File in Linux from Bash?

Before getting into the ways of creating a file using Bash, let’s first understand how Linux treats its files. Linux organizes all its data into files and files are organized into directories. Further, the directories are organized into tree-like structures called the filesystem. When you have to work in a Linux environment, you would definitely have to spend a lot of your time working on different types of files.

There are various ways in which one can create a file in Linux. You can create a file from the Bash Shell or you can use the Desktop File Manager to do so. In this article, we will focus on different Shell commands that you can use to create a file.

Using the «touch» command

The touch command is by far the most frequently used command for creating a new file in Linux. To create a new file, you need to run the touch command followed by the name of the file. For example,

It will create an empty file called «hello.txt» in the current directory. Use the «ls» command to verify if the file has been created or not.

Using the «cat» command

Normally we use the «cat» command to read the contents of a file; however, we can also use this command to create a new file. Let’s see how.

To create a new file, run the «cat» command and then use the redirection operator «>» followed by the name of the file. Now you will be prompted to insert data into this newly created file. Type a line and then press «Ctrl+D» to save the file.

$ cat > secondFile.txt Welcome to Tutorialspoint!

The above command will create a new file called «secondFile.txt» and save it with the content «Welcome to Tutorialspoint».

Again, use the «ls» command to verify if the new file has been created or not.

$ ls hello.txt newdirectory secondFile.txt

Next, use the «cat» command to see the contents of «secondFile.txt».

$ cat secondFile.txt Welcome to Tutorialspoint!

Using the Redirection Operator

You can simply use the redirection operator «>» to create a new blank file in the current directory. Run the «>» operator followed by the name of the file.

Now use the «ls» command again to verify −

$ ls hello.txt newdirectory secondFile.txt thirdFile.txt

Note that the «>» operator overwrites the contents of a file if it is already present. For example, the following command will overwrite the contents of «secondFile.txt» because the file already exists and we know it contains the line «Welcome to Tutorialspoint!»

Now use the «cat» command to check the contents of «secondFile.txt».

It will display nothing because the file is now empty.

You can use the redirection operator «>>» to append the contents of a file into another. For example,

$ cat hello.txt This is the first file. $ cat secondFile.txt This is the Second File.

Now we can use the following command to append the contents of «secondFile.txt» at the end of «hello.txt».

$ cat secondFile.txt >> hello.txt $ cat hello.txt This is the first file. This is the Second File.

Using the «echo» command

The «echo» command takes a string as argument and displays it as output. For example,

$ echo "This is the Fourth File" This is the Fourth File

We can redirect this output to a new file, such as −

$ echo "This is the Fourth File" > fourthFile.txt

The above command will create a new file (or overwrite the file if it already exists) with the string passed as the argument to «echo». Verify using the «cat» command −

$ cat fourthFile.txt This is the Fourth File

If you simply want to create a blank new file, use the «echo» command without any argument −

Using the «printf» command

The «printf» command works just like the «echo» command with the only exception that the «printf» command provides additional formatting options that you can use to pass a formatted string as the argument.

The following «printf» command redirects the input formatted string into a new file «fifthFile.txt». If the file already exists, then it will overwrite its contents.

$ printf "First Line.
Second Line.
" > fifthFile.txt $ cat fifthFile.txt First Line. Second Line.

Источник

How do I create a script file for terminal commands?

In Windows I can write a file containing commands for cmd (usually .cmd or .bat files). When I click on those files it will open cmd.exe and run them commands the file contains. How would I do this in Ubuntu? I’m sure this is a duplicate, but I can’t find my answer.
Its similar to these questions, but they don’t answer the question: Store frequently used terminal commands in a file CMD.exe Emulator in Ubuntu to run .cmd/.bat file

3 Answers 3

First, the most common is to write a file, make sure the first line is

Then save the file. Next mark it executable using chmod +x file

Then when you click (or run the file from the terminal) the commands will be executed. By convention these files usually have no extension, however you can make them end in .sh or any other way.

  • Any (and I mean any) file can be executed in Linux provided the first line is a path to the program that should interpret the file. Common examples include /bin/python , /bin/sh , /bin/dash , but even odd ball things work like /bin/mysql
  • Bash is a full language. It is vastly more complex than cmd.exe in windows. It has a strong programming language that supports functions, loops, conditionals, string operations, etc.
  • These documents may help if you run into problems.
  • If you do not wish to make the file executable then you can run it by passing it as an argument to bash: bash file/to/run.sh

A Simple Bash Example

#!/bin/bash echo "This is a shell script" ls -lah echo "I am done running ls" SOMEVAR='text stuff' echo "$SOMEVAR" 

The second method is to record commands using script . Run script then just do stuff. When you are done doing stuff type exit and script will generate a file for you with all the «stuff» you did. This is less used but works quite well for making things like macros. man script for more info.

Источник

4 Ways to Create a Text File in Linux Terminal

In this Linux beginner series, you’ll learn various methods to create a file in Linux terminal.

In this Linux beginner series, you’ll learn various methods to create a text file in Linux terminal.

If you have used the desktop oriented operating system such as Windows, creating file is a piece of cake. You right click in the file explorer and you would find the option of creating new file.

Things won’t look the same when you are in a command line environment. There is no right click option here. So how do you create a file in Linux then? Let me show you that.

Create file in Linux command line

There are various ways of creating a new file in Linux terminal. I’ll show you the commands one by one. I am using Ubuntu here but creating files in Ubuntu terminal is the same as any other Linux distribution.

1. Create an empty file using touch command

One of the biggest usages of the touch command in Linux is to create a new empty file. The syntax is super simple.

If the file doesn’t exist already, it will create a new empty file. If a file with the same name exists already, it will update the timestamps of the file.

2. Create files using cat command

Another popular way of creating new file is by using the cat command in Linux. The cat command is mostly used for viewing the content of a file but you can use it to create new file as well.

You can write some new text at this time if you want but that’s not necessary. To save and exit, use Ctrl+D terminal shortcut.

If the file with that name already exists and you write new text in it using the cat command, the new lines will be appended at the end of the file.

3. Create new file using echo command

The main use of the echo command is to simply repeat (echo) what you type on the screen. But if you use the redirection with echo, you can create a new file.

To create a new file using echo you can use something like this:

echo "This is a sample text" > filename.txt

The newly created filename.txt file will have the following text: This is a sample text. You can view the file in Linux using cat or other viewing commands.

You are not obliged to put a sample text with echo. You can create an (almost) empty file using the echo command like this:

This will create a new file with just one empty line. You can check the number of lines with wc command.

4. Create a new file using a text editor like Nano or Vim

The last method in this series is the use of a text editor. A terminal-based text editor such as Emacs, Vim or Nano can surely be used for creating a new file in Linux.

Before you use these text editors, you should make sure that you know the basics such as saving an existing from the editor. Unlike the GUI tools, using Ctrl+S in the terminal won’t save the file. It could, in fact, send your terminal into a seemingly frozen state from which you recover using Ctrl+Q.

Let’s say you are going to use Vim editor. Make sure that you are aware of the basic vim commands, and then open a new file with it like this:

What’s your favorite command?

So, I just shared 4 different ways of creating a file in Linux. Personally, I prefer using touch for creating empty file and Vim if I have to edit the file. On a related note, you may want to learn about the file command in Linux that is helpful in determining the actual type of the file.

Which command do you prefer here? Please share your views in the comment section below.

Источник

Читайте также:  Linux scan port nmap
Оцените статью
Adblock
detector