Display file names and size

ZeusTheOne

New Member
Joined
Mar 3, 2022
Messages
2
Reaction score
0
Credits
19
Hello I'm new here and I don't know how this forum works, so if I make a mistake please excuse me.
I've got a task to display the files and the subfolders names of a directory and the size of each one in descending order.
The commands that I tried are 'ls -sh', this does what I want but it shows the file size first instead of the file name.
Also I tried ' ls -l |awk '{print $9,$5}' ' but this doesn't work either.
What is the way I can do this to work?
 


Brickwizard

Well-Known Member
Joined
Apr 28, 2021
Messages
2,402
Reaction score
1,617
Credits
18,032

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,454
Reaction score
2,066
Credits
10,141
What about this?:
Bash:
ls -sh | awk '{printf "%-30s %s\n",$2,$1}'

That should do the trick nicely!
The file-system object names and their sizes are listed in distinct columns.

In awk, I've used the printf command to allow us to format the output.
The %-30s gives us a field of 30 characters to put the names of the files/directories etc in the listing.
And the final %s is where the file-size will go.
So $2 - The name of the file-system object, is substituted into the 30 character wide string field.
And $1 - The size of the file-system object, is substituted into the other string field.

NOTE: If 30 characters is too wide, or not wide enough - feel free to alter that value!

Also, because the output from ls is piped to awk - it loses it's colouring - which helps you to differentiate between files, directories and other file-system objects like symbolic links, if you have it enabled. So if you want to be able to easily tell which objects are files and which are directories, symbolic links, etc. You could optionally use the ls commands -F switch, which outputs an additional special character at the end of the name of each file-system object listed.

e.g.
Bash:
ls -shF | awk '{printf "%-30s %s\n",$2,$1}'

This lists everything in two columns as before, but the names of each object in the listing may have an additional character at the end, to indicate which type of FS object it is.

Using -F:
- Regular files don't have any extra characters at the end of their names. So if there are no characters at the end of the file-name - it's just a normal file.
- Executable files have an * after their names.
- Directories have a / after their names.
- Symbolic links have a @ after their names.
- Sockets have an = after their names.
- FIFO pipes have a | after their names.

There are a couple of symbols for other types of objects, like % for whiteouts. But you won't find those on your usual file-system, I think they're specific to OverlayFS, which I think is used by the kernel. I can't remember what the others are - I think they may be things that are used on older UNIX file-systems. I can't remember offhand!
But the symbols listed above are probably the only ones that you're likely to see in your day to day usage!

Hopefully this helps!
 
Last edited:

kleStudent

New Member
Joined
Apr 20, 2022
Messages
1
Reaction score
0
Credits
11
To display name and size of the files in the current directory and sort the list based on the size of the file, you can use the below awk command.

ls -shF | awk '{printf “%-30s %s\n”,$2,$1}' | sort -k2,2

You can use a shell script to add extensions to the above command. Other commands can be added to the shell script. For instance, I've made an extension that displays and then moves all exe files into a file called "exeFiles."

To create a shell script:
gedit shellScript.sh

Type the commands as follows:
ls -shF | awk '{printf “%-30s %s\n”,$2,$1}' | sort -k2,2
find . -name “*.exe”
find -name '*.exe' -exec mv -t exeFiles {} +

Use the following command to launch the shell script:
bash shellScript.sh

Now, you can see that each command is executed one by one.​
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Staff online


Top