Bash alias for complicated ls command does not work.

I'm trying to set up an alias in .bash_aliases to show just the filenames of the files in a directory, which the following command will do:

ls -l | grep ^- | awk '{print $NF}'

kjb.zip
ap.zip
tor.zip

However when I set up the following alias in .bash_aliases:

alias lf="ls -l | grep ^- | awk '{print $NF}'"

it only partially works. I get just the files listed but the -l longlisting too (permissions, owner, size, date, filename) and not just the filename on its own.

I'm guessing this is to do with the " and ' formatting but I can't seem to format it correctly. All the variations I've tried either fail with an awk error because the 'print' section isn't right, or do the same thing as already stated.

Any ideas? Thanks all.

Try this.

alias lf="ls -la|grep \^\-|awk '{print \$9}'"

Define a function:

lf() {
  perl -le'
    -f and print for glob "*"
    '
    }

With zsh it would be as simple as:

print -l -- *(.)

If you want to include dotfiles:

lf() {
  perl -le'
    -f and print for glob "* .?*"
    '
  }

With zsh:

print -l -- *(D.)

That gave me a series of empty lines, but the right amount for it to be one line per file in dir.

When I replaced the $9 with $NF it worked perfectly. Thanks for your help it is appreciated.

Cheers.

PS. Thanks too radoulov, but I prefer the alias solution now that it is working.

The reason it is not working is because it is expanding the $NF to an empty string when you define the alias:

>alias test_alias="ls -l | grep ^- | awk '{print $NF}'"

>alias test_alias
test_alias='ls -l | grep ^- | awk '\''{print }'\'

Try to excape the $ and it'll most likely work:

>alias test_alias="ls -l | grep ^- | awk '{print \$NF}'"
>alias test_alias
test_alias='ls -l | grep ^- | awk '\''{print $NF}'\'

Note that I tested this on ksh, not on bash.

soleil4716 - thanks that also works.

However I've descovered a problem with my original command. It does not work with filenames with spaces in them. Printing everything after the final space only.

EG. filename is:

this is a test.txt

will ouput just

test.txt

Damn, just when I thought it was working.

Any ideas how to fix this?

Thanks again.

Yes,
try the code I posted :slight_smile:

ok function it is. :slight_smile:

Thanks radoulov. Your function works perfectly.

However one thing I didn't mention before is that I was doing a second ls alias called lfq which added the ls switch -Q to wrap "" around the filenames so that files with spaces would be conveniently enclosed in "".

My attempts to modify your function to achieve this have failed dismally. Can it be done, if so, how?

Thanks again.

lfq() {
  perl -le'
    -f and print qq("$_") for glob "*"
    '
    }

To do this with unix "find" we'll need to know which Operating System you have.

Shell only:

lfq() {
  for f in *; do
    [ -f "$f" ] && 
      printf '"%s"\n' "$f"
  done
  }
lf() {
  for f in *; do
    [ -f "$f" ] && 
	  printf '%s\n' "$f"
  done
  }

---------- Post updated at 07:20 PM ---------- Previous update was at 07:11 PM ----------

Actually, I would recommend the shell only solution.

Thanks again radoulov, works a treat. Appreciate your time. :b:

methyl - no need radoulov's function solution is perfect and done. Thanks anyway.

---------- Post updated at 06:28 PM ---------- Previous update was at 06:20 PM ----------

Ok, I've tested those and they work perfectly as well.

Any particualar reason why they are preferable?

Cheers.

They are faster ...

Ok, done. That's a good enough reason for me! :smiley:

Thanks again for all your help.

You're welcome!

Hi.

I also like functions, but they can cause clutter and complexity. One way to address that is to use an autoload-like facility. Such a feature allows the functions to be placed in a separate directory, say ~/.functions, one function (or group of related functions) to a file. I think these ideas were first found in ksh.

Regrettably, autoload is not built into bash. However, you may have access to a set of functions that perform that task. In my main workstation system (Debian 5, lenny), these functions are in:

/usr/share/doc/bash/examples/functions

in the filenames containing the string autoload.

I have used them with (most recently) GNU bash 3.2.39 and they seem to work correctly.

So whenever you create a new function, or you find something interesting on the 'net, you can just drop the file in .functions, and you'll be set.

Looking briefly over my examples, the functions in .functions can be made to work with ksh (and probably zsh, but I have not tried that), assuming no bashisms are present.

Best wishes ... cheers, drl

You already have been provided fine solutions by radoulov, but I whipped this up in case it's of interest:

alias lf="ls -l | sed -n '/^-/s/^\([^ ]* *\)\{8\}//p'"

or

alias lf="ls -l | grep ^- | tr -s ' ' | cut -d' ' -f9-"

Cheers,
Alister