list filenames of all files in a folder's subfolders, grouped by subfolder, in one terminal command [solved]

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
User avatar
pepperminty
Level 5
Level 5
Posts: 973
Joined: Thu Jun 23, 2011 10:51 pm

list filenames of all files in a folder's subfolders, grouped by subfolder, in one terminal command [solved]

Post by pepperminty »

I have a folder containing about 9 subfolders. Each subfolder has about 10 files. How can I easily list, in one terminal command, all (approximately) 90 filenames, grouped by subfolder? The listing can appear in the terminal or can be outputted into a simple text file.
All (approximately) 90 files are have the same filename extension. I'd prefer it if the terminal command can just chop off the extension part of the filename. For example, "Every morning.txt" will appear in the text file as "Every morning".

Here's a printout I don't mind having:
Folder1

Every morning
Sweet sundae
The sweet spring season
blah blah


Folder2

The road to nowhere
Surfing USA
A change in time
blahblahblah
Thank you.

UPDATE: Solution is simply one word in terminal:

Code: Select all

find
Last edited by pepperminty on Wed Sep 16, 2020 8:23 pm, edited 6 times in total.
User avatar
MrEen
Level 22
Level 22
Posts: 15097
Joined: Mon Jun 12, 2017 8:39 pm

Re: How to list the filenames of all files in a folder's subfolders, in one terminal command

Post by MrEen »

How close does this get:

Code: Select all

ls -R1 /path/to/folder | rev | cut -d"." -f2-  | rev
User avatar
pepperminty
Level 5
Level 5
Posts: 973
Joined: Thu Jun 23, 2011 10:51 pm

Re: How to list the filenames of all files in a folder's subfolders, in one terminal command

Post by pepperminty »

MrEen. Not quite there.

It doesn't include the name of the folder.
When I wrote "Folder1" or "Folder2" in my OP, that's supposed to be the actual names of the subfolders. :)

I want the actual subfolder names to be part of the output. :)

What it did was list all the subfolder names at the beginning of the output, but separated from the contents of each subfolder.
Last edited by pepperminty on Wed Sep 16, 2020 7:40 pm, edited 1 time in total.
User avatar
MrEen
Level 22
Level 22
Posts: 15097
Joined: Mon Jun 12, 2017 8:39 pm

Re: How to list the filenames of all files in a folder's subfolders, in one terminal command [solved]

Post by MrEen »

Wowzers! I figured it'd get you part way, but not across the finish line.

I'm glad, surprised as heck, but glad, I could help. :D
User avatar
MrEen
Level 22
Level 22
Posts: 15097
Joined: Mon Jun 12, 2017 8:39 pm

Re: How to list the filenames of all files in a folder's subfolders, in one terminal command

Post by MrEen »

pepperminty wrote:
Wed Sep 16, 2020 7:37 pm
MrEen. Not quite there.
ROFL. I knew it! :D
User avatar
pepperminty
Level 5
Level 5
Posts: 973
Joined: Thu Jun 23, 2011 10:51 pm

Re: How to list the filenames of all files in a folder's subfolders, in one terminal command

Post by pepperminty »

Sorry. I was so excited just to see something.
But I had to quickly erase the "[Solved]" from the OP's title.
User avatar
MrEen
Level 22
Level 22
Posts: 15097
Joined: Mon Jun 12, 2017 8:39 pm

Re: How to list the filenames of all files in a folder's subfolders, in one terminal command

Post by MrEen »

As a guess, I'll bet you want the folder paths changed to just the folder names. Would that be accurate?
User avatar
pepperminty
Level 5
Level 5
Posts: 973
Joined: Thu Jun 23, 2011 10:51 pm

Re: How to list the filenames of all files in a folder's subfolders, in one terminal command

Post by pepperminty »

MrEen wrote:
Wed Sep 16, 2020 7:43 pm
As a guess, I'll bet you want the folder paths changed to just the folder names. Would that be accurate?
I think the answer to your question is yes. :)

I *don't" want this sort of printout
/home/folder/folder1

file1
file 2
file 3

/home/folder/folder2

filea
file AB
filebc
Rather, I'd prefer something like:
folder1 [<--- Let's say this is the actual name of the folder :)]

file1
file 2
file 3

folder2

filea
file AB
filebc
FYI, I ran your command from inside the main folder, so I just removed the /path/to/folder part of the command.
User avatar
MrEen
Level 22
Level 22
Posts: 15097
Joined: Mon Jun 12, 2017 8:39 pm

Re: How to list the filenames of all files in a folder's subfolders, in one terminal command

Post by MrEen »

That part I can't help with I'm afraid. I believe it's hard coded to have the path with a semicolon at the end for each folder. There's still a way to do it, but it's beyond my abilities.

Someone else should be able to help you get over that hurdle.
User avatar
pepperminty
Level 5
Level 5
Posts: 973
Joined: Thu Jun 23, 2011 10:51 pm

Re: How to list the filenames of all files in a folder's subfolders, in one terminal command

Post by pepperminty »

MrEen wrote:
Wed Sep 16, 2020 7:49 pm
That part I can't help with I'm afraid. I believe it's hard coded to have the path with a semicolon at the end for each folder. There's still a way to do it, but it's beyond my abilities.

Someone else should be able to help you get over that hurdle.
I don't mind if there's a semicolon. :) I'm not picky.
I also don't mind if it's "path/to/folder" and not just Folder1 or Folder2.

What's important is that the filenames appear in the right group (i.e., associated with the subfolder that contains them).
User avatar
pepperminty
Level 5
Level 5
Posts: 973
Joined: Thu Jun 23, 2011 10:51 pm

Re: list filenames of all files in a folder's subfolders, grouped by subfolder, in one terminal command

Post by pepperminty »

Solved with one word:

Code: Select all

find
:)
User avatar
MrEen
Level 22
Level 22
Posts: 15097
Joined: Mon Jun 12, 2017 8:39 pm

Re: list filenames of all files in a folder's subfolders, grouped by subfolder, in one terminal command [solved]

Post by MrEen »

LOL!

I just thought of this as a possible solution:

Code: Select all

ls -R1 | rev | cut -d"." -f2-  | cut -d"/" -f1,2 | rev
User avatar
pepperminty
Level 5
Level 5
Posts: 973
Joined: Thu Jun 23, 2011 10:51 pm

Re: list filenames of all files in a folder's subfolders, grouped by subfolder, in one terminal command [solved]

Post by pepperminty »

I just tried that, and grouping is still off. :) Thanks so much though.
User avatar
MrEen
Level 22
Level 22
Posts: 15097
Joined: Mon Jun 12, 2017 8:39 pm

Re: list filenames of all files in a folder's subfolders, grouped by subfolder, in one terminal command [solved]

Post by MrEen »

You're welcome. I'm glad you found a solution. :D
Post Reply

Return to “Newbie Questions”