Must parse ls -Al output and get file or directory name
6,790
You should never attempt to parse ls
for a list of filenames. The bash wiki has an entire article about why you shouldn't parse the output of ls
. Having files with spaces is one of the biggest reasons.
You want to use a glob
instead:
printf '%s\n' *
Your question only indicates that you want to print the filename, but I suspect you also want to take an action on it. In that case, you want to iterate over the glob:
for f in *; do
somecommand "$f"
done
Related videos on Youtube
Author by
Alex Zern
Updated on September 18, 2022Comments
-
Alex Zern over 1 year
I must parse
ls -Al
output and get file or directory namels -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?-
jordanm about 11 yearsIt's also cross-posted to stackoverflow.com/questions/15721925/…
-