Views:
93,395β
Votes: 10β
Tag :
command-line
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/1037165
Title:
How to run a file in a different directory
ID:
/2018/05/16/How-to-run-a-file-in-a-different-directory
Created:
May 16, 2018
Edited: May 24, 2018
Upload:
October 19, 2025
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
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
somefileis located use:locate somefile. - If
somefilewas added today you need to first update the locate database by runningsudo updatedb. - When there are multiple versions of
somefilelocated in the PATH you can find out which one is executed first usewhich somefile. - If you want to run
somefilewithout specifying a directory name in front put it in the path. To check the path useecho $PATH. Common path locations to putsomefileare/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 typesomefileno matter what directory you are in and it will run. - Of course
somefilemust be executable which you set with the command:chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile