I must parse ls -Al output
and get file or directory name
ls -Al
output :
drwxr-xr-x 12 s162103 studs 12 march 28 2012 personal domain
drwxr-xr-x 2 s162103 studs 3 march 28 22:32 public_html
drwxr-xr-x 7 s162103 studs 8 march 28 13:59 WebApplication1
I should use only ls -Al | <something>
for example:
ls -Al | awk '{print $8}'
but this doesn't work because $8
is not name if there's spaces in directory name,it is a part of name. maybe there's some utilities that cut last name or delete anything before? I need to find any solution. Please, help!
EDITED: I know what parse ls -Al is bad idea but I should exactly parse it with construction above! No way to use some thing like this
for f in *; do
somecommand "$f"
done
Don't parse
ls -Al
, if all you need is the file name.You can put all file names in an array:
or you can iterate over the files directly:
If there is something specific from
ls
that you need, update your question to specify what you need.Based on @squiguy request on comments, I post my comment as an answer:
What about just this?
instead of
l
(L, the letter), a1
(one, the number). It will only list the names of the files.Hope awk works for you:
In case you're interested in sed:
It's also worth noting that
find
can do what you're looking for:Everything in this directory, equivalent to
ls
:Recursively, similar to
ls -R
:Only directories in a given directory :
md5sum
every regular file :How about this
ls -Al |awk '{$1=$2=$3=$4=$5=$6=$7=$8="";print $0}'
I know it's a cheap trick but since you don't want to use anything other thanls -Al
I cant think anything better...