LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-24-2014, 06:30 AM   #1
inferno1980
LQ Newbie
 
Registered: Oct 2014
Posts: 12

Rep: Reputation: Disabled
Alias or Function of LS


Hi
How can I create alias (or function) of following command;
Code:
ls -lh --color | awk '{printf("%5s %s\n",$5,$9)}'
because putting following:
Code:
alias ls='ls -hl --color | awk '{printf("%5s %s\n",$5,$9)}''
in .bashrc is not working.
Thanks

Last edited by inferno1980; 10-24-2014 at 06:32 AM.
 
Old 10-24-2014, 09:36 AM   #2
Ztcoracat
LQ Guru
 
Registered: Dec 2011
Distribution: Slackware
Posts: 6,408
Blog Entries: 6

Rep: Reputation: Disabled
Hi:

These articles have other alternative's to using alias with a cmd-
Code:
example:
alias newcommand='yourcommand -arguments'
http://linuxreviews.org/quicktips/alias/

You can use alias with a script if that helps.
http://www.cyberciti.biz/tips/bash-a...inux-unix.html

Using bash with alias and awk-
http://stackoverflow.com/questions/7...as-or-function
http://superuser.com/questions/69641...an-awk-command

Hope that helps.
 
Old 10-24-2014, 10:54 AM   #3
inferno1980
LQ Newbie
 
Registered: Oct 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
It means that I have to create a function. I have created following function:
Code:
ls (){ ls -lh --color | awk '{printf("%5s %s\n",$5,$9)}' ;}
but its not working and uplon executing command 'ls', the system becomes unresponsive for a while.
PS. Although 'ls' is already a command, but I still want to create a function with name 'ls'. I hope it wouldn't do any harm.
 
Old 10-24-2014, 11:50 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Well I assume you are aware that once you alias 'ls' this way you won't get your normal output when issuing the 'ls' command.

Secondly I am hoping the command works better for you than me as the output was particularly useless and not what I would be expecting seeing the awk command implies
it would return 2 items ... here is a sample of what I get when I run your command:
Code:
$ ls -lh --color | awk '{printf("%5s %s\n",$5,$9)}'
      
  16K 
 4.0K 
 4.0K 
 4.0K 
    0 
 2.0K
As for creating an alias or function I would see no real reason why it wouldn't work. Try doing it in multiple steps to confirm where you are getting an issue?

A side note, I presume you have already checked that 'ls' is not already aliased to something else which may conflict with what you are doing??
 
Old 10-24-2014, 05:01 PM   #5
inferno1980
LQ Newbie
 
Registered: Oct 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
'ls' working here fine, here is the output;
Code:
1:58:25 ~>ls -lh --color | awk '{printf("%5s %s\n",$5,$9)}'
 4.0K audio
 4.0K Desktop
 4.0K downloads
 4.0K picture
 4.0K software
 4.0K text
 4.0K video
ls is not conflicting with any other alias as i have disabled the alias 'ls --color'.
How can I set it in multiple steps?
 
Old 10-24-2014, 11:39 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Well you mention that it hangs so assign just the ls part to an alias and confirm it works, then do the same for the awk part (might have to pipe an echo to it).

I would also mention that whilst your -l is returning the expected number of columns (as mine has only eight) that if any of your file or directory names contain whitespace, your awk will come unstuck
and you will only get the first word
 
Old 10-25-2014, 12:05 AM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,961

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by inferno1980 View Post
It means that I have to create a function. I have created following function:
Code:
ls (){ ls -lh --color | awk '{printf("%5s %s\n",$5,$9)}' ;}
but its not working and uplon executing command 'ls', the system becomes unresponsive for a while.
PS.
Of course. The function is just recursively invoking itself. You need to use the command builtin or use the full pathname to bypass the function lookup.
Code:
ls (){ command ls -lh --color | awk '{printf("%5s %s\n",$5,$9)}' ;}
ls (){ /bin/ls -lh --color | awk '{printf("%5s %s\n",$5,$9)}' ;}
The other problem you will have is that there is no way to pass any arguments to this function. That's fine, as long as you never want a listing for anything but the current directory.
Code:
ls (){ command ls -lh --color "$@" | awk '{printf("%5s %s\n",$5,$9)}' ;}
But, IMHO it's pretty silly to replace a basic system command like ls with some special-purpose function.
 
1 members found this post helpful.
Old 10-25-2014, 07:49 AM   #8
inferno1980
LQ Newbie
 
Registered: Oct 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
This function did the trick:
Code:
ls (){ /bin/ls -lh --color | awk '{printf("%5s %s\n",$5,$9)}' ;}
Thanks a lot
 
Old 10-25-2014, 11:22 AM   #9
inferno1980
LQ Newbie
 
Registered: Oct 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
There is a flaw
'ls' is not showing the part of the file name name which is after space i.e. if a file is named 'mobile videos', ls is only showing 'mobile'. How can I fix it?
 
Old 10-25-2014, 12:35 PM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,961

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
awk doesn't have a simple way to reference "the rest of the line starting with field 9." Simplest is to use a regex to separate the first 8 fields from the rest.
Code:
ls (){ LC_TIME=C /bin/ls -lh --color | awk --re-interval '{match($0,"([^ ]+ +){8}(.*)",aa); printf("%5s %s\n",$5,aa[2])}' ;}
Analysis of that regex is left as an exercise.

Note that I have also passed "LC_TIME=C" to ls since your locale settings can affect whether the date is printed as one field or two (i.e., "Oct 25" vs. "2014-10-25").

Last edited by rknichols; 10-25-2014 at 12:41 PM. Reason: set LC_TIME vs. LC_ALL
 
Old 10-25-2014, 02:57 PM   #11
inferno1980
LQ Newbie
 
Registered: Oct 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
awk doesn't have a simple way to reference "the rest of the line starting with field 9." Simplest is to use a regex to separate the first 8 fields from the rest.
Code:
ls (){ LC_TIME=C /bin/ls -lh --color | awk --re-interval '{match($0,"([^ ]+ +){8}(.*)",aa); printf("%5s %s\n",$5,aa[2])}' ;}
Analysis of that regex is left as an exercise.

Note that I have also passed "LC_TIME=C" to ls since your locale settings can affect whether the date is printed as one field or two (i.e., "Oct 25" vs. "2014-10-25").
Thanks a lot man
Now its working perfectly. If you live near to me, I will definitely invite you to have a coffee with me
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash: alias vs. shell function bartonski Linux - General 7 04-07-2014 12:29 AM
Using an alias inside a function in bash martindl Linux - Newbie 1 06-09-2011 12:55 AM
[SOLVED] Using an alias or function without the newline? gctaylor1 Programming 3 05-02-2010 02:53 AM
[SOLVED] [bash] alias + function = weird... RaptorX Programming 4 08-01-2009 07:36 PM
alias or function in .bashrc, which sends command to background ngomong Linux - General 1 04-23-2002 10:50 PM


All times are GMT -5. The time now is 04:47 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration