Using tail command to create a file

20,412

Solution 1

tail will only output the last 10 lines. So one way or another, you'll have to tell it you've finished typing so it knows what lines (the 10 last ones) to output.

If you press CTRL-C (the default intr character on most systems) a SIGINT signal will be sent to it which will kill it. Because it had not seen the end of input by the time you killed it, it will not have had the opportunity to write anything yet, so it will die without having output anything.

The terminal way to signify the end of input (when the terminal is in canonical mode) is to enter the eof character (by default Ctrl-D on most systems) on an empty line.

Then tail will detect that the end of input is being reached and will output the 10 last lines that it has received.

Solution 2

Yes, you should provides a input to tail, so :

tail file > newfile

If you want to use STDIN :

cat file | tail > newfile

or

 head > new_file < file

or

tail > AFS2F1<<EOF
PASSES ALL DATA FROM INPUT TO OUTPUT
PASSES ONLY SPECIFIED COLUMNS
PASSES NUMBER OF SPECIFIED LINES AT BEGINNING
COMBINES COLUMNS
ARRANGES DATA IN SEQUENCE
PASSES NUMBER OF SPECIFIED LINES AT THE END OF DATA
TRANSLATES ONE OR MORE CHARACTERS
DELETES DUPLICATE LINES
COUNTS CHARACTERS, WORDS, OR LINES
ABCDEFGHIJKLMNOPQRSTUVWXYZ
EOF

Solution 3

The problem is that tail program buffers its input file until it get a eof condition, then it prints out the last lines (10 by default). Most likely you are interrupting it with ctrl-c combination that terminates it, thus the tail has no chance to print out the collected lines. If you use ctrl-d instead then you effectively send the end-of-file character to the tail's input stream, so it will print out the lines collected.

Share:
20,412

Related videos on Youtube

user1763658
Author by

user1763658

Updated on September 18, 2022

Comments

  • user1763658
    user1763658 over 1 year

    This is what I did using tail command. When I try to output the file to screen the file is empty.

    ~]$ tail > AFS2F1
    PASSES ALL DATA FROM INPUT TO OUTPUT
    PASSES ONLY SPECIFIED COLUMNS
    PASSES NUMBER OF SPECIFIED LINES AT BEGINNING
    COMBINES COLUMNS
    ARRANGES DATA IN SEQUENCE
    PASSES NUMBER OF SPECIFIED LINES AT THE END OF DATA
    TRANSLATES ONE OR MORE CHARACTERS
    DELETES DUPLICATE LINES
    COUNTS CHARACTERS, WORDS, OR LINES
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    

    Is there anything wrong with the way I'm using tail command?

  • user1763658
    user1763658 about 11 years
    Then how do i use tail to create a file from stdin? The way I did above was similar for cat command so i thought i would work
  • Angel Todorov
    Angel Todorov about 11 years
    tail > file is perfectly fine if the user wants to type a bunch of stuff on stdin and save the last 10 lines into the file.
  • Gilles Quenot
    Gilles Quenot about 11 years
    See my edited post