Page 1 of 2
How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Tue May 04, 2021 5:25 pm
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
)
Re: How to get a list of files in a folder + sizes , as text?
Posted: Tue May 04, 2021 5:44 pm
by HerrBert
open a terminal in the directory you want to examine and type
Re: How to get a list of files in a folder + sizes , as text?
Posted: Tue May 04, 2021 6:02 pm
by williwaw
and to get the output directed to a text file
Re: How to get a list of files in a folder + sizes , as text?
Posted: Tue May 04, 2021 7:34 pm
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?
Re: How to get a list of files in a folder + sizes , as text?
Posted: Tue May 04, 2021 8:06 pm
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.
Re: How to get a list of files in a folder + sizes , as text?
Posted: Tue May 04, 2021 8:14 pm
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/
Re: How to get a list of files in a folder + sizes , as text?
Posted: Tue May 04, 2021 9:33 pm
by MrAccident
I would never be able to compose the code, even reading the instructions; but I got what I needed.
Thank you.
Re: How to get a list of files in a folder + sizes , as text?
Posted: Fri May 07, 2021 5:25 pm
by HerrBert
Never say never and never give up!
Maybe this is more helpful:
https://grymoire.com/Unix/Sed.html
Re: How to get a list of files in a folder + sizes , as text?
Posted: Fri May 07, 2021 5:50 pm
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.
Re: How to get a list of files in a folder + sizes , as text?
Posted: Fri May 07, 2021 7:01 pm
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.
Posted: Fri May 07, 2021 8:00 pm
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.
Re: How to get a list of files in a folder + sizes , as text?
Posted: Fri May 07, 2021 8:34 pm
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:
Output should be 'clean' text without size, but sorted descending by filesize.
Be aware that it also lists subdirectories...
Re: How to get a list of files in a folder + sizes , as text?
Posted: Fri May 07, 2021 8:52 pm
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
Re: How to get a list of files in a folder + sizes , as text?
Posted: Fri May 07, 2021 9:15 pm
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
Posted: Fri May 07, 2021 9:26 pm
by MrAccident
@HerrBert - no, I need the file sizes to be listed, in human terms (best if separated by a tab from the filename).
Re:
Posted: Sat May 08, 2021 1:12 am
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.
Re: How to get a list of files in a folder + sizes , as text?
Posted: Sat May 08, 2021 2:24 am
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.
Re: How to get a list of files in a folder + sizes , as text?
Posted: Sat May 08, 2021 3:39 am
by Flash
Excellent. Thanks for telling us what worked for you.
Re: How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Sat May 08, 2021 6:10 am
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.
Re: How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Sat May 08, 2021 8:55 pm
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?
Re: How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Sat May 08, 2021 9:55 pm
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.
Re: How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Sat May 08, 2021 10:41 pm
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.
Re: How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Sat May 08, 2021 11:01 pm
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.
Re: How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Sat May 08, 2021 11:20 pm
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?
Re: How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Sat May 08, 2021 11:45 pm
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}'
Re: How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Sun May 09, 2021 12:44 am
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?
Re: How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Sun May 09, 2021 12:58 am
by williams2
in awk ...
change ,$1
to "\t" $1
or as many \t
tabs as you like, eg "\t\t\t"
Re: How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Sun May 09, 2021 3:05 am
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 (41.03 KiB) Viewed 1241 times
Re: How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Mon May 10, 2021 4:28 am
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?
Re: How to get a list of files in a folder + sizes , as text? (Solved)
Posted: Mon May 10, 2021 7:05 pm
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.