need help via bash

[edu@linux]$ tree test
├── 1dir
│      └── text.txt
├── 2dir
│      └── text.txt
└── 3dir
       └── text.txt

3 directories, 3 files

[edu@linux]$ cat test/*/text.txt

how i can get output the name of dir masked via '*' in commandline like this:

1dir testtext1
2dir testtext2
3dir testtext3

Thank you

  • Unless I am missing something, it is far simpler to use ls */text/txt | awk -F/ '{print $1,"test"$2}' . Adding a number to the end of each line is a also trivial operation with sed.
    – dhm
    Jan 24 at 19:30

1 Answer 1


With awk:

awk 'FNR == 1 {split(FILENAME, parts, "/")}
     {print parts[2], $0}' test/*/test.txt

That is, on the first line of each file, split the filename on / into the parts array.

And for each line, print the second element of that array, and the full record ($0), separated with OFS (space by default).

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.