LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to output full path file name & first line of text of each file? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-output-full-path-file-name-and-first-line-of-text-of-each-file-4175555648/)

rosalee 10-08-2015 03:21 PM

How to output full path file name & first line of text of each file?
 
What command would you enter to list the first lines of all text files within your directory or within any directory inside there, and the full path filename?

I tried this: find . -type f -exec head -n1 v {} \;

But the file names come out looking like this:
==> ./.progress <==
mpWjPDUAzdcO6


How can I get output that shows the full path file name on one line, then the first line of the text file on the next line?

MensaWater 10-08-2015 04:00 PM

In your find the dot is specifying "this directory" as it is shortcut you see within every directory for the directory itself.

Instead of specifying dot specify the full path you want to search.

e.g.
If the files are all in /home/user and your current working directory (cwd) is /home/user type the dot you did will work but the output will include "./":
find . -type f -exec head -vn1 {} \;

If instead you type:
find /home/user -type f -exec head -vn1 {} \;
The output will show the full path of the files it finds. (You can do this even if you're sitting in /home/user.)

By the way I grouped the -v with the -n1 to be -vn1. That is usually a good idea for shorthand and making sure the flags you're using are going to the command (e.g. head vs find) you expect on a command line.

normanlinux 10-08-2015 04:18 PM

You could also try head -1 **/*

Although you will also get entries like this:
==> vis/test <==
head: error reading vis/test: Is a directory

caveat: I use zsh which has had **/* for years. but I seem to remember reading somewhere that bash can also do it now


All times are GMT -5. The time now is 05:19 AM.