2

I am looking for a way to edit the source code of common Linux commands (passwd, cd, rm, cat)

Ex. Every time the 'cat' command is called (by any user), it performs its regular function, but also prints "done" to stdout after.

0andriy
  • 4,183
  • 1
  • 24
  • 37

2 Answers2

3

If you're only looking to "augment" the commands as in your example, you can create e.g. /opt/bin/cat.sh:

/bin/cat && echo "done"

and then either:

change the default PATH (in /etc/bash.bashrc in Ubuntu) as follows:

PATH=/opt/bin:$PATH

or rename cat to e.g. cat.orig and move cat.sh to /bin/cat. (If you do the latter, your script needs to call cat.orig not cat).

If you want to actually change the behavior, then you need to look at the sources here: https://ftp.gnu.org/gnu/coreutils/ and then build them and replaces them.

All this assumes, of course, that you have root permissions, seeing how you want to change that behavior for any user.

root
  • 5,528
  • 1
  • 7
  • 15
1

The answer to how to modify the source is not to, unless you have a REALLY good reason to. There’s a plethora of reasons why you shouldn’t, but a big one is that you should try to avoid modifying the source of anything that could receive an update. The update breaks, if not erases, your code and you’re left with a lot of work.

Alternatively, you can use things like Alias for quick customizations and write scripts that call and rely upon the command being available, instead of worrying about its implementation. I’ve over explained, but that’s because I’m coming to you as someone with only a little experience with Linux but much more in Development, and what I’ve said extends beyond an Operating Systems CLI capabilities and lands further into general concepts of development.

CTheCheese
  • 362
  • 2
  • 12