Score:0

alias not working as expected

ph flag

I am trying to get file types using an alias. I am using:

alias file-types="find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u"

However, it is not working:

When I type the command in the terminal it shows (which is exactly what I want):

azw
azw3
bz2
chm
epub
pdf

but when I use the alias, it shows something like:

./x.epub
./y.pdf
./z.pdf
.....

What can I do to make this command an alias.

24601 avatar
in flag
Your reference is from SO. Why not ask the question there? Generic programming questions are usually handled well there and unless ubuntu specific a better resource to use.
cn flag
The perl bit can be shorter (perhaps simpler?) -- `perl -ne 's/.+\.// && print'`
Score:3
hr flag

It's because the outer double quotes allow $1 to be evaluated by the shell when the alias is defined, so print $1 becomes plain print :

$ alias file-types="find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u"
$ alias file-types
alias file-types='find . -type f | perl -ne '\''print  if m/\.([^.\/]+)$/'\'' | sort -u'

Probably the simplest way to fix the quoting is to backslash-escape the $:

print \$1 if m/\.([^.\/]+)$/

however I'd recommend using a shell function in place of an alias for cases like this - see for example In Bash, when to alias, when to script, and when to write a function?

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.