Commands, the $PATH, and other Linux basics

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
User avatar
rednoah
The Source
Posts: 22898
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Commands, the $PATH, and other Linux basics

Post by rednoah »

What is a command? How do commands work? Why do commands not work?

Most importantly, what does command not found mean and how do I fix it?

Console Output: Select all

$ filebot -version
filebot: command not found
$ java -version
java: command not found
$ node -v
node: command not found
...



1. The $PATH

When you call a command such as filebot then the shell will check each folder that is in the $PATH for an executable (or symlink to an executable) with exactly that name.

Let's have a look at the $PATH:

Console Output: Select all

$ echo $PATH
/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin

:idea: For example, if you call filebot then the shell will check /bin/filebot, /sbin/filebot, /usr/bin/filebot and so on, until it finds an executable that matches your command, which is then executed. If there is no such executable, then you will get filebot: command not found.


The which command can tell us which executable is executed for a given command:

Console Output: Select all

$ which filebot
/usr/local/bin/filebot

The ls -l command can further tell us if this executable is a symlink and where that symlinks points to:

Console Output: Select all

$ ls -l `which filebot`
lrwxrwxrwx 1 root root 37 Jul 21 15:48 /usr/local/bin/filebot -> /var/packages/filebot/target/bin/filebot.sh



Let's say we have command that does not work:

Console Output: Select all

$ filebot -version
filebot: command not found

and an executable that does work:

Console Output: Select all

$ /var/packages/filebot/target/bin/filebot.sh -version
FileBot 4.8.2 (r5728) / Java(TM) SE Runtime Environment 1.8.0_181 / Linux 3.2.40 (arm)

We can easily make this work by symlinking our filebot executable into the $PATH:

Shell: Select all

ln -sf /var/packages/filebot/target/bin/filebot.sh /usr/local/bin/filebot

Let's test our new symlink first:

Console Output: Select all

$ /usr/local/bin/filebot -version
FileBot 4.8.2 (r5728) / Java(TM) SE Runtime Environment 1.8.0_181 / Linux 3.2.40 (arm)

Finally, confirm that the filebot command can now be found:

Console Output: Select all

$ filebot -version
FileBot 4.8.2 (r5728) / Java(TM) SE Runtime Environment 1.8.0_181 / Linux 3.2.40 (arm)


:idea: Alternatively, instead of symlinking executables into the $PATH, you can also add additional folders to the $PATH in your script:

Shell: Select all

export PATH="/var/packages/filebot/target/bin:$PATH"
filebot.sh -version



3. How do I find an executable?

Let's say we're unfamiliar with the device we're using. We know that we have downloaded or installed some package somewhere, but we don't know where exactly. The find command can help us find out.

e.g. search the entire filesystem for a file called java:

Shell: Select all

find / -type f -name "java" 2> /dev/null
:idea: We use 2> /dev/null to redirect and ignore error output, since find / would otherwise print lots of confusing error messages (because / refers to the entire filesystem including system files we don't have access to).




4. Read the Shell Scripting Tutorial

Learning basic shell scripting will take a few days. However, it's an extremely useful skill that will serve you well for the rest of your live. You don't need to be a mathematician to find basic math useful in daily life. You don't need to be a programmer to find basic shell scripting useful in daily life. ;)

:arrow: https://www.shellscript.sh/
:idea: Please read the FAQ and How to Request Help.
Post Reply