Is there an easy way I can print the full path of file.txt
?
file.txt = /nfs/an/disks/jj/home/dir/file.txt
The
This will work for both file and folder:
getAbsolutePath(){
[[ -d $1 ]] && { cd "$1"; echo "$(pwd -P)"; } ||
{ cd "$(dirname "$1")" || exit 1; echo "$(pwd -P)/$(basename "$1")"; }
}
You can save this in your shell.rc
or just put in console
function absolute_path { echo "$PWD/$1"; }
alias ap="absolute_path"
example:
ap somefile.txt
will output
/home/user/somefile.txt
You could use the fpn (full path name) script:
% pwd
/Users/adamatan/bins/scripts/fpn
% ls
LICENSE README.md fpn.py
% fpn *
/Users/adamatan/bins/scripts/fpn/LICENSE
/Users/adamatan/bins/scripts/fpn/README.md
/Users/adamatan/bins/scripts/fpn/fpn.py
fpn
is not a standard Linux package, but it's a free and open github project and you could set it up in a minute.
find $PWD -type f | grep "filename"
or
find $PWD -type f -name "*filename*"
In a similar scenario, I'm launching a cshell script from some other location. For setting the correct absolute path of the script so that it runs in the designated directory only, I'm using the following code:
set script_dir = `pwd`/`dirname $0`
$0
stores the exact string how the script was executed.
For e.g. if the script was launched like this: $> ../../test/test.csh
,
$script_dir
will contain /home/abc/sandbox/v1/../../test
Create a function like the below (echoes the absolute path of a file with pwd and adds the file at the end of the path:
abspath() { echo $(pwd "$1")/"$1"; }
Now you can just find any file path:
abspath myfile.ext