LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 05-07-2018, 06:01 PM   #1
saderror256
LQ Newbie
 
Registered: May 2018
Location: localhost
Distribution: Arch Linux and Ubuntu
Posts: 1

Rep: Reputation: Disabled
Question 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?
 
Old 05-07-2018, 06:12 PM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
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
 
Old 05-07-2018, 06:15 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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?
 
Old 05-07-2018, 06:35 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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

Last edited by BW-userx; 05-07-2018 at 06:47 PM.
 
1 members found this post helpful.
Old 05-07-2018, 06:42 PM   #5
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by BW-userx View Post
$ cat hello
how are you to day?
I am fine, how are you?
not really wanting to tell you
I look forward to the sequel.
 
Old 05-07-2018, 06:47 PM   #6
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,512

Rep: Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656
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?
 
Old 05-07-2018, 07:48 PM   #7
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Less is the best way, except it won't create files.
 
Old 05-07-2018, 08:17 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
As per the above comments, we really need more info.
Exactly what kind of file do you want and why?
 
Old 05-10-2018, 01:28 AM   #9
FlinchX
Member
 
Registered: Nov 2017
Distribution: Slackware Linux
Posts: 666

Rep: Reputation: Disabled
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.
 
1 members found this post helpful.
Old 05-10-2018, 06:29 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by saderror256 View Post
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.
 
Old 05-10-2018, 06:58 AM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

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


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Create new file in bash using date as condition rashmi88 Linux - Newbie 28 06-18-2017 01:37 PM
[BASH] How to create *.tgz file? Ivano Programming 2 04-24-2013 05:00 PM
Create a bash file trolojik1 Programming 6 05-26-2008 02:48 AM
how do i create an empty bash(.sh) file in command line? bbmak Linux - Newbie 1 10-26-2007 02:39 AM
Create a text file using Bash Boffy Programming 5 03-14-2005 09:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration