does head create new file

hi,

Here my doubt is can we create a file using head/tail command in unix.

for example: my file consists 2000 lines , i want to cut it into 2. does this work please give correct command line.

head -1000 abc.txt > acd.txt
tail -1000 abc.txt > bdc.txt

hi,

Here my doubt is can we create a file using head/tail command in unix.

for example: my file consists 2000 lines , i want to cut it into 2. does this work please give correct command line.

head -1000 abc.txt > acd.txt
tail -1000 abc.txt > bdc.txt

  1. Yes the command you had given will work correctly.
    That is
    1-1000 lines
    bcd.txt
    1000-2000 lines

Why you have doubt in that, you could have executed and checked by yourself ?

  1. If you are checking for the other ways.. try split command.

  2. Why you posted this here ? You should create a new post ?!

That's easy to test:

perl -e 'for($i=0;$i<26;$i++){print chr(ord('A')+$i)x40,"\n";}' > test1.txt
head -13 test1.txt > part1.txt
tail -13 test1.txt > part2.txt

Do you now have 2 files, part1.txt and part2.txt, one containing lines A-M, the other containing lines N-Z?

Most versions of "tail" have a limited buffer. Mine is 20k. See "man tail" for your system. Thus the "tail -count" syntax is not reliable where the volume of output will exceed the size of the buffer - in fact it will just give you as many lines as it can.

Here is an example of "tail" going wrong on one of my logs:

tail -200 mylog|wc -l
200

ukh44004 # tail -300 mylog|wc -l
218

tail -400 mylog|wc -l
218

The unix "split" command mentioned earlier will give the correct result for simple text files.

Hi Chitishri,

head -1000 abc.txt               _______this will give you the first 1000 lines
tail -1000 abc.txt                 ________this will give the last 1000 lines

so (> acd.txt) which yu added to the above command wil just create a file or override to the existed file .

note:like if you write cat file >new_file _______________here same thing happens. 

not the head command will create file.

regards,
Sanjay.

This is safer than "head" and "tail" for large numbers of lines:

sed -n '1,1000 p' abc.txt > acd.txt
sed -n '1001,$ p' abc.txt > bcd.txt