Need help with ls command.

Hi,

ls log/*.err

Current Output:
log/tmp.err
log/ser.err

But i want Output without the parent folder name and without changing directory like you see here...

ls log/*.err

Desired Output:
tmp.err
ser.err

Is there any flag with ls that can help get the desired output ?

The man page of ls is as accessible to you as it is to us, so wouldn't it be nice and wise to consult that first?

As I don't know of any such flag, you'll have two options: 1) cd to the directory first, then ls ; 2) pipe ls 's result into an additional processing step and use a filter or a parameter expansion in there .

Hello mohtashims,

Considering that your files will NOT have spaces in their names. Could you please try following and let me know if this helps you.

ls log/*.err | awk '{gsub(/[[:space:]]/,"\n",$0);sub(/.*\//,X,$0);print}'

Thanks,
R. Singh

Below is nice and easy

ls log/*.err | awk -F "/" '{print $2}'
( cd log; ls *.err )

or, in an interactive shell:

( cd log; ls -1 *.err )

The basename would work for you, too, such as

ls log/*err | xargs -l basename