Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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??
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
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.
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?
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.
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 11:41 AM.
Reason: set LC_TIME vs. LC_ALL
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.