LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-27-2015, 09:22 AM   #1
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,726

Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Question Add long command to an alias.


Hi all at LQ.

How do I add this long command to an alias?

Code:
alias ck=sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-z]+//')")
If I add as above I get the errors:
Code:
bash: alias: apt-get: not found
bash: alias: purge: not found
If I add single quotes at the start and end, it only partially works.

ck = clean kernels. ^_^
 
Old 10-27-2015, 10:20 AM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
try this:

Code:
ck() {
  sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-z]+//')")
}
 
1 members found this post helpful.
Old 10-27-2015, 10:21 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,910

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
write a shell script or a function and use that instead of this alias.
 
Old 10-28-2015, 01:49 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
LinusStallman, just to elaborate, aliases are not designed to handle this kind of script.
and a script it is, though it is a one-liner.

you can really just add the function into your .bashrc or where-ever you have your aliases.
though i'd suggest to change it to a multiliner instead of a oneliner with pipes.
 
Old 10-28-2015, 05:37 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
This is also a good time to point out that alias definitions are parsed when they are created (ie at login or equiv), not when called.

As above, use a fn instead.
 
Old 10-28-2015, 08:18 AM   #6
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by ondoho View Post
though i'd suggest to change it to a multiliner instead of a oneliner with pipes.
Why is that?
And how would you do it?
Besides from redirecting output to a file and then reread it with the next command? Or do you mean just multiple sed commands?
 
Old 10-28-2015, 08:48 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,910

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
something like that:
Code:
ck() {
  VAR=$(uname -r | sed -r 's/-[a-z]+//')
  VAR2=$(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$VAR")
  sudo apt-get purge $VAR2
}
(not tested)
 
Old 10-28-2015, 10:37 AM   #8
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
That makes sense. Thanks pan64
 
Old 10-31-2015, 05:39 AM   #9
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,726

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Thumbs up

Quote:
Originally Posted by thesnow View Post
try this:

Code:
ck() {
  sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-z]+//')")
}
Hi thesnow.

This worked fine. Thank you.
 
Old 10-31-2015, 02:26 PM   #10
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,726

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Thumbs up

Hi chrism01, ondoho, pan64, thesnow, & zhjim.

Thank you all for the info.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add alias to my profile ? fatou Linux - Newbie 8 04-14-2014 06:09 PM
How to add new alias nasridine Linux - Newbie 3 02-18-2010 11:47 AM
Add Second Gateway for Alias ETH0 linchat Linux - Networking 2 01-06-2006 04:10 PM
Create alias or add to $PATH ryedunn Linux - Newbie 3 08-18-2004 10:42 AM
add an alias gubak Linux - Networking 3 08-12-2004 02:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration