Tag Archives: awk

AWK: how to get filenames from ls -la command

example:
$ ls -la
итого 8
drwxrwxr-x 2 alter alter 4096 июл 10 17:18 .
drwxrwxr-x. 56 alter alter 4096 июл 10 16:44 ...
— rw-rw-r-- 1 alter alter 13 июл 10 16:44 log file name for test
— rw-rw-r-- 1 alter alter 5 июл 10 16:44 two words
— rw-rw-r-- 1 alter alter 40 июл 10 16:44 words.txt

1
ls -la | awk '{ for (i=9; i<=NF; i++) if( $i != $NF ) str = str " " $i; else str = str " " $i "\n"}; END {print str}'

or

1
ls -la | awk '{str=""; for (i=9; i<=NF; i++) if( $i != $NF ) str = str " " $i; else str = str " " $i; print str};'

result:
.
...
log file name for test
two words
words.txt

if we need to get names including filesize

1
ls -la | awk '{str = ""; for (i=9; i<=NF; i++) if( $i != $NF ) str = str " " $i; else str = $5 " " str " " $i; print str};'

result:
4096 .
4096 ...
13 log file name for test
5 two words
40 words.txt