You mean like this, just prepending the current directory to each filename?
ls -l | awk -v PWD=$PWD '{printf("%s %s/%s\n", $5, PWD, $9); }'
(the -v
just imports the $PWD
shell variable into the awk script).
Or something else?
OK, apparently what you want is
$ cd /some/path/to/somewhere
$ <insert command here>
somewhere/file1 size1
somewhere/file2 size2
...
Is that correct?
If so, the change you need is this:
ls -l | awk -v PWD=$(basename $PWD) '{printf("%s/%s %s\n", PWD, $9, $5); }'
If you have an older shell, the $()
may not work, in which case try:
ls -l | awk -v PWD=`basename $PWD` '{printf("%s/%s %s\n", PWD, $9, $5); }'
instead. I don't have immediate access to any shell that doesn't support $()
, but I can't think where else your Illegal variable name
error would come from, when this works for me.
If it still doesn't work, please describe your platform, shell, version of awk etc. in your question - the comment thread is getting pretty long and I'm running out of guesses :-)
The following function does most of what you're asking for:
dir () { ls -FaGl "${@}" | awk '{ total += $4; print }; END { print total }'; }
... but it won't give you what you're asking for from dir -R *.jpg *.tif
, because that's not how ls -R
works. You might want to play around with the find
utility for that.
Best Answer
ls -l --block-size=M
will give you a long format listing (needed to actually see the file size) and round file sizes up to the nearest MiB.If you want MB (10^6 bytes) rather than MiB (2^20 bytes) units, use
--block-size=MB
instead.If you don't want the
M
suffix attached to the file size, you can use something like--block-size=1M
. Thanks Stéphane Chazelas for suggesting this.If you simply want file sizes in "reasonable" units, rather than specifically megabytes, then you can use
-lh
to get a long format listing and human readable file size presentation. This will use units of file size to keep file sizes presented with about 1-3 digits (so you'll see file sizes like6.1K
,151K
,7.1M
,15M
,1.5G
and so on.The
--block-size
parameter is described in the man page for ls;man ls
and search forSIZE
. It allows for units other than MB/MiB as well, and from the looks of it (I didn't try that) arbitrary block sizes as well (so you could see the file size as a number of 429-byte blocks if you want to).Note that both
--block-size
and-h
are GNU extensions on top of the Open Group'sls
, so this may not work if you don't have a GNU userland (which most Linux installations do). Thels
from GNU Coreutils 8.5 does support --block-size and -h as described above. Thanks to kojiro for pointing this out.