Results 1 to 5 of 5

Thread: Insert text from one file into another text file

  1. #1
    Join Date
    Aug 2016
    Location
    El Lago, Texas
    Beans
    685
    Distro
    Ubuntu Mate

    Insert text from one file into another text file

    I would like to be able to insert the contents of a text file at the end of the host file.

    I need that because my hosts file is frequently updated.

    How can I do that?

    Thanks.
    Ubuntu Mate 18.04 Bionic
    Mate Desktop

  2. #2
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    15,603
    Distro
    Kubuntu 20.04 Focal Fossa

    Re: Insert text from one file into another text file

    Code:
    cat file1 >> file2
    Appends the contents of file1 to the bottom of file2. The double redirection (">>") means append. With only one redirection character (">") file2 would overwritten.
    Last edited by SeijiSensei; April 7th, 2021 at 12:55 AM.
    If you ask for help, do not abandon your request. Please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  3. #3
    Join Date
    Aug 2016
    Location
    El Lago, Texas
    Beans
    685
    Distro
    Ubuntu Mate

    Re: Insert text from one file into another text file

    It does not work because it is root owned.

    I tried

    Code:
    echo xx | sudo -S cat AddTo_hosts.txt >> /etc/hosts
    Last edited by raleigh3; April 7th, 2021 at 03:45 AM.
    Ubuntu Mate 18.04 Bionic
    Mate Desktop

  4. #4
    Join Date
    Jan 2006
    Location
    Sunny Southend-on-Sea
    Beans
    8,359
    Distro
    Kubuntu 20.04 Focal Fossa

    Re: Insert text from one file into another text file

    Quote Originally Posted by raleigh3 View Post
    It does not work because it is root owned.
    What you need is for the part of the operation that writes to the file (not necessarily the part that reads from a file) to be run with elevated privileges.

    Something like
    Code:
    echo 'xxx' | sudo tee -a /etc/hosts
    or
    Code:
    cat AddTo_hosts.txt | sudo tee -a /etc/hosts
    You can stick a
    Code:
    > /dev/null
    on the end if you don't want the text that you're piping around to also be displayed on your terminal.
    Last edited by CatKiller; April 7th, 2021 at 06:20 AM.

  5. #5
    Join Date
    Aug 2016
    Location
    El Lago, Texas
    Beans
    685
    Distro
    Ubuntu Mate

    Re: Insert text from one file into another text file

    Thanks CatKiller.
    Ubuntu Mate 18.04 Bionic
    Mate Desktop

Bookmarks

Posting Permissions

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