3

What command could I create that will list the first 4 lines of all the files in a given directory?

  • 2
    head -4 /some/directory/*? – muru Dec 6 '15 at 22:04
  • echo "Please input the location of the directory you would like to list" read $dirname head -4 ~/$dirname/* How could i implement it to this? – S.Jones Dec 6 '15 at 22:19
  • 1
    Please edit your question to add clarifications. – muru Dec 6 '15 at 22:27
8
[root@xxx httpd]# head -n 4 /var/log/httpd/*
==> /var/log/httpd/access_log <==
xxxx - - [06/Dec/2015:22:22:45 +0100] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36 Vivaldi/1.0.303.52"
xxxx - - [06/Dec/2015:22:22:46 +0100] "GET /favicon.ico HTTP/1.1" 404 291 "http://195.154.165.63:8001/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36 Vivaldi/1.0.303.52"

==> /var/log/httpd/access_log-20151018 <==
xxxx - - [12/Oct/2015:14:05:42 +0200] "GET /git HTTP/1.1" 404 281 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"
xxxx - - [12/Oct/2015:14:05:42 +0200] "GET /favicon.ico HTTP/1.1" 404 289 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"
xxxx - - [12/Oct/2015:14:05:43 +0200] "GET /favicon.ico HTTP/1.1" 404 289 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"
xxxx - - [12/Oct/2015:14:06:24 +0200] "GET /git HTTP/1.1" 502 465 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"

==> /var/log/httpd/access_log-20151115 <==
xxxx - - [14/Nov/2015:18:56:04 +0100] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0"
xxxx - - [14/Nov/2015:18:56:05 +0100] "GET /favicon.ico HTTP/1.1" 404 291 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0"
xxxx - - [14/Nov/2015:18:56:05 +0100] "GET /favicon.ico HTTP/1.1" 404 291 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0"
xxxx - - [14/Nov/2015:18:58:28 +0100] "GET /phpmyadmin HTTP/1.1" 403 294 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101   Firefox/42.0"

It's a sample of my httpd directory with the command head -n 4 /var/log/httpd/* for the first 4 lines. Replace head -n 4 by head -n 1 for the first lines.

And you can replace the directory /var/log/httpd/* with your directory for example, /my/directory/* but don't forget the wildcard at the end (*). This wildcard permits to tell that we want all the (non-hidden) files in the directory.

  • If you also want hidden files, you can use find: find /some/path -type f -exec head -4 {} +, this will also traverse subdirectories for you – Xophmeister Dec 7 '15 at 11:23
3

If you only want those 4 lines content and not the file names:

awk 'FNR<=4' ./*.log

To have the lines prefixed with the file names:

awk 'FNR<=4 {print FILENAME ": " $0}' ./*.log

Or with GNU grep:

grep -Hm4 '^' ./*.log
  • +1 for a (very) portable one (./ to avoid some pb with filenames, and awk is available on some (very) old systems where tail doesn't support more than 1 file). But gnu grep m4 is an oddity here... seems like it has a bit of "creeping-featurism" ^^ [edit: I looked into -m : it is in fact quite handy and clever, and I understand now why it's added in. Not out of context in grep at all, especially the way it handles the seeking position in the file] – Olivier Dulac Dec 7 '15 at 16:20
  • an alternative way: print the filename and then the lines : awk 'FNR==1 {printf "\n%s:\n",FILENAME } FNR<=4' ./*.log – Olivier Dulac Dec 7 '15 at 16:35
1

If by "all files" you mean "regular files" and not directories, sockets, FIFOs, etc, then you'll need to find those using, well, find:

find "$dir" -maxdepth 1 -type f

and pass them to xargs to invoke head on them:

find "$dir" -maxdepth 1 -type f -print0 | xargs -0 head -n 4

If you also want to follow symbolic links that eventually point at regular files, then use -xtype in place of -type (or use find -L in place of find).

(I've assumed GNU find and xargs above; if you're not in GNU environment, see notes in the comments).

  • 1
    Note that -maxdepth, -print0 and -0 are GNU extensions (though now supported by a few other implementations). POSIXly: find "$dir/." ! -name . -prune -type f -exec head -n 4 {} +. – Stéphane Chazelas Dec 7 '15 at 13:47
  • 1
    Note that xargs or -exec {} + may end up running head several times. If the last batch has only one file, its name will not be printed. GNU head has a -v option to work around that. – Stéphane Chazelas Dec 7 '15 at 13:49
  • Note that contrary to "$dir"/*, it will include hidden files and not sort the file list. – Stéphane Chazelas Dec 7 '15 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.