This is my first post, so it may not be possible to reach it, but thank you. Please feel free to comment if there are any strange points or improvements.
This article introduces a command (script) that displays the full path (absolute path) of a file in a directory using Bash in a general Linux environment (assuming debian system here).
I made it because I needed the full path of the file to download it from the Google Compute Engine (GCE) console.
In lf, the same files as those displayed by "ls" are displayed in the same order with the full path. lfa displays the same files as those displayed by "ls -a" in the same order with the full path. That is, the dot file is also displayed. lft displays the same files as those displayed by "ls -t" in the same order with the full path. That is, the update times are displayed in chronological order.
Allows you to specify multiple file names (file paths) and directory names (directory paths) in command arguments as input. Either a relative path or an absolute path is acceptable. If the directory is not included, it is assumed to be in the current directory. It does not search from PATH variable etc. Glob is not controlled by the command side.
Processes in the order of the entered file name and directory name, and if it is a directory, outputs the full path of all files in that directory to the standard output. If it is not a directory, print the full path of the file to standard output.
If the path specified in the argument does not exist, an error will be output.
The implementation is at the end of ~ / .bashrc. If you can't use readlink, use the following line in the comments. I don't think "--color = auto" is necessary, so you can delete it.
~/.bashrc
function _lsfp ()
{
local dir file;
if [[ -d $1 ]]; then
dir=$(cd "$1";pwd);
ls --color=auto -1 $2 "$1" | while read file; do
echo "$dir/$file";
done;
elif [[ -e $1 ]]; then
readlink -f "$1"
# echo "$(cd $(dirname "$1");pwd)/${1##*/}";
else
echo "'$1' is not found." >&2;
fi
}
function lsfp ()
{
local arg;
[[ $# -eq 0 ]] && _lsfp "$(pwd)" || for arg in "$@";
do
_lsfp "$arg";
done
}
function lsfpa ()
{
local arg;
[[ $# -eq 0 ]] && _lsfp "$(pwd)" -a || for arg in "$@";
do
_lsfp "$arg" -a;
done
}
function lsfpt ()
{
local arg;
[[ $# -eq 0 ]] && _lsfp "$(pwd)" -t || for arg in "$@";
do
_lsfp "$arg" -t;
done
}
alias lf='lsfp'
alias lfa='lsfpa'
alias lft='lsfpt'
$ cd ~/
$ source ./.bashrc
$ lf
$ lfa
$ lft
$ lf ~/
Bash is very convenient. I will use it in various ways from now on. Thank you for reading to the end.
I also added lfs which are shortened and displayed in order of size (largest order). The function name etc. have changed.
~/.bashrc
function __lsfp ()
{
local dir file;
if [[ -d $1 ]]; then
[[ $rl ]] && dir=$(readlink -f "$1") || dir=$(cd "$1";pwd);
ls --color=auto -1 $2 "$1" | while read ent; do
echo "$dir/$ent";
done;
elif [[ -e $1 ]]; then
[[ $rl ]] && readlink -f "$1" || echo "$(cd $(dirname "$1");pwd)/${1##*/}";
else
echo "'$1' is not found." >&2;
fi
}
function _lsfp ()
{
local opt arg rl;
which readlink >/dev/null 2>&1 && rl=1
[[ $1 =~ ^-[A-Za-z\-]$ ]] && { opt=$1; shift; }
[[ $# -eq 0 ]] && __lsfp "$(pwd)" $opt || for arg in "$@";
do
__lsfp "$arg" $opt;
done
}
alias lf='_lsfp --'
alias lfa='_lsfp -a'
alias lft='_lsfp -t'
alias lfs='_lsfp -S' #By size
Recommended Posts