How to get a full path to a file by name in Linux [duplicate]
This question already has an answer here:
bash/fish command to print absolute path to a file
17 answers
I was given a task to write script that works similar to "which" command in terminal. Here is what I already wrote:
#! /bin/bash
FILE=$1
for i in $PATH
do
if [[ "$i" -eq "FILE" ]]
then
echo …
Here I need to get a full path to a file that has been found. How could i get it? Thanks in advice.
linux bash
marked as duplicate by omajid, tripleee
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 13:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
bash/fish command to print absolute path to a file
17 answers
I was given a task to write script that works similar to "which" command in terminal. Here is what I already wrote:
#! /bin/bash
FILE=$1
for i in $PATH
do
if [[ "$i" -eq "FILE" ]]
then
echo …
Here I need to get a full path to a file that has been found. How could i get it? Thanks in advice.
linux bash
marked as duplicate by omajid, tripleee
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 13:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Userealpath
.
– Maroun
Nov 20 '18 at 13:03
Link should be to the shell version: man7.org/linux/man-pages/man1/realpath.1.html. Or you can also usereadlink -f
.
– omajid
Nov 20 '18 at 13:19
2
Yourfor
loop is confused. ThePATH
variable contains a single string; to loop over the individual components, you have to split it on colons. Then you have to add the name of the file you are looking for to the end of each extracted directory. The resulting paths are typically (but not necessarily) already absolute. The-eq
comparison operator is for numeric equality; use=
for string comparison. And finally use"$FILE"
with a dollar sign to examine your variable (but you should probably prefer lower case for your private variables).
– tripleee
Nov 20 '18 at 13:30
See also stackoverflow.com/questions/33469374/…
– tripleee
Nov 20 '18 at 13:42
add a comment |
This question already has an answer here:
bash/fish command to print absolute path to a file
17 answers
I was given a task to write script that works similar to "which" command in terminal. Here is what I already wrote:
#! /bin/bash
FILE=$1
for i in $PATH
do
if [[ "$i" -eq "FILE" ]]
then
echo …
Here I need to get a full path to a file that has been found. How could i get it? Thanks in advice.
linux bash
This question already has an answer here:
bash/fish command to print absolute path to a file
17 answers
I was given a task to write script that works similar to "which" command in terminal. Here is what I already wrote:
#! /bin/bash
FILE=$1
for i in $PATH
do
if [[ "$i" -eq "FILE" ]]
then
echo …
Here I need to get a full path to a file that has been found. How could i get it? Thanks in advice.
This question already has an answer here:
bash/fish command to print absolute path to a file
17 answers
linux bash
linux bash
asked Nov 20 '18 at 13:02
MakMak
276
276
marked as duplicate by omajid, tripleee
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 13:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by omajid, tripleee
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 13:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Userealpath
.
– Maroun
Nov 20 '18 at 13:03
Link should be to the shell version: man7.org/linux/man-pages/man1/realpath.1.html. Or you can also usereadlink -f
.
– omajid
Nov 20 '18 at 13:19
2
Yourfor
loop is confused. ThePATH
variable contains a single string; to loop over the individual components, you have to split it on colons. Then you have to add the name of the file you are looking for to the end of each extracted directory. The resulting paths are typically (but not necessarily) already absolute. The-eq
comparison operator is for numeric equality; use=
for string comparison. And finally use"$FILE"
with a dollar sign to examine your variable (but you should probably prefer lower case for your private variables).
– tripleee
Nov 20 '18 at 13:30
See also stackoverflow.com/questions/33469374/…
– tripleee
Nov 20 '18 at 13:42
add a comment |
2
Userealpath
.
– Maroun
Nov 20 '18 at 13:03
Link should be to the shell version: man7.org/linux/man-pages/man1/realpath.1.html. Or you can also usereadlink -f
.
– omajid
Nov 20 '18 at 13:19
2
Yourfor
loop is confused. ThePATH
variable contains a single string; to loop over the individual components, you have to split it on colons. Then you have to add the name of the file you are looking for to the end of each extracted directory. The resulting paths are typically (but not necessarily) already absolute. The-eq
comparison operator is for numeric equality; use=
for string comparison. And finally use"$FILE"
with a dollar sign to examine your variable (but you should probably prefer lower case for your private variables).
– tripleee
Nov 20 '18 at 13:30
See also stackoverflow.com/questions/33469374/…
– tripleee
Nov 20 '18 at 13:42
2
2
Use
realpath
.– Maroun
Nov 20 '18 at 13:03
Use
realpath
.– Maroun
Nov 20 '18 at 13:03
Link should be to the shell version: man7.org/linux/man-pages/man1/realpath.1.html. Or you can also use
readlink -f
.– omajid
Nov 20 '18 at 13:19
Link should be to the shell version: man7.org/linux/man-pages/man1/realpath.1.html. Or you can also use
readlink -f
.– omajid
Nov 20 '18 at 13:19
2
2
Your
for
loop is confused. The PATH
variable contains a single string; to loop over the individual components, you have to split it on colons. Then you have to add the name of the file you are looking for to the end of each extracted directory. The resulting paths are typically (but not necessarily) already absolute. The -eq
comparison operator is for numeric equality; use =
for string comparison. And finally use "$FILE"
with a dollar sign to examine your variable (but you should probably prefer lower case for your private variables).– tripleee
Nov 20 '18 at 13:30
Your
for
loop is confused. The PATH
variable contains a single string; to loop over the individual components, you have to split it on colons. Then you have to add the name of the file you are looking for to the end of each extracted directory. The resulting paths are typically (but not necessarily) already absolute. The -eq
comparison operator is for numeric equality; use =
for string comparison. And finally use "$FILE"
with a dollar sign to examine your variable (but you should probably prefer lower case for your private variables).– tripleee
Nov 20 '18 at 13:30
See also stackoverflow.com/questions/33469374/…
– tripleee
Nov 20 '18 at 13:42
See also stackoverflow.com/questions/33469374/…
– tripleee
Nov 20 '18 at 13:42
add a comment |
1 Answer
1
active
oldest
votes
If you're willing to use Python, use Terminal's locate
program using subprocess
, like:
import subprocess
def find_filepath(file_name):
outcome = (subprocess.Popen(["locate", file_name], stdout=subprocess.PIPE).communicate()[0]).decode()
return outcome.split("n")
This should fetch a list of Absolute file path.
locate my find multiple files with the same name. It doesn't pick one based on the order of appearance in$PATH
– omajid
Nov 20 '18 at 13:17
I agree but what if there are multiple files with same name, hence to avoid ambiguity why not simultaneously check for duplicates as well, and then just slice out the one required based on index position (maybe even delete rest, as per need).
– Random Nerd
Nov 20 '18 at 13:20
You are right, but that's not howPATH
orwhich
work. The index oflocate
doesn't correspond to the one from searchingPATH
:/
– omajid
Nov 20 '18 at 13:22
My answer was keeping in mind a bigger picture, but again you're right, as OP asked for something similar towhich
, sorealpath
should be a better and easier alternative.
– Random Nerd
Nov 20 '18 at 13:26
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you're willing to use Python, use Terminal's locate
program using subprocess
, like:
import subprocess
def find_filepath(file_name):
outcome = (subprocess.Popen(["locate", file_name], stdout=subprocess.PIPE).communicate()[0]).decode()
return outcome.split("n")
This should fetch a list of Absolute file path.
locate my find multiple files with the same name. It doesn't pick one based on the order of appearance in$PATH
– omajid
Nov 20 '18 at 13:17
I agree but what if there are multiple files with same name, hence to avoid ambiguity why not simultaneously check for duplicates as well, and then just slice out the one required based on index position (maybe even delete rest, as per need).
– Random Nerd
Nov 20 '18 at 13:20
You are right, but that's not howPATH
orwhich
work. The index oflocate
doesn't correspond to the one from searchingPATH
:/
– omajid
Nov 20 '18 at 13:22
My answer was keeping in mind a bigger picture, but again you're right, as OP asked for something similar towhich
, sorealpath
should be a better and easier alternative.
– Random Nerd
Nov 20 '18 at 13:26
add a comment |
If you're willing to use Python, use Terminal's locate
program using subprocess
, like:
import subprocess
def find_filepath(file_name):
outcome = (subprocess.Popen(["locate", file_name], stdout=subprocess.PIPE).communicate()[0]).decode()
return outcome.split("n")
This should fetch a list of Absolute file path.
locate my find multiple files with the same name. It doesn't pick one based on the order of appearance in$PATH
– omajid
Nov 20 '18 at 13:17
I agree but what if there are multiple files with same name, hence to avoid ambiguity why not simultaneously check for duplicates as well, and then just slice out the one required based on index position (maybe even delete rest, as per need).
– Random Nerd
Nov 20 '18 at 13:20
You are right, but that's not howPATH
orwhich
work. The index oflocate
doesn't correspond to the one from searchingPATH
:/
– omajid
Nov 20 '18 at 13:22
My answer was keeping in mind a bigger picture, but again you're right, as OP asked for something similar towhich
, sorealpath
should be a better and easier alternative.
– Random Nerd
Nov 20 '18 at 13:26
add a comment |
If you're willing to use Python, use Terminal's locate
program using subprocess
, like:
import subprocess
def find_filepath(file_name):
outcome = (subprocess.Popen(["locate", file_name], stdout=subprocess.PIPE).communicate()[0]).decode()
return outcome.split("n")
This should fetch a list of Absolute file path.
If you're willing to use Python, use Terminal's locate
program using subprocess
, like:
import subprocess
def find_filepath(file_name):
outcome = (subprocess.Popen(["locate", file_name], stdout=subprocess.PIPE).communicate()[0]).decode()
return outcome.split("n")
This should fetch a list of Absolute file path.
answered Nov 20 '18 at 13:13
Random NerdRandom Nerd
1314
1314
locate my find multiple files with the same name. It doesn't pick one based on the order of appearance in$PATH
– omajid
Nov 20 '18 at 13:17
I agree but what if there are multiple files with same name, hence to avoid ambiguity why not simultaneously check for duplicates as well, and then just slice out the one required based on index position (maybe even delete rest, as per need).
– Random Nerd
Nov 20 '18 at 13:20
You are right, but that's not howPATH
orwhich
work. The index oflocate
doesn't correspond to the one from searchingPATH
:/
– omajid
Nov 20 '18 at 13:22
My answer was keeping in mind a bigger picture, but again you're right, as OP asked for something similar towhich
, sorealpath
should be a better and easier alternative.
– Random Nerd
Nov 20 '18 at 13:26
add a comment |
locate my find multiple files with the same name. It doesn't pick one based on the order of appearance in$PATH
– omajid
Nov 20 '18 at 13:17
I agree but what if there are multiple files with same name, hence to avoid ambiguity why not simultaneously check for duplicates as well, and then just slice out the one required based on index position (maybe even delete rest, as per need).
– Random Nerd
Nov 20 '18 at 13:20
You are right, but that's not howPATH
orwhich
work. The index oflocate
doesn't correspond to the one from searchingPATH
:/
– omajid
Nov 20 '18 at 13:22
My answer was keeping in mind a bigger picture, but again you're right, as OP asked for something similar towhich
, sorealpath
should be a better and easier alternative.
– Random Nerd
Nov 20 '18 at 13:26
locate my find multiple files with the same name. It doesn't pick one based on the order of appearance in
$PATH
– omajid
Nov 20 '18 at 13:17
locate my find multiple files with the same name. It doesn't pick one based on the order of appearance in
$PATH
– omajid
Nov 20 '18 at 13:17
I agree but what if there are multiple files with same name, hence to avoid ambiguity why not simultaneously check for duplicates as well, and then just slice out the one required based on index position (maybe even delete rest, as per need).
– Random Nerd
Nov 20 '18 at 13:20
I agree but what if there are multiple files with same name, hence to avoid ambiguity why not simultaneously check for duplicates as well, and then just slice out the one required based on index position (maybe even delete rest, as per need).
– Random Nerd
Nov 20 '18 at 13:20
You are right, but that's not how
PATH
or which
work. The index of locate
doesn't correspond to the one from searching PATH
:/– omajid
Nov 20 '18 at 13:22
You are right, but that's not how
PATH
or which
work. The index of locate
doesn't correspond to the one from searching PATH
:/– omajid
Nov 20 '18 at 13:22
My answer was keeping in mind a bigger picture, but again you're right, as OP asked for something similar to
which
, so realpath
should be a better and easier alternative.– Random Nerd
Nov 20 '18 at 13:26
My answer was keeping in mind a bigger picture, but again you're right, as OP asked for something similar to
which
, so realpath
should be a better and easier alternative.– Random Nerd
Nov 20 '18 at 13:26
add a comment |
2
Use
realpath
.– Maroun
Nov 20 '18 at 13:03
Link should be to the shell version: man7.org/linux/man-pages/man1/realpath.1.html. Or you can also use
readlink -f
.– omajid
Nov 20 '18 at 13:19
2
Your
for
loop is confused. ThePATH
variable contains a single string; to loop over the individual components, you have to split it on colons. Then you have to add the name of the file you are looking for to the end of each extracted directory. The resulting paths are typically (but not necessarily) already absolute. The-eq
comparison operator is for numeric equality; use=
for string comparison. And finally use"$FILE"
with a dollar sign to examine your variable (but you should probably prefer lower case for your private variables).– tripleee
Nov 20 '18 at 13:30
See also stackoverflow.com/questions/33469374/…
– tripleee
Nov 20 '18 at 13:42