Update 2
Here is a one-liner that uses awk
, which has the benefit of also retaining multiple consecutive spaces in the filenames:
ls -l | awk 'BEGIN{FPAT="([[:space:]]*[^[:space:]]+)"; OFS = "";} FNR == 1 {next} {$1=$2=$3=$4=$6=$7=$8=""}1'
FPAT="([[:space:]]*[^[:space:]]+)"
is used to set a regular expression of what a field should be. Here we set the field to have zero or more whitespace characters and at least one of every other character except whitespace. The suggestion about using FPAT
was found in this Stack Overflow answer.
OFS = ""
is used so that the output will only have a single space as a field separator (another one is added by the text manipulation).
FNR == 1 {next}
is used so that a blank line at the beginning, that corresponds to the first line of the ls -l
output that shows the total number of blocks used, won't be printed.
{$1=$2=$3=$4=$6=$7=$8=""}1
empties the specified fields and prints
the rest, i.e. the fifth field (file size) and the ninth till the end (filename).
It should be noted that each line of the output of the above command will have one or more leading spaces. In my opinion this makes the output easier to read, however, you can remove the leading spaces by piping the awk
command to sed
as follows (remove the *
from the sed
command to remove just one leading space):
ls -l | awk 'BEGIN{FPAT="([[:space:]]*[^[:space:]]+)"; OFS = "";} FNR == 1 {next} {$1=$2=$3=$4=$6=$7=$8=""}1' | sed 's/^ *//'
Update
A better approach would be to use cut
on the ls -l
output:
ls -l | tr -s ' ' | cut -d ' ' -f 5,9-
Here, tr -s ' '
is used to squeeze multiple spaces between the fields of the ls -l
output to one and then cut
uses space as a delimiter to display the fifth field (file size) and all fields after the ninth (filename) of the ls -l
output.
Note, however, that, if you have filenames with multiple consecutive spaces, they will be displayed as having single spaces, due to tr
(thanks bac0n).
You can use this:
ls -sh
-s
(or --size
): prints the allocated size of each file, in blocks.
-h
(or --human-readable
) prints the size in a human readable format (i.e using K, M, etc. for thousands, millions, etc. respectively).
You can read the ls
manpage for more info by running man ls
in your terminal or by visiting the Ubuntu ls
manpage online.
find -maxdepth 1 -type f -printf '%s %f\n'
– Nov 01 '19 at 03:11ls
options? No. Is it possible to get such output ? Yes, either with what bac0n suggested or withdu
command. That's exactly the output thatdu
produces. So the question becomes, what is really the purpose ? To usels
or to get the right output ? What is the output for ? Viewing ? Passing to another command or saving somewhere ? – Sergiy Kolodyazhnyy Nov 02 '19 at 06:07ls -sd --block-size=1 --format=single-column *
– mchid Nov 04 '19 at 01:20-s
is the allocated size in blocks, so will differ from whatdu
may report for the same file (that is what is allocated for the file vs what the file actually is in bytes), and--block-size=1
is GNU specific option. But technically it fits what this question asks. So +1'ed your answer. – Sergiy Kolodyazhnyy Nov 04 '19 at 04:32