How to get a list of files in a folder + sizes , as text? (Solved)

Issues and / or general discussion relating to Puppy

Moderator: Forum moderators

User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

How to get a list of files in a folder + sizes , as text? (Solved)

Post by MrAccident »

(Edit ― in this post ― I only kept the solutions):

• Terminal code; with output into a specific text-file; change it to the file and folder you want. To print only in Terminal instead - remove > /mnt/home/FileList.txt.
Open Terminal in the folder:
• With suffixes: M, K, B:

Code: Select all

du -a -d 1 --block-size=1 | sort -rnk1 | numfmt --to=iec --field=1 | awk '{print substr($0, index($0,$2)), "\t" $1}' | sed 's_./__' > /mnt/home/FileList.txt

• With suffixes: MiB, KIB, B:

Code: Select all

du -a -d 1 --block-size=1 | sort -rnk1 | numfmt --to=iec-i --suffix=B --field=1 | awk '{print substr($0, index($0,$2)), "\t" $1}' | sed 's_./__' > /mnt/home/FileList.txt

With subfolders; either:
• Large to small:

Code: Select all

du -a --block-size=1 | sort -rnk1 | numfmt --to=iec-i --suffix=B --field=1 | awk '{print substr($0, index($0,$2)), "\t" $1}' | sed 's_./__' > /mnt/home/FileList.txt

• Small to large:

Code: Select all

find "$@" -type f -printf "%s %p\n" | sort -nk1 | numfmt --to=iec-i --suffix=B --field=1 | awk '{print substr($0, index($0,$2)) "\t\t" $1}' | sed 's_^./__' > /mnt/home/FileList.txt

-----------------------------
Explanations:
“|” separates the kinds of commands.
""\t" " ― is the separating tab. To have more tabs - add more "\t"s.
Largest to smallest ― change sort -nk1 to sort -rnk1.
delete "./" ― add | sed 's_./__
Search in sub-folders too ― change du -a -d 1 to du -a.
You can put the file names in quotes with the following:

Code: Select all

du -a -d 1 --block-size=1 | sort -rnk1 | numfmt --to=iec --field=1 | awk '{print "\"" substr($0,index($0,$2)) "\"" ,$1}' | sed 's_./__' > /mnt/home/FileList.txt

-----------------------------
• Program ― Double Commander; with which it's possible to create a list of files in a folder; among many other things. (Portable; unpack(for instance to /opt/). The file to launch the program - “doublecmd”).
-----------------------------
• Script: in /root/my-applications/bin/ Mouse right-click → New → Script. Open the file as text ― and paste the code:

Code: Select all

#!/bin/sh
du -a -d 1 --block-size=1 | sort -rnk1 | numfmt --to=iec-i --suffix=B --field=1 | awk '{print substr($0, index($0,$2)), "\t" $1}' | sed 's_./__' > /mnt/home/FileList.txt

To call upon the Script ― Terminal:
cd /mnt/home/the folder you want to load
NameOfTheScript > /mnt/home/FileList.txt
-----------------------------
• Another way:

MochiMoppel wrote: Sun May 09, 2021 3:05 am

@williams2 I tried to push this a little further to make it behave and (almost) look like DoubleCommander. Not sure if this is what MrAccident needs but this is how I would like the output to look like (except for the "human readable" format which I find less readable than sizes in bytes with thousand delimiters).

Includes hidden files, removes '->' trailers from links and keeps directories always on top

Code: Select all

MAX=$(ls -1 | wc -L)                            #find width of longest file name
IFS=$'\t\n'                                     #make sure that printf "sees" only 2 (tab delimited) strings per line
printf "%-${MAX}s\t%5s\n" $(                    #print filenames left aligned, sizes right aligned 
    ls -lAh | awk 'NR>1 {                       #include hidden files, format sizes as human readable, ignore 1st total line
    if (substr($1,1,1)=="d") $5="9999G"         #make sure that directories are listed on top
    if (substr($1,1,1)=="l") sub(" ->.*","")    #in case of links remove trailing " -> /path/of/link/target"
    print substr($0,index($0,$9))"\t"$5}' |     #print field 9 to end of line (=filename),then a tab, followed by field 5 (=size)
    sort -t$'\t' -hrk2 |                        #sort 2nd field (=size) of awk output
    sed 's/9999G/<DIR>/'                        #remove fake directory size
)

Image

Last edited by MrAccident on Sat May 15, 2021 12:11 pm, edited 3 times in total.
HerrBert
Posts: 350
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 18 times
Been thanked: 119 times

Re: How to get a list of files in a folder + sizes , as text?

Post by HerrBert »

open a terminal in the directory you want to examine and type

Code: Select all

du -ah
williwaw
Posts: 1874
Joined: Tue Jul 14, 2020 11:24 pm
Has thanked: 167 times
Been thanked: 345 times

Re: How to get a list of files in a folder + sizes , as text?

Post by williwaw »

and to get the output directed to a text file

Code: Select all

du -ah > some-file-name.txt
User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

Re: How to get a list of files in a folder + sizes , as text?

Post by MrAccident »

@williwaw:
Thanks. But I guess I have a few more specific needs.
A problem ― the list is with the addresses.
Can I change the order of file-name and size? Size is first, and I would like it to be in the end.
And a general question ― is it possible to exclude sub-folders?
And where are these codes from; is there a list of more?

williwaw
Posts: 1874
Joined: Tue Jul 14, 2020 11:24 pm
Has thanked: 167 times
Been thanked: 345 times

Re: How to get a list of files in a folder + sizes , as text?

Post by williwaw »

And where are these codes from; is there a list of more?

-ah is an arguement for the command du. the authoritave list can be seen on the man page (man is short for manual, and a command of it's own) puppy may not have the man page included like some other distributions, but you can google man du and find it easily

that said, the man pages can appear a bit cryptic to some, but you can find tutorials also.

"how to use the du command" brought up a few.

HerrBert
Posts: 350
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 18 times
Been thanked: 119 times

Re: How to get a list of files in a folder + sizes , as text?

Post by HerrBert »

Can I change the order of file-name and size? Size is first, and I would like it to be in the end.
And a general question ― is it possible to exclude sub-folders?
And where are these codes from; is there a list of more?

1)

Code: Select all

du -ah -d 1 | sed 's#\(.*[^\t]*\t\)\(.*\)#\2\t\1#;s#^\./##'

2)
-d 1 limits the depth to the examined directory
sub-folders will be displayed but not their content
3)
du is part of coreutils:
http://www.gnu.org/software/ (scroll down half the page)
http://www.gnu.org/manual/

User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

Re: How to get a list of files in a folder + sizes , as text?

Post by MrAccident »

I would never be able to compose the code, even reading the instructions; but I got what I needed.
Thank you.

HerrBert
Posts: 350
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 18 times
Been thanked: 119 times

Re: How to get a list of files in a folder + sizes , as text?

Post by HerrBert »

Never say never and never give up! :wink:
Maybe this is more helpful:
https://grymoire.com/Unix/Sed.html

User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

Re: How to get a list of files in a folder + sizes , as text?

Post by MrAccident »

@HerrBert - thanks; but I don't understand what it's about. And it's the first time I even needed to make a list of files in a folder, and I can't think of anything else in this relation I may need.

User avatar
Flash
Moderator
Posts: 958
Joined: Tue Dec 03, 2019 3:13 pm
Location: Arizona, U.S.
Has thanked: 50 times
Been thanked: 121 times

Re: How to get a list of files in a folder + sizes , as text?

Post by Flash »

MrAccident wrote: Tue May 04, 2021 9:33 pm

... I got what I needed.

So your problem is solved? Would you tell us more detail about how you solved it?
May I suggest a utility called "tree"? I don't know where you can find it but I think it might do what you want.

Chaos coordinator :?
User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

Post by MrAccident »

@Flash:
I still haven't done that; and I see that I still need another specification. :-\
Specifically - what I need: I have a folder for deletion with many heavy files, on my old computer\drive; I wanted to make a list of them, and to delete the folder.
So now, using the code - I see that the files are arranged... I don't even know by what criteria. So - how can I arrange them in different ways? The specific way I need now ― is by size - biggest to smallest.
I actually can do this by opening the folder in the browser. I can use that; but it will require some work after copying the text into a text-file: deleting all the dates; and the file-size ends in the line under the line of the name of the file ― so I need to go to the beginning of the line - and click Backspace. Most useful would be to have a utility for this purpose.
I've found your thread about Tree; it doesn't seem though - that it worked; and I downloaded it, extracted and clicked on the file ― and it didn't do anything.

HerrBert
Posts: 350
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 18 times
Been thanked: 119 times

Re: How to get a list of files in a folder + sizes , as text?

Post by HerrBert »

@MrAccident
Do i understand you right? You need a list of files sorted by size from biggest to smallest?
Then i suggest using:

Code: Select all

ls -S1 > some_file_name.txt

Output should be 'clean' text without size, but sorted descending by filesize.
Be aware that it also lists subdirectories...

User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

Re: How to get a list of files in a folder + sizes , as text?

Post by MrAccident »

@HerrBert :
I need it with size. The following code - is fine, accept that it isn't arranged by size:

Code: Select all

du -ah -d 1 | sed 's#\(.*[^\t]*\t\)\(.*\)#\2\t\1#;s#^\./##' > /mnt/home/FileList.txt
HerrBert
Posts: 350
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 18 times
Been thanked: 119 times

Re: How to get a list of files in a folder + sizes , as text?

Post by HerrBert »

Maybe this:

Code: Select all

du -a -d 1 | sort -nr | sed 's#\(.*[^\t]*\t\)\(.*\)#\2\t\1#;s#^\./##'

Sorts descending by size, but size is in bytes - not human-readable

User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

Post by MrAccident »

@HerrBert - no, I need the file sizes to be listed, in human terms (best if separated by a tab from the filename).

User avatar
Flash
Moderator
Posts: 958
Joined: Tue Dec 03, 2019 3:13 pm
Location: Arizona, U.S.
Has thanked: 50 times
Been thanked: 121 times

Re:

Post by Flash »

MrAccident wrote: Fri May 07, 2021 8:00 pm

I've found your thread about Tree; it doesn't seem though - that it worked; and I downloaded it, extracted and clicked on the file ― and it didn't do anything.

Open your package manager and search for tree. I did that in Easypup, found tree_1.8.0 for Debian and installed it. Evidently tree requires some other programs to be installed, so whatever version of tree you find in your package manager should give you a clue to where to find the other programs if they're not already in BionicPup.

Chaos coordinator :?
User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

Re: How to get a list of files in a folder + sizes , as text?

Post by MrAccident »

@Flash - I wasn't able to find the first dependency - "linux-vdso.so.1". But thanks to ThruHammer suggesting to install Double Commander (Portable; unpack and click doublecmd) ― I was able to get the list of files in a folder, ordered and with size - by using Mark→Copy all shown columns (and removing extra columns: Date, Attribute etc).
And seems to be a great program.

User avatar
Flash
Moderator
Posts: 958
Joined: Tue Dec 03, 2019 3:13 pm
Location: Arizona, U.S.
Has thanked: 50 times
Been thanked: 121 times

Re: How to get a list of files in a folder + sizes , as text?

Post by Flash »

Excellent. Thanks for telling us what worked for you. :)

Chaos coordinator :?
williams2
Posts: 1059
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 302 times

Re: How to get a list of files in a folder + sizes , as text? (Solved)

Post by williams2 »

The following code - is fine, accept that it isn't arranged by size:

on one line:

Code: Select all

du -a -d 1 --block-size=1 | sort -nk1 | numfmt --to=iec-i --suffix=B --field=1 | awk '{print $2,$1}'

Code: Select all

du -a -d 1 --block-size=1 |
sort -nk1 |
numfmt --to=iec-i --suffix=B --field=1 |
awk '{print $2,$1}'

Won't work properly if file names have spaces in the name.

User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

Re: How to get a list of files in a folder + sizes , as text? (Solved)

Post by MrAccident »

@williams2:
I used this code (added the file at the end of your code):

Code: Select all

du -a -d 1 --block-size=1 | sort -nk1 | numfmt --to=iec-i --suffix=B --field=1 | awk '{print $2,$1}' > /mnt/home/FileList.txt

Strange - I'm sure the first time I ran the code - it wasn't ordered by size at all; and then I ordered the folder in Rox to be by size - smallest to largest ― and it was arranged like that in the file. Anyway - since then ― it only arranges the files smallest to largest, and also adds "./" in the beginning of each filename.

Won't work properly if file names have spaces in the name.

Of course many do; duh! So it's not suppose to work?

williams2
Posts: 1059
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 302 times

Re: How to get a list of files in a folder + sizes , as text? (Solved)

Post by williams2 »

awk '{print $2,$1}' prints field #2 (the size) then field #1 (the filename.)
Which works if the filename has no spaces in it.
But awk uses <space> as a field delimiter, which is why there would be a problem.

So you would need to do something like this:

Code: Select all

du -a -d 1 --block-size=1 | sort -nk1 | numfmt --to=iec-i --suffix=B --field=1 | awk '{print substr($0, index($0,$2)),$1}' > /mnt/home/FileList.txt

I think that should work properly.

User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

Re: How to get a list of files in a folder + sizes , as text? (Solved)

Post by MrAccident »

@williams2 - no, it's still from smallest to largest, even with files without spaces; and with “./”. But there are spaces in most files, and I found the way to do this in Double Commander.

williams2
Posts: 1059
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 302 times

Re: How to get a list of files in a folder + sizes , as text? (Solved)

Post by williams2 »

no, it's still from smallest to largest

Change sort -nk1 to sort -rnk1

You can delete the ./
something like sed 's_./__'

You can put the file names in quotes.
Something like awk '{print "\"" substr($0,index($0,$2)) "\"" ,$1}'

DoubleCommander can be installed in MS Windows, too.

User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

Re: How to get a list of files in a folder + sizes , as text? (Solved)

Post by MrAccident »

It seems to work; there's the same amount of lines as files, and they seem to be ordered right, at a glance. Why you thought it shouldn't work with files with spaces in the name?

williams2
Posts: 1059
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 302 times

Re: How to get a list of files in a folder + sizes , as text? (Solved)

Post by williams2 »

Why you thought it shouldn't work with files with spaces in the name?

awk treats space characters as file separators, so the first version would not work right.

This might work too:

Files and dirs by size, with the size after the filename:

Code: Select all

ls -Slh | awk 'NR>1 {print substr($0,index($0,$9)),$5}'
User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

Re: How to get a list of files in a folder + sizes , as text? (Solved)

Post by MrAccident »

@williams2 - works too. Do you know how to separate the filename and size with a tab or a couple, instead of 1 space?

williams2
Posts: 1059
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 302 times

Re: How to get a list of files in a folder + sizes , as text? (Solved)

Post by williams2 »

in awk ...
change ,$1 to "\t" $1

or as many \t tabs as you like, eg "\t\t\t"

User avatar
MochiMoppel
Posts: 1196
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 20 times
Been thanked: 415 times

Re: How to get a list of files in a folder + sizes , as text? (Solved)

Post by MochiMoppel »

@williams2 I tried to push this a little further to make it behave and (almost) look like DoubleCommander. Not sure if this is what MrAccident needs but this is how I would like the output to look like (except for the "human readable" format which I find less readable than sizes in bytes with thousand delimiters).

Includes hidden files, removes '->' trailers from links and keeps directories always on top

Code: Select all

MAX=$(ls -1 | wc -L)                            #find width of longest file name
IFS=$'\t\n'                                     #make sure that printf "sees" only 2 (tab delimited) strings per line
printf "%-${MAX}s\t%5s\n" $(                    #print filenames left aligned, sizes right aligned 
    ls -lAh | awk 'NR>1 {                       #include hidden files, format sizes as human readable, ignore 1st total line
    if (substr($1,1,1)=="d") $5="9999G"         #make sure that directories are listed on top
    if (substr($1,1,1)=="l") sub(" ->.*","")    #in case of links remove trailing " -> /path/of/link/target"
    print substr($0,index($0,$9))"\t"$5}' |     #print field 9 to end of line (=filename),then a tab, followed by field 5 (=size)
    sort -t$'\t' -hrk2 |                        #sort 2nd field (=size) of awk output
    sed 's/9999G/<DIR>/'                        #remove fake directory size
)
ListFilesAndSizes.png
ListFilesAndSizes.png (41.03 KiB) Viewed 1078 times
User avatar
MrAccident
Posts: 279
Joined: Mon Aug 03, 2020 4:29 am
Has thanked: 41 times
Been thanked: 8 times

Re: How to get a list of files in a folder + sizes , as text? (Solved)

Post by MrAccident »

@MochiMoppel - Chef's Kiss.
Still a little question ― why "b" for file-sizes in bytes isn't shown?

@williams2:

williams2 wrote: Sun May 09, 2021 12:58 am

in awk ...
change ,$1 to "\t" $1

or as many \t tabs as you like, eg "\t\t\t"

Worked. And I already used it for what I intended.
Now to make it perfect ― is it possible to get rid of the "./" in the beginning of the filename?
And how to search in sub-folders as well?

williams2
Posts: 1059
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 302 times

Re: How to get a list of files in a folder + sizes , as text? (Solved)

Post by williams2 »

is it possible to get rid of the "./" in the beginning of the filename?

Yes, as I said in an earlier post, add | sed 's_./__' to the chain, maybe at the end of the chain.

And how to search in sub-folders as well?

Change du -a -d 1 to du -a

You might prefer to use find instead of du

Code: Select all

find "$@" -type f -printf "%s %p\n" | sort -nk1 | numfmt --to=iec-i --suffix=B --field=1 | awk '{print substr($0, index($0,$2)) "\t\t" $1}' | sed 's_^./__' > /mnt/home/FileList.txt

If this is a script named largest
then largest would list all file in the current working dir and subdirs,
and largest /mnt/videos/ /root/downloads/ would list all files in /mnt/videos/ and in /root/downloads/ and subdirs.
It will not list dirs or symlinks.

By the way, gdmap can be useful to find large files.

Post Reply

Return to “Users”