How to run a file in a different directory
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
4
down vote
favorite
There is a file that I would like execute in a different folder, under about four sub-directories.
For example:
My pwd
may be /home/directoryA
. However, the file I would like
execute may be in directoryD
. At the moment if I want to execute the file I would need to go to cd /home/directoryA/directoryB/directoryC/directoryD/
and then execute the file. Or I might have do something like src /directoryA/directoryB/directoryC/directoryD/somefile
Is it possible to execute the file without actually being in the directory where the file is?
Is there a shortcut way of executing the file
somefile
without going into the directory?
command-line
add a comment |Â
up vote
4
down vote
favorite
There is a file that I would like execute in a different folder, under about four sub-directories.
For example:
My pwd
may be /home/directoryA
. However, the file I would like
execute may be in directoryD
. At the moment if I want to execute the file I would need to go to cd /home/directoryA/directoryB/directoryC/directoryD/
and then execute the file. Or I might have do something like src /directoryA/directoryB/directoryC/directoryD/somefile
Is it possible to execute the file without actually being in the directory where the file is?
Is there a shortcut way of executing the file
somefile
without going into the directory?
command-line
1
Beside the point, but note thatdirectoryD
andDirectoryD
are not the same.
â wjandrea
May 17 at 3:33
2
@wjandrea Plus/home/directoryA
is probably meant to be/home/ME/directoryA
. Also thecd /directoryA/directoryB....
is missing the~
prefix. Also thecd
ends in.../somefile
which is impossible to change directory to a file. There are lots of flaws that need to be overlooked.
â WinEunuuchs2Unix
May 17 at 4:40
I've updated my answer based on new comments read under other answers.
â WinEunuuchs2Unix
May 17 at 12:32
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
There is a file that I would like execute in a different folder, under about four sub-directories.
For example:
My pwd
may be /home/directoryA
. However, the file I would like
execute may be in directoryD
. At the moment if I want to execute the file I would need to go to cd /home/directoryA/directoryB/directoryC/directoryD/
and then execute the file. Or I might have do something like src /directoryA/directoryB/directoryC/directoryD/somefile
Is it possible to execute the file without actually being in the directory where the file is?
Is there a shortcut way of executing the file
somefile
without going into the directory?
command-line
There is a file that I would like execute in a different folder, under about four sub-directories.
For example:
My pwd
may be /home/directoryA
. However, the file I would like
execute may be in directoryD
. At the moment if I want to execute the file I would need to go to cd /home/directoryA/directoryB/directoryC/directoryD/
and then execute the file. Or I might have do something like src /directoryA/directoryB/directoryC/directoryD/somefile
Is it possible to execute the file without actually being in the directory where the file is?
Is there a shortcut way of executing the file
somefile
without going into the directory?
command-line
edited May 17 at 12:09
![](https://i.stack.imgur.com/ulbom.jpg?s=32&g=1)
![](https://i.stack.imgur.com/ulbom.jpg?s=32&g=1)
ToxicMender
143
143
asked May 16 at 23:07
Carltonp
211
211
1
Beside the point, but note thatdirectoryD
andDirectoryD
are not the same.
â wjandrea
May 17 at 3:33
2
@wjandrea Plus/home/directoryA
is probably meant to be/home/ME/directoryA
. Also thecd /directoryA/directoryB....
is missing the~
prefix. Also thecd
ends in.../somefile
which is impossible to change directory to a file. There are lots of flaws that need to be overlooked.
â WinEunuuchs2Unix
May 17 at 4:40
I've updated my answer based on new comments read under other answers.
â WinEunuuchs2Unix
May 17 at 12:32
add a comment |Â
1
Beside the point, but note thatdirectoryD
andDirectoryD
are not the same.
â wjandrea
May 17 at 3:33
2
@wjandrea Plus/home/directoryA
is probably meant to be/home/ME/directoryA
. Also thecd /directoryA/directoryB....
is missing the~
prefix. Also thecd
ends in.../somefile
which is impossible to change directory to a file. There are lots of flaws that need to be overlooked.
â WinEunuuchs2Unix
May 17 at 4:40
I've updated my answer based on new comments read under other answers.
â WinEunuuchs2Unix
May 17 at 12:32
1
1
Beside the point, but note that
directoryD
and DirectoryD
are not the same.â wjandrea
May 17 at 3:33
Beside the point, but note that
directoryD
and DirectoryD
are not the same.â wjandrea
May 17 at 3:33
2
2
@wjandrea Plus
/home/directoryA
is probably meant to be /home/ME/directoryA
. Also the cd /directoryA/directoryB....
is missing the ~
prefix. Also the cd
ends in .../somefile
which is impossible to change directory to a file. There are lots of flaws that need to be overlooked.â WinEunuuchs2Unix
May 17 at 4:40
@wjandrea Plus
/home/directoryA
is probably meant to be /home/ME/directoryA
. Also the cd /directoryA/directoryB....
is missing the ~
prefix. Also the cd
ends in .../somefile
which is impossible to change directory to a file. There are lots of flaws that need to be overlooked.â WinEunuuchs2Unix
May 17 at 4:40
I've updated my answer based on new comments read under other answers.
â WinEunuuchs2Unix
May 17 at 12:32
I've updated my answer based on new comments read under other answers.
â WinEunuuchs2Unix
May 17 at 12:32
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
6
down vote
No you don't need to use:
cd home/directoryA/directoryB/directoryC/DirectoryD
./somefileÃÂ
You can simply run the command by prefixing it with its path:
/home/directoryA/directoryB/directoryC/DirectoryD/somefile
Because you are already in the /home/directoryA
you can use the current directory shortcut .
and run the command like this:
./directoryB/directoryC/DirectoryD/somefile
I noticed OP has expanded scope via comments under other answers. Here is some additional information:
- To find out where
somefile
is located use:locate somefile
. - If
somefile
was added today you need to first update the locate database by runningsudo updatedb
. - When there are multiple versions of
somefile
located in the PATH you can find out which one is executed first usewhich somefile
. - If you want to run
somefile
without specifying a directory name in front put it in the path. To check the path useecho $PATH
. Common path locations to putsomefile
are/usr/local/bin
(if it uses sudo powers) and/home/your_user_name/bin
(you might have to create the directory first). - You can also add
/home/directoryA/directoryB/directoryC/DirectoryD/
to your path but that would be highly unusual. However you could then simply typesomefile
no matter what directory you are in and it will run. - Of course
somefile
must be executable which you set with the command:chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile
add a comment |Â
up vote
1
down vote
Sure! If somefile is marked as executable, you can run it with
~/directoryA/directoryB/directoryC/DirectoryD/somefile
Want to know if somefile is executable? Go to its directory and run
find . -maxdepth 1 -perm -111 -type f
to see all the executables in that directory.
Ok, can you let me know how I would execute the file if IâÂÂm not already in directory A, but in some other directory.
â Carltonp
May 16 at 23:25
Also, can you let me know how to find out what directory a file is located in?
â Carltonp
May 16 at 23:27
2
If you want to know if it's executable, why not runls -l /directoryA/directoryB/directoryC/DirectoryD/somefile
?
â wjandrea
May 17 at 3:31
@Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
â K7AAY
May 17 at 20:13
add a comment |Â
up vote
1
down vote
The $PATH
shell variable contains the directories where the executables are searched. Add the directory containing your executable into $PATH
and it can be executed from anywhere.
Add in .bashrc
file:
export PATH=$PATH:/../your_directory
/..
is the same as/
. Is that supposed to be/...
?
â wjandrea
May 24 at 1:59
add a comment |Â
up vote
-2
down vote
There is another way (somehow not already mentioned) by using shell profile (.bashrc
, .zshrc
etc).
You can run:
# Assuming it is a script you made, changing file permission to make it executable
chmod a+x ~/directoryA/directoryB/directoryC/directoryD/somefile
# Appending your shell profile with an alias to run the script from wherever you are
echo "alias somename="source ~/directoryA/directoryB/directoryC/directoryD/somefile"" >> ~/.profile
# replace ~/.profile with config file of whichever shell you use
# Also replace source with python if the script is a python script or whichever interpreter it requires for execution
# Make sure you have #!/usr/bin/env python or #!/path/to/interpreter on your computer as the first line of your script
Although above method allows the script to be run from anywhere you should make sure the script doesn't depend on the pwd
(present working directory) for execution (unless intended).
You can then run the script as executable in any directory, like:
somename
P.S.:
As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
@peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
â David Foerster
May 17 at 22:15
-1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in.bashrc
, not.profile
. And why create an alias instead of adding the directory to the PATH?
â wjandrea
May 24 at 2:18
Oh, I think there may have been confusion around OP's use ofsrc
in the question. I don't think that's supposed to mean the same thing assource
.
â wjandrea
May 24 at 2:24
i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
â ToxicMender
May 24 at 21:07
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
No you don't need to use:
cd home/directoryA/directoryB/directoryC/DirectoryD
./somefileÃÂ
You can simply run the command by prefixing it with its path:
/home/directoryA/directoryB/directoryC/DirectoryD/somefile
Because you are already in the /home/directoryA
you can use the current directory shortcut .
and run the command like this:
./directoryB/directoryC/DirectoryD/somefile
I noticed OP has expanded scope via comments under other answers. Here is some additional information:
- To find out where
somefile
is located use:locate somefile
. - If
somefile
was added today you need to first update the locate database by runningsudo updatedb
. - When there are multiple versions of
somefile
located in the PATH you can find out which one is executed first usewhich somefile
. - If you want to run
somefile
without specifying a directory name in front put it in the path. To check the path useecho $PATH
. Common path locations to putsomefile
are/usr/local/bin
(if it uses sudo powers) and/home/your_user_name/bin
(you might have to create the directory first). - You can also add
/home/directoryA/directoryB/directoryC/DirectoryD/
to your path but that would be highly unusual. However you could then simply typesomefile
no matter what directory you are in and it will run. - Of course
somefile
must be executable which you set with the command:chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile
add a comment |Â
up vote
6
down vote
No you don't need to use:
cd home/directoryA/directoryB/directoryC/DirectoryD
./somefileÃÂ
You can simply run the command by prefixing it with its path:
/home/directoryA/directoryB/directoryC/DirectoryD/somefile
Because you are already in the /home/directoryA
you can use the current directory shortcut .
and run the command like this:
./directoryB/directoryC/DirectoryD/somefile
I noticed OP has expanded scope via comments under other answers. Here is some additional information:
- To find out where
somefile
is located use:locate somefile
. - If
somefile
was added today you need to first update the locate database by runningsudo updatedb
. - When there are multiple versions of
somefile
located in the PATH you can find out which one is executed first usewhich somefile
. - If you want to run
somefile
without specifying a directory name in front put it in the path. To check the path useecho $PATH
. Common path locations to putsomefile
are/usr/local/bin
(if it uses sudo powers) and/home/your_user_name/bin
(you might have to create the directory first). - You can also add
/home/directoryA/directoryB/directoryC/DirectoryD/
to your path but that would be highly unusual. However you could then simply typesomefile
no matter what directory you are in and it will run. - Of course
somefile
must be executable which you set with the command:chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile
add a comment |Â
up vote
6
down vote
up vote
6
down vote
No you don't need to use:
cd home/directoryA/directoryB/directoryC/DirectoryD
./somefileÃÂ
You can simply run the command by prefixing it with its path:
/home/directoryA/directoryB/directoryC/DirectoryD/somefile
Because you are already in the /home/directoryA
you can use the current directory shortcut .
and run the command like this:
./directoryB/directoryC/DirectoryD/somefile
I noticed OP has expanded scope via comments under other answers. Here is some additional information:
- To find out where
somefile
is located use:locate somefile
. - If
somefile
was added today you need to first update the locate database by runningsudo updatedb
. - When there are multiple versions of
somefile
located in the PATH you can find out which one is executed first usewhich somefile
. - If you want to run
somefile
without specifying a directory name in front put it in the path. To check the path useecho $PATH
. Common path locations to putsomefile
are/usr/local/bin
(if it uses sudo powers) and/home/your_user_name/bin
(you might have to create the directory first). - You can also add
/home/directoryA/directoryB/directoryC/DirectoryD/
to your path but that would be highly unusual. However you could then simply typesomefile
no matter what directory you are in and it will run. - Of course
somefile
must be executable which you set with the command:chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile
No you don't need to use:
cd home/directoryA/directoryB/directoryC/DirectoryD
./somefileÃÂ
You can simply run the command by prefixing it with its path:
/home/directoryA/directoryB/directoryC/DirectoryD/somefile
Because you are already in the /home/directoryA
you can use the current directory shortcut .
and run the command like this:
./directoryB/directoryC/DirectoryD/somefile
I noticed OP has expanded scope via comments under other answers. Here is some additional information:
- To find out where
somefile
is located use:locate somefile
. - If
somefile
was added today you need to first update the locate database by runningsudo updatedb
. - When there are multiple versions of
somefile
located in the PATH you can find out which one is executed first usewhich somefile
. - If you want to run
somefile
without specifying a directory name in front put it in the path. To check the path useecho $PATH
. Common path locations to putsomefile
are/usr/local/bin
(if it uses sudo powers) and/home/your_user_name/bin
(you might have to create the directory first). - You can also add
/home/directoryA/directoryB/directoryC/DirectoryD/
to your path but that would be highly unusual. However you could then simply typesomefile
no matter what directory you are in and it will run. - Of course
somefile
must be executable which you set with the command:chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile
edited May 24 at 2:07
answered May 16 at 23:20
![](https://i.stack.imgur.com/2SXNl.jpg?s=32&g=1)
![](https://i.stack.imgur.com/2SXNl.jpg?s=32&g=1)
WinEunuuchs2Unix
34.6k756131
34.6k756131
add a comment |Â
add a comment |Â
up vote
1
down vote
Sure! If somefile is marked as executable, you can run it with
~/directoryA/directoryB/directoryC/DirectoryD/somefile
Want to know if somefile is executable? Go to its directory and run
find . -maxdepth 1 -perm -111 -type f
to see all the executables in that directory.
Ok, can you let me know how I would execute the file if IâÂÂm not already in directory A, but in some other directory.
â Carltonp
May 16 at 23:25
Also, can you let me know how to find out what directory a file is located in?
â Carltonp
May 16 at 23:27
2
If you want to know if it's executable, why not runls -l /directoryA/directoryB/directoryC/DirectoryD/somefile
?
â wjandrea
May 17 at 3:31
@Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
â K7AAY
May 17 at 20:13
add a comment |Â
up vote
1
down vote
Sure! If somefile is marked as executable, you can run it with
~/directoryA/directoryB/directoryC/DirectoryD/somefile
Want to know if somefile is executable? Go to its directory and run
find . -maxdepth 1 -perm -111 -type f
to see all the executables in that directory.
Ok, can you let me know how I would execute the file if IâÂÂm not already in directory A, but in some other directory.
â Carltonp
May 16 at 23:25
Also, can you let me know how to find out what directory a file is located in?
â Carltonp
May 16 at 23:27
2
If you want to know if it's executable, why not runls -l /directoryA/directoryB/directoryC/DirectoryD/somefile
?
â wjandrea
May 17 at 3:31
@Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
â K7AAY
May 17 at 20:13
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Sure! If somefile is marked as executable, you can run it with
~/directoryA/directoryB/directoryC/DirectoryD/somefile
Want to know if somefile is executable? Go to its directory and run
find . -maxdepth 1 -perm -111 -type f
to see all the executables in that directory.
Sure! If somefile is marked as executable, you can run it with
~/directoryA/directoryB/directoryC/DirectoryD/somefile
Want to know if somefile is executable? Go to its directory and run
find . -maxdepth 1 -perm -111 -type f
to see all the executables in that directory.
edited May 17 at 20:06
answered May 16 at 23:19
![](https://i.stack.imgur.com/IfEQx.jpg?s=32&g=1)
![](https://i.stack.imgur.com/IfEQx.jpg?s=32&g=1)
K7AAY
3,73221443
3,73221443
Ok, can you let me know how I would execute the file if IâÂÂm not already in directory A, but in some other directory.
â Carltonp
May 16 at 23:25
Also, can you let me know how to find out what directory a file is located in?
â Carltonp
May 16 at 23:27
2
If you want to know if it's executable, why not runls -l /directoryA/directoryB/directoryC/DirectoryD/somefile
?
â wjandrea
May 17 at 3:31
@Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
â K7AAY
May 17 at 20:13
add a comment |Â
Ok, can you let me know how I would execute the file if IâÂÂm not already in directory A, but in some other directory.
â Carltonp
May 16 at 23:25
Also, can you let me know how to find out what directory a file is located in?
â Carltonp
May 16 at 23:27
2
If you want to know if it's executable, why not runls -l /directoryA/directoryB/directoryC/DirectoryD/somefile
?
â wjandrea
May 17 at 3:31
@Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
â K7AAY
May 17 at 20:13
Ok, can you let me know how I would execute the file if IâÂÂm not already in directory A, but in some other directory.
â Carltonp
May 16 at 23:25
Ok, can you let me know how I would execute the file if IâÂÂm not already in directory A, but in some other directory.
â Carltonp
May 16 at 23:25
Also, can you let me know how to find out what directory a file is located in?
â Carltonp
May 16 at 23:27
Also, can you let me know how to find out what directory a file is located in?
â Carltonp
May 16 at 23:27
2
2
If you want to know if it's executable, why not run
ls -l /directoryA/directoryB/directoryC/DirectoryD/somefile
?â wjandrea
May 17 at 3:31
If you want to know if it's executable, why not run
ls -l /directoryA/directoryB/directoryC/DirectoryD/somefile
?â wjandrea
May 17 at 3:31
@Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
â K7AAY
May 17 at 20:13
@Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
â K7AAY
May 17 at 20:13
add a comment |Â
up vote
1
down vote
The $PATH
shell variable contains the directories where the executables are searched. Add the directory containing your executable into $PATH
and it can be executed from anywhere.
Add in .bashrc
file:
export PATH=$PATH:/../your_directory
/..
is the same as/
. Is that supposed to be/...
?
â wjandrea
May 24 at 1:59
add a comment |Â
up vote
1
down vote
The $PATH
shell variable contains the directories where the executables are searched. Add the directory containing your executable into $PATH
and it can be executed from anywhere.
Add in .bashrc
file:
export PATH=$PATH:/../your_directory
/..
is the same as/
. Is that supposed to be/...
?
â wjandrea
May 24 at 1:59
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The $PATH
shell variable contains the directories where the executables are searched. Add the directory containing your executable into $PATH
and it can be executed from anywhere.
Add in .bashrc
file:
export PATH=$PATH:/../your_directory
The $PATH
shell variable contains the directories where the executables are searched. Add the directory containing your executable into $PATH
and it can be executed from anywhere.
Add in .bashrc
file:
export PATH=$PATH:/../your_directory
edited May 24 at 1:57
![](https://i.stack.imgur.com/eVuAv.png?s=32&g=1)
![](https://i.stack.imgur.com/eVuAv.png?s=32&g=1)
wjandrea
7,10742054
7,10742054
answered May 17 at 8:54
Pierre Legay
111
111
/..
is the same as/
. Is that supposed to be/...
?
â wjandrea
May 24 at 1:59
add a comment |Â
/..
is the same as/
. Is that supposed to be/...
?
â wjandrea
May 24 at 1:59
/..
is the same as /
. Is that supposed to be /...
?â wjandrea
May 24 at 1:59
/..
is the same as /
. Is that supposed to be /...
?â wjandrea
May 24 at 1:59
add a comment |Â
up vote
-2
down vote
There is another way (somehow not already mentioned) by using shell profile (.bashrc
, .zshrc
etc).
You can run:
# Assuming it is a script you made, changing file permission to make it executable
chmod a+x ~/directoryA/directoryB/directoryC/directoryD/somefile
# Appending your shell profile with an alias to run the script from wherever you are
echo "alias somename="source ~/directoryA/directoryB/directoryC/directoryD/somefile"" >> ~/.profile
# replace ~/.profile with config file of whichever shell you use
# Also replace source with python if the script is a python script or whichever interpreter it requires for execution
# Make sure you have #!/usr/bin/env python or #!/path/to/interpreter on your computer as the first line of your script
Although above method allows the script to be run from anywhere you should make sure the script doesn't depend on the pwd
(present working directory) for execution (unless intended).
You can then run the script as executable in any directory, like:
somename
P.S.:
As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
@peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
â David Foerster
May 17 at 22:15
-1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in.bashrc
, not.profile
. And why create an alias instead of adding the directory to the PATH?
â wjandrea
May 24 at 2:18
Oh, I think there may have been confusion around OP's use ofsrc
in the question. I don't think that's supposed to mean the same thing assource
.
â wjandrea
May 24 at 2:24
i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
â ToxicMender
May 24 at 21:07
add a comment |Â
up vote
-2
down vote
There is another way (somehow not already mentioned) by using shell profile (.bashrc
, .zshrc
etc).
You can run:
# Assuming it is a script you made, changing file permission to make it executable
chmod a+x ~/directoryA/directoryB/directoryC/directoryD/somefile
# Appending your shell profile with an alias to run the script from wherever you are
echo "alias somename="source ~/directoryA/directoryB/directoryC/directoryD/somefile"" >> ~/.profile
# replace ~/.profile with config file of whichever shell you use
# Also replace source with python if the script is a python script or whichever interpreter it requires for execution
# Make sure you have #!/usr/bin/env python or #!/path/to/interpreter on your computer as the first line of your script
Although above method allows the script to be run from anywhere you should make sure the script doesn't depend on the pwd
(present working directory) for execution (unless intended).
You can then run the script as executable in any directory, like:
somename
P.S.:
As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
@peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
â David Foerster
May 17 at 22:15
-1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in.bashrc
, not.profile
. And why create an alias instead of adding the directory to the PATH?
â wjandrea
May 24 at 2:18
Oh, I think there may have been confusion around OP's use ofsrc
in the question. I don't think that's supposed to mean the same thing assource
.
â wjandrea
May 24 at 2:24
i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
â ToxicMender
May 24 at 21:07
add a comment |Â
up vote
-2
down vote
up vote
-2
down vote
There is another way (somehow not already mentioned) by using shell profile (.bashrc
, .zshrc
etc).
You can run:
# Assuming it is a script you made, changing file permission to make it executable
chmod a+x ~/directoryA/directoryB/directoryC/directoryD/somefile
# Appending your shell profile with an alias to run the script from wherever you are
echo "alias somename="source ~/directoryA/directoryB/directoryC/directoryD/somefile"" >> ~/.profile
# replace ~/.profile with config file of whichever shell you use
# Also replace source with python if the script is a python script or whichever interpreter it requires for execution
# Make sure you have #!/usr/bin/env python or #!/path/to/interpreter on your computer as the first line of your script
Although above method allows the script to be run from anywhere you should make sure the script doesn't depend on the pwd
(present working directory) for execution (unless intended).
You can then run the script as executable in any directory, like:
somename
P.S.:
As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
There is another way (somehow not already mentioned) by using shell profile (.bashrc
, .zshrc
etc).
You can run:
# Assuming it is a script you made, changing file permission to make it executable
chmod a+x ~/directoryA/directoryB/directoryC/directoryD/somefile
# Appending your shell profile with an alias to run the script from wherever you are
echo "alias somename="source ~/directoryA/directoryB/directoryC/directoryD/somefile"" >> ~/.profile
# replace ~/.profile with config file of whichever shell you use
# Also replace source with python if the script is a python script or whichever interpreter it requires for execution
# Make sure you have #!/usr/bin/env python or #!/path/to/interpreter on your computer as the first line of your script
Although above method allows the script to be run from anywhere you should make sure the script doesn't depend on the pwd
(present working directory) for execution (unless intended).
You can then run the script as executable in any directory, like:
somename
P.S.:
As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
edited Jun 17 at 14:12
answered May 17 at 6:28
![](https://i.stack.imgur.com/ulbom.jpg?s=32&g=1)
![](https://i.stack.imgur.com/ulbom.jpg?s=32&g=1)
ToxicMender
143
143
@peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
â David Foerster
May 17 at 22:15
-1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in.bashrc
, not.profile
. And why create an alias instead of adding the directory to the PATH?
â wjandrea
May 24 at 2:18
Oh, I think there may have been confusion around OP's use ofsrc
in the question. I don't think that's supposed to mean the same thing assource
.
â wjandrea
May 24 at 2:24
i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
â ToxicMender
May 24 at 21:07
add a comment |Â
@peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
â David Foerster
May 17 at 22:15
-1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in.bashrc
, not.profile
. And why create an alias instead of adding the directory to the PATH?
â wjandrea
May 24 at 2:18
Oh, I think there may have been confusion around OP's use ofsrc
in the question. I don't think that's supposed to mean the same thing assource
.
â wjandrea
May 24 at 2:24
i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
â ToxicMender
May 24 at 21:07
@peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
â David Foerster
May 17 at 22:15
@peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
â David Foerster
May 17 at 22:15
-1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in
.bashrc
, not .profile
. And why create an alias instead of adding the directory to the PATH?â wjandrea
May 24 at 2:18
-1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in
.bashrc
, not .profile
. And why create an alias instead of adding the directory to the PATH?â wjandrea
May 24 at 2:18
Oh, I think there may have been confusion around OP's use of
src
in the question. I don't think that's supposed to mean the same thing as source
.â wjandrea
May 24 at 2:24
Oh, I think there may have been confusion around OP's use of
src
in the question. I don't think that's supposed to mean the same thing as source
.â wjandrea
May 24 at 2:24
i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
â ToxicMender
May 24 at 21:07
i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
â ToxicMender
May 24 at 21:07
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1037162%2fhow-to-run-a-file-in-a-different-directory%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Beside the point, but note that
directoryD
andDirectoryD
are not the same.â wjandrea
May 17 at 3:33
2
@wjandrea Plus
/home/directoryA
is probably meant to be/home/ME/directoryA
. Also thecd /directoryA/directoryB....
is missing the~
prefix. Also thecd
ends in.../somefile
which is impossible to change directory to a file. There are lots of flaws that need to be overlooked.â WinEunuuchs2Unix
May 17 at 4:40
I've updated my answer based on new comments read under other answers.
â WinEunuuchs2Unix
May 17 at 12:32