I wanted to read the first text file in every directory using "cat or vi". To clarify the issue:

The Folder_A1 contain 1000 text files. The files named in the following format "i.1351". All the files names starts with "i." and the number is different between these files.

I have 500 Folders, each one contain ~1000 text file (*.txt), and I want to read just the first file in each folder. How can I do that?

marked as duplicate by don_crissti, Satō Katsura, Stephen Kitt, GAD3R, Anthon Sep 29 '16 at 20:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    What do you mean by "first file"? – Satō Katsura Sep 29 '16 at 14:17
  • if you "cd" the folder and run "ls ". I want to read the first file – user88036 Sep 29 '16 at 14:18

If you really want the first file in directory order, and assuming all folder names start with Folder_, all entries starting with Folder_ are directories, and all entries stating with i. are files:

for d in Folder_*; do
    for f in "$d"/i.*; do
        cat -- "$f"

This is a bit slow because of the sort.

find . -type f | sort -V | 
awk -F/ '{ dir=substr($0,1,length($0)-length($NF));
           if(last==dir)next; last=dir; print}' |
| xargs cat

The -V is to sort i.2 before i.10. The awk prints the filename every time the directory changes. (Assumes names have no spaces in them.)