Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I need some help please. I'd like to know if it's possible to create and insert some text into a file with one (or a couple) of command line ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2004
    Location
    Leicester, UK
    Posts
    7

    Creating and editing files with CLI


    Hi,

    I need some help please. I'd like to know if it's possible to create and insert some text into a file with one (or a couple) of command line statements.

    For example, I want to create a file called monkey.nuts - and on the same command line add the text "Monkey Nuts" into it. It would also be acceptable to create the file first - and then add the "Monkey Nuts" text on a new line.

    So something like;

    vim -[arg] "monkey nuts" > monkey.nuts

    Or piping the command into the file. How do I create a file in the first place?

    I hope this makes sense!

    Thanks for any help

  2. #2
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    Code:
    touch monkey.nuts; echo Monkey Nuts > monkey.nuts
    Wow.. I didn't realize how dirty that sounds. But that's one way to do it. The touch command opens a file and immediately closes it, and if there is no file, creates it. It's a good way to update the modification dates on old files without actually having to open them up, save them, and close them.

    To put more than one line of commands on a single line in BASH, use a semicolon ; The echo command simply prints back whatever text you send it, and the > redirects the output of echo to the file you specify.
    Registered Linux user #270181
    TechieMoe's Tech Rants

  3. #3
    Just Joined!
    Join Date
    Nov 2004
    Location
    Leicester, UK
    Posts
    7
    You sir, have made my day! I knew it was going to be something nice and simple - it's just a mine field when you dont know where to start!!

    Thank you so much for the quick reply.

  4. #4
    Just Joined!
    Join Date
    Nov 2004
    Location
    Leicester, UK
    Posts
    7
    Oh!

    Wait - there's something else if you dont mind.

    Say then I do this:

    touch map.gmt; echo line 1 > map.gmt

    then I want to add another line of text either after or below the line 1 I originally created (without overwriting line 1)

    Is there an echo command to appened or add on new line? So I could keep adding new lines to the file, line 2, line 3, line 4?

    Thanks again!

  5. #5
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    Yes, you can use cat to append to the end of a file instead of overwriting it.

    I don't remember exactly the syntax to do this right now, but you can do a little research by typing man cat.




    I believe the >> operator appends lines to the end of a file in BASH as well. Here's a page that mentions it.
    Registered Linux user #270181
    TechieMoe's Tech Rants

  6. #6
    Just Joined!
    Join Date
    Nov 2004
    Location
    Leicester, UK
    Posts
    7
    Thanks for the help

  7. #7
    Linux Enthusiast puntmuts's Avatar
    Join Date
    Dec 2004
    Location
    Republic Banana
    Posts
    562
    Or use
    Code:
     echo blaat >> filename
    which will append instead of overwrite.
    I\'m so tired .....
    #200472

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •