LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Best way to create a file in Bash? (https://www.linuxquestions.org/questions/linux-newbie-8/best-way-to-create-a-file-in-bash-4175629218/)

saderror256 05-07-2018 06:01 PM

Best way to create a file in Bash?
 
What the title says, what is the best and recommended way to create a file in the terminal?

hydrurga 05-07-2018 06:12 PM

I don't know about best or recommended, but if you just want to create a file with no content, you can use:

touch filename

keefaz 05-07-2018 06:15 PM

I don't think there is a best way to do it, maybe you mean creative way?
Also create empty file or file with specified size?

BW-userx 05-07-2018 06:35 PM

create file then add to it.
Code:

$ cat > hello
how are you to day?
I am fine, how are you?
not really wanting to tell you

ctrl+d

$ cat hello
how are you to day?
I am fine, how are you?
not really wanting to tell you

or create a bash script in the terminal
Code:

$ cat > readMySelf
#!/bin/bash
while read line
do
echo $line
done < readMySelf

ctrl+d

$ chmod +x readMySelf

$ ./readMySelf
#!/bin/bash
while read line
do
echo $line
done < readMySelf


hydrurga 05-07-2018 06:42 PM

Quote:

Originally Posted by BW-userx (Post 5851846)
$ cat hello
how are you to day?
I am fine, how are you?
not really wanting to tell you

:D I look forward to the sequel.

wpeckham 05-07-2018 06:47 PM

Best? There IS no "Best" under Linux. As in the Perl world, there is always more than one way to do "it" for almost any given "it" that can be done.

What is your purpose?

AwesomeMachine 05-07-2018 07:48 PM

Less is the best way, except it won't create files.

chrism01 05-07-2018 08:17 PM

As per the above comments, we really need more info.
Exactly what kind of file do you want and why?

FlinchX 05-10-2018 01:28 AM

I usually do

Code:

$ > myfilename
To create an empty file. The dollar sign is the bash prompt here, the command is "> myfilename".

This is pretty much the same as "touch filename" that was suggested above, just less typing.

But files I have to create by hand are usually text files, humans don't deal often with binary files. So I rather write some content in the text editor, then save it into a file.

rtmistler 05-10-2018 06:29 AM

Quote:

Originally Posted by saderror256 (Post 5851832)
What the title says, what is the best and recommended way to create a file in the terminal?

What the link says, what makes a good issue description.

LQ members are happy to help. Meanwhile you should consider asking a question where you've done some basic research.

You should update your fellow members to indicate if you feel their suggestions were of any help to you.

rtmistler 05-10-2018 06:58 AM

This thread has been closed. Please review LQ guidelines about how to ask effective questions for your next question.


All times are GMT -5. The time now is 02:08 AM.