Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-08-2015, 04:21 PM   #1
LQ Newbie
Registered: Oct 2015
Posts: 1

Rep: Reputation: Disabled
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 <==

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?
Old 10-08-2015, 05:00 PM   #2
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
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.

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.
Old 10-08-2015, 05:18 PM   #3
Registered: Apr 2013
Location: S.E. England
Distribution: Arch
Posts: 161

Rep: Reputation: Disabled
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Output of full file path eldiener Linux - Software 4 04-06-2013 09:46 PM
Configuring GNOME to have Full File Path Text Fields in File Open Dialog Boxes bgoodr Linux - Desktop 3 11-29-2011 10:07 AM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 01:38 PM
set path with output from text file. jstephens84 Slackware 3 12-04-2006 01:49 PM
how to enumerate first line of text file to standard output? zero79 Linux - General 1 07-07-2004 07:37 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:28 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration