7

Every once in awhile, I find myself needing to list files in a directory that I don't have execution permission on. So I do a sudo:

>sudo ll /opt/mydir
[sudo] password for civfan:
sudo: ll: command not found

And then I say, "D'oh!" and switch to ls. And then remember I really wanted ls -l. And then remember I REALLY wanted ls -al.

Can someone save me from myself?

I see that ll is an alias for my user:

>alias ll
alias ll='ls -alF'

How do I make sudo ll work? Presumably I need an alias for root... if so, how do I set aliases for root?

EDIT: My guess is trying to run sudo ll is so common that this question deserves to stand alone from the suggested duplicate.

UPDATE: root already has an ll alias!

civfan@civfan:~>sudo -s
root@civfan:~>alias ll
alias ll='ls -alF'

And yet sudo ll doesn't work...

UPDATE 2 - answer:

The answer in the suggested duplicate question fits my workflow the best, and I don't see any downsides. I wanted to note it here:

alias sudo="sudo "

From man bash:

If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

CivFan
  • 457
  • Can't you just add 'sudo ls -alF' to the alias? or you can log as a root and then edit your file where the aliases are ~/.bashrc and add the alias – VaTo Jun 15 '15 at 17:55
  • @SaulOrtega I don't always want to run ll as sudo. To your second point, can you elaborate in an answer? – CivFan Jun 15 '15 at 18:11
  • In my second answer I meant log as root with (su) then open your alias file depending on your system and add the same alias you have there I mean alias ll. then when you want to execute that command it will prompt you for a password. – VaTo Jun 15 '15 at 18:14
  • @SaulOrtega After switching to root using either su or sudo -s, it turns out the ll alias already exists. I don't know why sudo ll doesn't work, then. – CivFan Jun 15 '15 at 18:22
  • does it work when you are logged in as a root? – VaTo Jun 15 '15 at 18:24
  • @SaulOrtega ll works when switched to root user. – CivFan Jun 15 '15 at 18:27
  • 1
    sudo -c "source /root/.bashrc && ll" – VaTo Jun 15 '15 at 18:38
  • ll is a shell alias, sudo does not run the command in a shell. – ctrl-alt-delor Jun 15 '15 at 19:18
  • @richard Ahhh ok. That makes sense. – CivFan Jun 15 '15 at 19:19

1 Answers1

4

-Edit for adult users- This alias description feels a bit like magic and looks cool, when you want to impress your friends, but they aren't! Remember that you are using your shell interactively, because it looks so cool and you have so many great commands to combine, but when you grow up, or your software does, you possibly want to automate a few things.

When you automate things with sudo, it is best to not rely on, tricks to help bash users in interactive sessions.

Of course you can simply add such features to the sudo command on your own:

mv /usr/bin/sudo /usr/bin/sudo_orig
ed /usr/bin/sudo
a
#!/bin/sh
cmd="$*"
if [ $1.cmd = "ll.cmd" ]
then
  shift
  cmd="ls -alF $*"
elif [ $1.cmd = ...more definitions... ]
then ...
fi
sudo_orig $cmd
.
w
q
chmod 755 /usr/bin/sudo

This is how you can create a toolset that matches your needs, if you really think you need it this way ;) -END-OF-EDIT-

A shell alias is used as an interactive part of the shell. When you call sudo you leave the shell and execute a command.

You could either enter an interactive shell with sudo -s, define your aliases and use them, or you have to rewrite your aliases as commands or functions, for example use a folder /root/alias-cmds and write the command /root/alias-cmds/ll:

echo "ls -alF" > /root/alias-cmds/ll; chmod 700 /root/alias-cmds/ll;

now you can use

sudo 'PATH=$PATH:/root/alias-cmds ll' 

Another option is to define

alias sudoll='sudo ls -alF'

and use sudoll instead of sudo ll.

ikrabbe
  • 2,163
  • 1
    I really want sudo ll to work, since that fits the typical workflow of sudo x when just x doesn't have permission. I'd also prefer it to work with something simpler than adding a new command to root's path. – CivFan Jun 15 '15 at 19:24
  • BTW: The use of additional commands is quite more stable and more flexible in different situations. I guess it is quite simple to generate these commands from your aliases, so you don't even need to write them by hand! – ikrabbe Jul 17 '20 at 15:03