Page 1 of 1

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

Posted: Wed Sep 16, 2020 7:21 pm
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

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

Posted: Wed Sep 16, 2020 7:35 pm
by MrEen
How close does this get:

Code: Select all

ls -R1 /path/to/folder | rev | cut -d"." -f2-  | rev

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

Posted: Wed Sep 16, 2020 7:37 pm
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.

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

Posted: Wed Sep 16, 2020 7:39 pm
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

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

Posted: Wed Sep 16, 2020 7:39 pm
by MrEen
pepperminty wrote:
Wed Sep 16, 2020 7:37 pm
MrEen. Not quite there.
ROFL. I knew it! :D

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

Posted: Wed Sep 16, 2020 7:41 pm
by pepperminty
Sorry. I was so excited just to see something.
But I had to quickly erase the "[Solved]" from the OP's title.

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

Posted: Wed Sep 16, 2020 7:43 pm
by MrEen
As a guess, I'll bet you want the folder paths changed to just the folder names. Would that be accurate?

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

Posted: Wed Sep 16, 2020 7:46 pm
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.

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

Posted: Wed Sep 16, 2020 7:49 pm
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.

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

Posted: Wed Sep 16, 2020 7:51 pm
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).

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

Posted: Wed Sep 16, 2020 8:22 pm
by pepperminty
Solved with one word:

Code: Select all

find
:)

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

Posted: Wed Sep 16, 2020 8:27 pm
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

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

Posted: Wed Sep 16, 2020 8:28 pm
by pepperminty
I just tried that, and grouping is still off. :) Thanks so much though.

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

Posted: Wed Sep 16, 2020 8:33 pm
by MrEen
You're welcome. I'm glad you found a solution. :D