ls and more and path as parameter

General support questions
Post Reply
wos
Posts: 5
Joined: 2015/03/17 14:18:27

ls and more and path as parameter

Post by wos » 2015/03/25 13:40:41

Hi,

I've an alias for ls command (dir). Also I'd like to have my output in color. First I'd the problem, that if I pipe ls into more, all colors are gone. A solution to overcome this is to make the alias like that ...
alias dir='ls -Al --color | more'

My first impression was: Ok ... running. But then i realized a problem, if i want to list a different directory than I'm located in! Assume that I'm in /etc and want to have the directory contents of /bin I'd type then ...
dir /bin
... will give me the contents of my actual directory /etc. I understand thats a problem because the ls command is processed first and then piped into more. But maybe somebody knows a workaround/solution for this problem.

Thx

giulix63
Posts: 1305
Joined: 2014/05/14 10:06:37
Location: UK

Re: ls and more and path as parameter

Post by giulix63 » 2015/03/25 16:02:43

There's none. But you can use a script instead, and call it dir:

Code: Select all

#!/bin/bash
ls -Al --color $1 | more
$1 will be replaced by whatever you specify as the first parameter to the command (use $* for multiple parameters). Make it executable and run it like this:

Code: Select all

chmod 754 dir
./dir /bin/
It will then produce the expected result. If you place it in a directory listed in the $PATH variable (or you add the path where dir is located to it), you will be able to run it without specifying its full path, as in

Code: Select all

dir /bin/
P.S. Mind that a system dir command already exists in /usr/bin which is likely to be executed in place of yours.
Root is evil: Do not use root (sudo) to run any of the commands specified in my posts unless explicitly indicated. Please, provide the necessary amount of context to understand your problem/question.

wos
Posts: 5
Joined: 2015/03/17 14:18:27

Re: ls and more and path as parameter

Post by wos » 2015/03/26 08:09:21

Thx, this works like a charm. To use it like an alias I understand that I've to put it somewhere where my $PATH is pointing in. $PATH on my system is here:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

So I tried to put my dir file (no extension) in /root/bin and was surprised that /root/bin is not existing on the hd. It's in the $PATH but not there!? Anyway ... I created /root/bin and put the dir script there. But it was also not working. Then i looked for other "dir" files and found some

/usr/share/info/dir (looks like a text file)
/usr/bin/dir (it's the other dir executable)

Edit:
giulix63 wrote:P.S. Mind that a system dir command already exists in /usr/bin which is likely to be executed in place of yours.
Sorry ... I'd read over this

Whats about that second dir file? I thought linux has no dir command? To overcome this I put my dir script /usr/local/sbin (the very first entry of $PATH) and after a logoff/logon the behavior is now almost as i would expect it. But there remains a final, strategical question:

Wouldn't it be better to make a extra directory e.g. /var/commonsripts (or is there a better place) and try to modify the global path variable to enable invoking that script for every user and where to modify the global $PATH best, in a way that it would survive any centos updates in the future?

giulix63
Posts: 1305
Joined: 2014/05/14 10:06:37
Location: UK

Re: ls and more and path as parameter

Post by giulix63 » 2015/03/26 08:49:33

Absolutely spot on! That's what /usr/local is for. Some sysadmins make that a dedicated partition so that it survives a reinstallation too. See also FHS [wikipedia]
Root is evil: Do not use root (sudo) to run any of the commands specified in my posts unless explicitly indicated. Please, provide the necessary amount of context to understand your problem/question.

wos
Posts: 5
Joined: 2015/03/17 14:18:27

Re: ls and more and path as parameter

Post by wos » 2015/03/26 12:47:52

Thx a lot. Great hint with the FHS ... will memorize it 8). I asked me all the time why the filesystem is as it is and then there are some different distributions and some make thinks different than others.

Post Reply