How do I read the first line of a file using cat
?

- 5,034
- 2
- 20
- 43

- 10,411
- 11
- 40
- 48
11 Answers
You don't need cat
.
head -1 file
will work fine.

- 5,034
- 2
- 20
- 43

- 219,201
- 40
- 422
- 469
-
82+1. you only need the head not the whole cat :P (the command tail is part of the bash pun too) – SparK Feb 17 '14 at 18:54
-
1Does `head` read the entire file and cut it, or does it read only the 1 line? – CMCDragonkai May 01 '15 at 02:53
-
@CMCDragonkai, I guess you could check the [source](http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/head.c). – Carl Norum May 01 '15 at 03:47
-
2I guess it reads only first lines - it opens a hundreds-megabytes file in milliseconds. – Brian Cannard Sep 14 '15 at 14:21
-
What about if your were catting multiple files? e.g. I want to achieve `cat file1 file2 | head` without reading all of `file1` and `file2`? – James Owers Oct 28 '15 at 14:02
-
1...got it! `cat <(head file1) <(head file2)`. [Source](http://www.commandlinefu.com/commands/view/1385/cat-stdout-of-multiple-commands) – James Owers Oct 28 '15 at 14:07
-
I'm not sure if this is a Mac-specific thing, but the command is `head -n 1 file`. There is a `-n` argument to pass the number of lines with. – Gustavo Straube Aug 25 '16 at 10:49
-
`head -1 *.txt` would work too to find first line of all text files – a7md0 Oct 04 '22 at 19:35
You could use cat file.txt | head -1
, but it would probably be better to use head directly, as in head -1 file.txt
.

- 2,939
- 22
- 23
This may not be possible with cat
. Is there a reason you have to use cat
?
If you simply need to do it with a bash command, this should work for you:
head -n 1 file.txt

- 8,949
- 10
- 42
- 63
cat
alone may not be possible, but if you don't want to use head
this works:
cat <file> | awk 'NR == 1'

- 4,861
- 20
- 31
-
2I suppose it's silly to call out a 'useless use of cat' on a line specifically designed to use `cat`, isn't it. – jkerian May 24 '11 at 19:20
-
-
3@desgua `awk` is great, but you don't need `cat` here. `awk 'NR == 2 {print $0}'
` does the same thing. (And much more, if you learn a little `awk`. – Eric Wilson Jun 25 '15 at 13:36
I'm surprised that this question has been around as long as it has, and nobody has provided the pre-mapfile built-in approach yet.
IFS= read -r first_line <file
...puts the first line of the file in the variable expanded by "$first_line"
, easy as that.
Moreover, because read
is built into bash and this usage requires no subshell, it's significantly more efficient than approaches involving subprocesses such as head
or awk
.

- 280,126
- 43
- 390
- 441
-
-
my bad, here is an explanation: https://unix.stackexchange.com/questions/184863/what-is-the-meaning-of-ifs-n-in-bash-scripting – Florian Castellane May 24 '18 at 12:01
-
Right; in this case, clearing `IFS` prevents leading and trailing whitespace from being stripped from the string that's read. – Charles Duffy May 24 '18 at 15:39
Use the below command to get the first row from a CSV file or any file formats.
head -1 FileName.csv

- 1,584
- 1
- 19
- 26

- 71
- 3
You dont need any external command if you have bash v4+
< file.txt mapfile -n1 && echo ${MAPFILE[0]}
or if you really want cat
cat file.txt | mapfile -n1 && echo ${MAPFILE[0]}
:)

- 62,119
- 17
- 107
- 194
There is plenty of good answer to this question. Just gonna drop another one into the basket if you wish to do it with lolcat
lolcat FileName.csv | head -n 1

- 8,272
- 23
- 96
- 152
Adding one more obnoxious alternative to the list:
perl -pe'$.<=1||last' file
# or
perl -pe'$.<=1||last' < file
# or
cat file | perl -pe'$.<=1||last'

- 2,028
- 20
- 29