Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List the first few lines of every file in a directory

Tags:

linux

bash

shell

I'm trying to create a really simple bash script, which will list the first few lines of every file in a specific directory. The directory should be specified by the argument. I think that the Grep command should be used, but I have really no idea how.

My existing script does not seem to work at all, so it's no use putting it in here.

like image 231
user3084390 Avatar asked Dec 09 '13 20:12

user3084390


People also ask

How do you list number of lines of every file in a directory?

The easiest way to count files in a directory on Linux is to use the “ls” command and pipe it with the “wc -l” command. The “wc” command is used on Linux in order to print the bytes, characters or newlines count.

Which command is used to view the first line of every file in a directory?

Then, you need to use the head command to show first few lines of a file.

Which command is used to list all the files in a directory?

The ls command is used to list files. "ls" on its own lists all files in the current directory except for hidden files.


2 Answers

Use head command:

head -3 /path/to/dir/*
like image 135
anubhava Avatar answered Oct 16 '22 22:10

anubhava


For any answer using head and *, redirect stderr to /dev/null unless you want to see errors like:

head: error reading ‘tmp’: Is a directory
like image 42
glenn jackman Avatar answered Oct 16 '22 21:10

glenn jackman