Commands, the $PATH, and other Linux basics

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

Commands, the $PATH, and other Linux basics

Post by rednoah » 03 Aug 2018, 10:32

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?

Code: 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:

Code: Select all

$ echo $PATH

Code: Select all

/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:

Code: 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:

Code: Select all

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


2. How to symlink an executable into the $PATH?

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

Code: Select all

$ filebot -version
filebot: command not found
and an executable that does work:

Code: Select all

$ /var/packages/filebot/target/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:

Code: Select all

$ ln -sf /var/packages/filebot/target/filebot.sh /usr/local/bin/filebot
Let's test our new symlink first:

Code: 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:

Code: Select all

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


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:

Code: 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).
:idea: Please read the FAQ and How to Request Help.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests