Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie: creating new text file from command line

0 views
Skip to first unread message

hdcpa

unread,
Jun 25, 2003, 12:40:38 AM6/25/03
to

How can I create a new text file (in my home directory lets say) in a bash
console or as an entry in a bash script?

Thanks.

Julian Baldwin

unread,
Jun 25, 2003, 1:18:16 AM6/25/03
to
On Wed, 25 Jun 2003 14:40:38 +1000, hdcpa wrote:


> How can I create a new text file (in my home directory lets say) in a
> bash console or as an entry in a bash script?

$ touch <filename>
$ > <filename>

JB

hdcpa

unread,
Jun 25, 2003, 1:52:07 AM6/25/03
to
Julian Baldwin wrote:

Thank you.

This was one of those hard things for me to find.

I tried man -k new| grep text, man -k create, man -k file | grep new, etc.
all to no avail.

Thanks.

Vwakes

unread,
Jun 25, 2003, 3:37:24 AM6/25/03
to
On Wed, 25 Jun 2003 hdcpa wrote:

>> $ touch <filename>
>> $ > <filename>
>

>This was one of those hard things for me to find.
>
>I tried man -k new| grep text, man -k create, man -k file | grep new,
>etc. all to no avail.

Try this manual, its a good resource for linux/unix starters.

http://www.icon.co.za/~psheer/book/index.html.gz
http://www.icon.co.za/~psheer/book/node7.html.gz#chap:basiccmd

Download and keep a local copy with a bookmark to it.

Spozza

unread,
Jun 25, 2003, 3:40:57 AM6/25/03
to
> Julian Baldwin wrote:
>
> Thank you.
>
> This was one of those hard things for me to find.
>
> I tried man -k new| grep text, man -k create, man -k file | grep new, etc.
> all to no avail.
>
you can always

$ echo "" > newtextfile.txt

As well. If the newtextfile.txt doesn't exist it will create it and add
"" to it (i.e. nothing). Thats the bodgers way though!

Sybren Stuvel

unread,
Jun 25, 2003, 4:11:56 AM6/25/03
to
hdcpa enlightened us with:

> This was one of those hard things for me to find.
>
> I tried man -k new| grep text, man -k create, man -k file | grep new,
> etc. all to no avail.

One of the reasons you didn't find anything, is that in Linux a file is
classified by its contents. That means: it's not a text file until you
put text into it. It's not a .jpeg file unless you put a JPEG image into
it. An empty file thus has no type except "empty file". Linux doesn't
look at extentions.

Another nice tool: do "man file" - "file" can identify files for you by
their contents.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?

Davorin Vlahovic

unread,
Jun 25, 2003, 5:45:02 AM6/25/03
to
In article <aJ9Ka.11429$C83.1...@newsread1.prod.itd.earthlink.net>,

cat >> file
Some text is in this file...
^D

--
You know how to win a victory, Hannibal, but not how to use it.
-- Maharbal

Peter Jensen

unread,
Jun 25, 2003, 10:41:51 AM6/25/03
to
Spozza wrote:

> you can always
>
> $ echo "" > newtextfile.txt
>
> As well. If the newtextfile.txt doesn't exist it will create it and add
> "" to it (i.e. nothing). Thats the bodgers way though!

There appears to be no shortage of ways to accomplish this simple task
:-)

Before I learnt about 'touch', I used 'cat /dev/null > <file>'.
However, I have found that the simplest way of creating a file is to
just use it ...

--
PeKaJe

Don't worry if you're a kleptomaniac; you can always take something for it.

Ivan Marsh

unread,
Jun 25, 2003, 10:51:05 AM6/25/03
to
On Wed, 25 Jun 2003 14:41:51 +0000, Peter Jensen wrote:

> Spozza wrote:
>
>> you can always
>>
>> $ echo "" > newtextfile.txt
>>
>> As well. If the newtextfile.txt doesn't exist it will create it and add
>> "" to it (i.e. nothing). Thats the bodgers way though!
>
> There appears to be no shortage of ways to accomplish this simple task
> :-)
>
> Before I learnt about 'touch', I used 'cat /dev/null > <file>'. However, I
> have found that the simplest way of creating a file is to just use it ...

Keep in mind if the file already exists touch updates it's timestamp and
otherwise leaves it alone.

touch doesn't create a new, empty file unless the file doesn't exist.

--
i.m.
All views, opinions and alleged facts expressed by this tactless moron are
protected by the constitution of the United States of America and should be
taken as good natured and friendly unless specifically stated otherwise.

hdcpa

unread,
Jun 25, 2003, 1:35:18 PM6/25/03
to

Thanks very much to everyone for the replies.

Lew Pitcher

unread,
Jun 25, 2003, 10:54:37 AM6/25/03
to
Without hesitation, hdcpa asserted (on or about 06/25/03 00:40) that:

> How can I create a new text file (in my home directory lets say) in a bash
> console or as an entry in a bash script?

Assuming that you specify a new "text file" because you want the file to
contain text, then you can use any of these...

a) echo "Some text goes here" >new.text.file

b) cat >new.text.file <-EOF
many lines of new text
can be placed here, and
will be copied to stdout
The text is terminated by
a single line containing
the word "EOF". See the
bash manpage, "REDIRECTION"
section, "HERE Documents"
subsection for details.
EOF

c) any text editor, like vi, emacs, or pico


--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.

Bill Marcum

unread,
Jun 25, 2003, 10:08:47 PM6/25/03
to
You can simply open your favorite text editor with the filename that you
want, or no filename, and then save your text.

--
It is often the case that the man who can't tell a lie thinks he is the best
judge of one.
-- Mark Twain, "Pudd'nhead Wilson's Calendar"

Gene Wainwright

unread,
Jun 27, 2003, 12:45:28 AM6/27/03
to
Bill Marcum wrote:
> On Wed, 25 Jun 2003 04:40:38 GMT, hdcpa
> <> wrote:
>
>>How can I create a new text file (in my home directory lets say) in a bash
>>console or as an entry in a bash script?
>>
>>
>
> You can simply open your favorite text editor with the filename that you
> want, or no filename, and then save your text.
>
Or you can try --> 'touch filename' to create a file, & then open it
with the editor of your choice.

0 new messages