Pass wildcard to alias
Asked Answered
C

4

5

I use a modifies list command as alias (in KSH):

alias ltf='ls -lrt -d -1 $PWD/*'

So the command ltf displays something like this:

-rw-r--r-- 1 myuser mygroup 0 Apr 18 12:00 /usr/test.txt
-rw-r--r-- 1 myuser mygroup 0 Apr 18 12:00 /usr/test.log

Now I want to use wildcards. But using ltf *.log does not work.

What is the best way to achieve that?


Update: I want to specify my question because the answers does not solve my problem so far: the command ls -lrt -d -1 $PWD/* executes a list command with some options AND it displays the full path, which is an important point for me.

Unfortunately the alias approach does not allow wildcard parameters. My goal is to make this possible. Probably it is the best way to create the command as a function. This is mentioned in the answers, but it does not work yet (see comments).

Any ideas?

Cracow answered 18/4, 2011 at 7:24 Comment(0)
D
5

Use a shell function instead of an alias:

function ltf {
  if [ -z "$1" ]; then
    ls -lrtd1 ${PWD}/*
  else
    ls -lrtd1 $1
  fi
}
Dufour answered 18/4, 2011 at 7:30 Comment(5)
Out of interest, is there a similar notation in ksh like this one for bash: [[ -z "$1" ]] && ls -lrtd1 "$PWD/*" || ls -lrtd1 "$1"Scripture
I think it should be ls -lrtd1 ${PWD}/$1 after the else statement. Thanks!Cracow
@TechnoCore: no. [ -z "$1" ] is true if "$1" is the empty string, i.e. there is no argument.Dufour
the PWD command should also be executed if there is an argument; ls -lrtd1 $1 does not show the full pathCracow
After some testing I think that this approach does not solve the problem. If the wildcard (eg. *.log) returns more then one file, only one file is listed by ltf *.log.Cracow
A
3

Your problem is that the wildcards are getting expanded by the shell in your current directory, not in $PWD. You can solve this by using a shell function rather than an alias and doing some quoting (I use $HOME in the example for my convenience) - put this in .bashrc:

ltf() {
    ls $HOME/$*
}

and then:

$ ltf '*.log'
Aggappora answered 18/4, 2011 at 7:33 Comment(0)
E
2

try

alias ltf='ls -lrt -d -1 $1'

or if you want many params

alias ltf='ls -lrt -d -1 $@'
Ewold answered 18/4, 2011 at 7:32 Comment(1)
This doesn't work, $1 etc aren't recognized by aliases. The reason you might get results anyway is $1 will be an empty string, so ltf path will be ls -lrt -d -1 path. So you should use alias ltf='ls -lrt -d -1' for the same effect, and if that doesn't work, you will need to use a function.Injury
V
0

As mentioned by others, wildcard expansion is done before calling shell functions, but not so with an alias.

If you don't need to change the input (which the OP does), this would work fine:

alias ltf='ls -lrt -d -1'

To achieve what the OP wants, within a shell function you would need to use the $@ operator to access the set of files resulting from the wildcard expansion, which you would then need to prepend:

ltf() 
{ 
    #Prepend $PWD to the start of every input
    set -- "${@/#/$PWD/}"   

    #Run the command on the prepended input
    ls -lrt -d -1 $@;
}
Vexatious answered 25/2, 2021 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.