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.
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.
Then, you need to use the head command to show first few lines of a file.
The ls command is used to list files. "ls" on its own lists all files in the current directory except for hidden files.
Use head command:
head -3 /path/to/dir/*
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With