Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

50%
+0 −0
Q&A How to list the first x files in each directory

There's three parts to this: Find all directories (in your case, sounds like you want depth=3 only) Print the top 3 files in a single directory Apply 2 to each in 1 1 should be a separate q...

posted 1y ago by matthewsnyder‭  ·  edited 1y ago by matthewsnyder‭

Answer
#2: Post edited by user avatar matthewsnyder‭ · 2023-10-10T14:07:52Z (about 1 year ago)
  • There's two parts to this:
  • 1. Find all directories (in your case, sounds like you want depth=3 only)
  • 2. Print the top 3 files in a single directory
  • 3. Apply 2 to each in 1
  • 1 should be a separate question but both `find` can do it. I prefer `fd`: `fd --type directory . --max-depth 3 --min-depth 3`. There might be a shorter way to request a depth of exactly 3 but I don't know it.
  • 2 is obviously done with `head -n 3`.
  • 3 can be done as described in https://linux.codidact.com/posts/288310.
  • Putting it all together, it would be something like this:
  • ```
  • fd --type directory . --max-depth 3 --min-depth 3 \
  • | parallel 'fd --type file . {} | head -n 3'
  • ```
  • There's three parts to this:
  • 1. Find all directories (in your case, sounds like you want depth=3 only)
  • 2. Print the top 3 files in a single directory
  • 3. Apply 2 to each in 1
  • 1 should be a separate question but both `find` can do it. I prefer `fd`: `fd --type directory . --max-depth 3 --min-depth 3`. There might be a shorter way to request a depth of exactly 3 but I don't know it.
  • 2 is obviously done with `head -n 3`.
  • 3 can be done as described in https://linux.codidact.com/posts/288310.
  • Putting it all together, it would be something like this:
  • ```
  • fd --type directory . --max-depth 3 --min-depth 3 \
  • | parallel 'fd --type file . {} | head -n 3'
  • ```
#1: Initial revision by user avatar matthewsnyder‭ · 2023-10-10T14:07:40Z (about 1 year ago)
There's two parts to this:

1. Find all directories (in your case, sounds like you want depth=3 only)
2. Print the top 3 files in a single directory
3. Apply 2 to each in 1

1 should be a separate question but both `find` can do it. I prefer `fd`: `fd --type directory . --max-depth 3 --min-depth 3`. There might be a shorter way to request a depth of exactly 3 but I don't know it.

2 is obviously done with `head -n 3`.

3 can be done as described in https://linux.codidact.com/posts/288310.

Putting it all together, it would be something like this:
```
fd --type directory . --max-depth 3 --min-depth 3 \
    | parallel 'fd --type file . {} | head -n 3'
```