0

I'm trying to find all the values stored in files in a directory.

I can cat /some/dir/* but that just gets me something like

1
2
3
4

What I want is something like

/some/dir/file1: 1
/some/dir/file2: 2

Is there any way I can accomplish this?

1

If all files contain only one line:

find /some/dir -type f -exec awk '{print FILENAME ": " $0}' {} +

Else, print a single line with the filename as header:

find /some/dir -type f -exec awk 'FNR<2{print FILENAME ":"}1' {} +
2

With GNU grep (which happens to be the implementation found on CEntOS) or compatible:

grep -r '^' /some/dir

The ^ regular expression matches at the start of each line, so matches every line. grep prepends the file name to each matching line. You can also add the -n option to include line numbers.

POSIXly:

find /some/dir -type f -exec grep '^' /dev/null {} +

We add /dev/null to make sure grep prints the file name even if the list of files has only one element.

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.