Add Filename as first line of file in shell script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












Hello and thanks in advance.



I need to take a file, insert the filename as the first line of the file, then move to a different name. Here's the wrinkle. I need to grab the oldest file in the format of ORIGFILE_YYYYMMDD.TXT and save it as NEWFILE.TXT. For this example, let's say the file name is ORIGFILE_20151117.TXT



  1. Grab oldest file (ls -tr ORIGFILE*.txt)

  2. Add ORIGFILE_20151117.TXT as first line of file

  3. Rename/Move ORIGFILE_20151117.TXT to NEWFILE.TXT









share|improve this question
















bumped to the homepage by Community♦ 6 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • Write the filename to newfile.txt and then append the entire contents of origfile_20151117.txt. After that delete the original file.
    – roaima
    Nov 17 '15 at 21:58














up vote
2
down vote

favorite












Hello and thanks in advance.



I need to take a file, insert the filename as the first line of the file, then move to a different name. Here's the wrinkle. I need to grab the oldest file in the format of ORIGFILE_YYYYMMDD.TXT and save it as NEWFILE.TXT. For this example, let's say the file name is ORIGFILE_20151117.TXT



  1. Grab oldest file (ls -tr ORIGFILE*.txt)

  2. Add ORIGFILE_20151117.TXT as first line of file

  3. Rename/Move ORIGFILE_20151117.TXT to NEWFILE.TXT









share|improve this question
















bumped to the homepage by Community♦ 6 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • Write the filename to newfile.txt and then append the entire contents of origfile_20151117.txt. After that delete the original file.
    – roaima
    Nov 17 '15 at 21:58












up vote
2
down vote

favorite









up vote
2
down vote

favorite











Hello and thanks in advance.



I need to take a file, insert the filename as the first line of the file, then move to a different name. Here's the wrinkle. I need to grab the oldest file in the format of ORIGFILE_YYYYMMDD.TXT and save it as NEWFILE.TXT. For this example, let's say the file name is ORIGFILE_20151117.TXT



  1. Grab oldest file (ls -tr ORIGFILE*.txt)

  2. Add ORIGFILE_20151117.TXT as first line of file

  3. Rename/Move ORIGFILE_20151117.TXT to NEWFILE.TXT









share|improve this question















Hello and thanks in advance.



I need to take a file, insert the filename as the first line of the file, then move to a different name. Here's the wrinkle. I need to grab the oldest file in the format of ORIGFILE_YYYYMMDD.TXT and save it as NEWFILE.TXT. For this example, let's say the file name is ORIGFILE_20151117.TXT



  1. Grab oldest file (ls -tr ORIGFILE*.txt)

  2. Add ORIGFILE_20151117.TXT as first line of file

  3. Rename/Move ORIGFILE_20151117.TXT to NEWFILE.TXT






shell-script text-processing files






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '15 at 21:58









don_crissti

48.1k15126156




48.1k15126156










asked Nov 17 '15 at 21:46









user3191402

112




112





bumped to the homepage by Community♦ 6 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community♦ 6 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.













  • Write the filename to newfile.txt and then append the entire contents of origfile_20151117.txt. After that delete the original file.
    – roaima
    Nov 17 '15 at 21:58
















  • Write the filename to newfile.txt and then append the entire contents of origfile_20151117.txt. After that delete the original file.
    – roaima
    Nov 17 '15 at 21:58















Write the filename to newfile.txt and then append the entire contents of origfile_20151117.txt. After that delete the original file.
– roaima
Nov 17 '15 at 21:58




Write the filename to newfile.txt and then append the entire contents of origfile_20151117.txt. After that delete the original file.
– roaima
Nov 17 '15 at 21:58










2 Answers
2






active

oldest

votes

















up vote
0
down vote













Well, let's break this down into simple steps:



#!/bin/bash

# First, let's get that file's name:
FILE=$(ls -rt ORIGFILE*.txt | tail -n1)
if [[ 0 -ne $? ]]; then
echo "Unable to locate matching file. Aborting." 1>&2
exit 1
fi

# Now, create a new file containing the file's name:
echo "$FILE" > NEWFILE.TXT

# And append the contents of the old file into the new:
cat "$FILE" >> NEWFILE.TXT

# Finally, get rid of the old file: (uncomment if you're sure)
# rm "$FILE"





share|improve this answer



























    up vote
    0
    down vote













    This will do the trick:



    f=$(ls -1tr ORIGFILE*.txt | head -1); echo $f | cat - $f > NEWFILE.txt && rm $f





    share|improve this answer




















    • This works also. Thanks for your help
      – user3191402
      Nov 17 '15 at 22:12










    • Only if the filenames don't contain nonprintable characters.
      – Kusalananda
      Oct 16 at 9:40










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f243711%2fadd-filename-as-first-line-of-file-in-shell-script%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Well, let's break this down into simple steps:



    #!/bin/bash

    # First, let's get that file's name:
    FILE=$(ls -rt ORIGFILE*.txt | tail -n1)
    if [[ 0 -ne $? ]]; then
    echo "Unable to locate matching file. Aborting." 1>&2
    exit 1
    fi

    # Now, create a new file containing the file's name:
    echo "$FILE" > NEWFILE.TXT

    # And append the contents of the old file into the new:
    cat "$FILE" >> NEWFILE.TXT

    # Finally, get rid of the old file: (uncomment if you're sure)
    # rm "$FILE"





    share|improve this answer
























      up vote
      0
      down vote













      Well, let's break this down into simple steps:



      #!/bin/bash

      # First, let's get that file's name:
      FILE=$(ls -rt ORIGFILE*.txt | tail -n1)
      if [[ 0 -ne $? ]]; then
      echo "Unable to locate matching file. Aborting." 1>&2
      exit 1
      fi

      # Now, create a new file containing the file's name:
      echo "$FILE" > NEWFILE.TXT

      # And append the contents of the old file into the new:
      cat "$FILE" >> NEWFILE.TXT

      # Finally, get rid of the old file: (uncomment if you're sure)
      # rm "$FILE"





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Well, let's break this down into simple steps:



        #!/bin/bash

        # First, let's get that file's name:
        FILE=$(ls -rt ORIGFILE*.txt | tail -n1)
        if [[ 0 -ne $? ]]; then
        echo "Unable to locate matching file. Aborting." 1>&2
        exit 1
        fi

        # Now, create a new file containing the file's name:
        echo "$FILE" > NEWFILE.TXT

        # And append the contents of the old file into the new:
        cat "$FILE" >> NEWFILE.TXT

        # Finally, get rid of the old file: (uncomment if you're sure)
        # rm "$FILE"





        share|improve this answer












        Well, let's break this down into simple steps:



        #!/bin/bash

        # First, let's get that file's name:
        FILE=$(ls -rt ORIGFILE*.txt | tail -n1)
        if [[ 0 -ne $? ]]; then
        echo "Unable to locate matching file. Aborting." 1>&2
        exit 1
        fi

        # Now, create a new file containing the file's name:
        echo "$FILE" > NEWFILE.TXT

        # And append the contents of the old file into the new:
        cat "$FILE" >> NEWFILE.TXT

        # Finally, get rid of the old file: (uncomment if you're sure)
        # rm "$FILE"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 '15 at 21:54









        DopeGhoti

        41.8k55180




        41.8k55180






















            up vote
            0
            down vote













            This will do the trick:



            f=$(ls -1tr ORIGFILE*.txt | head -1); echo $f | cat - $f > NEWFILE.txt && rm $f





            share|improve this answer




















            • This works also. Thanks for your help
              – user3191402
              Nov 17 '15 at 22:12










            • Only if the filenames don't contain nonprintable characters.
              – Kusalananda
              Oct 16 at 9:40














            up vote
            0
            down vote













            This will do the trick:



            f=$(ls -1tr ORIGFILE*.txt | head -1); echo $f | cat - $f > NEWFILE.txt && rm $f





            share|improve this answer




















            • This works also. Thanks for your help
              – user3191402
              Nov 17 '15 at 22:12










            • Only if the filenames don't contain nonprintable characters.
              – Kusalananda
              Oct 16 at 9:40












            up vote
            0
            down vote










            up vote
            0
            down vote









            This will do the trick:



            f=$(ls -1tr ORIGFILE*.txt | head -1); echo $f | cat - $f > NEWFILE.txt && rm $f





            share|improve this answer












            This will do the trick:



            f=$(ls -1tr ORIGFILE*.txt | head -1); echo $f | cat - $f > NEWFILE.txt && rm $f






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 17 '15 at 22:02









            Kira

            2,932823




            2,932823











            • This works also. Thanks for your help
              – user3191402
              Nov 17 '15 at 22:12










            • Only if the filenames don't contain nonprintable characters.
              – Kusalananda
              Oct 16 at 9:40
















            • This works also. Thanks for your help
              – user3191402
              Nov 17 '15 at 22:12










            • Only if the filenames don't contain nonprintable characters.
              – Kusalananda
              Oct 16 at 9:40















            This works also. Thanks for your help
            – user3191402
            Nov 17 '15 at 22:12




            This works also. Thanks for your help
            – user3191402
            Nov 17 '15 at 22:12












            Only if the filenames don't contain nonprintable characters.
            – Kusalananda
            Oct 16 at 9:40




            Only if the filenames don't contain nonprintable characters.
            – Kusalananda
            Oct 16 at 9:40

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f243711%2fadd-filename-as-first-line-of-file-in-shell-script%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?