View the full path (absolute path) of a file in a directory in Linux Bash

Introduction

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.

Overview

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).

Motivation

I made it because I needed the full path of the file to download it from the Google Compute Engine (GCE) console.

specification

Command name

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.

Input specifications

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.

Output specifications

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.

Error handling

If the path specified in the argument does not exist, an error will be output.

Implementation

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'

test

$ cd ~/
$ source ./.bashrc
$ lf
$ lfa
$ lft
$ lf ~/

Afterword

Bash is very convenient. I will use it in various ways from now on. Thank you for reading to the end.

Postscript

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

View the full path (absolute path) of a file in a directory in Linux Bash
[Note] Import of a file in the parent directory in Python
Replace the directory name and the file name in the directory together with a Linux command.
The story of the "hole" in the file
Process the contents of the file in order with a shell script
On Linux, the time stamp of a file is a little past.
How to output the output result of the Linux man command to a file
Read a file in Python with a relative path from the program
Dig the directory and create a list of directory paths + file names
[Understanding in 3 minutes] The beginning of Linux
Get the filename of a directory (glob)
Test & Debug Tips: Create a file of the specified size in Python
I made a program to check the size of a file in Python
Various ways to read the last line of a csv file in Python
I want to get the path of the directory where the running file is stored.
Extract only the file name excluding the directory in the directory
Get the caller of a function in Python
View the result of geometry processing in Python
Make a copy of the list in Python
Find the number of days in a month
Set a fixed IP in the Linux environment
Try modifying the TortoiseHg file view a bit
Output in the form of a python array
In bash, "Delete the file if it exists".
How to read a file in a different directory
How to get a list of files in the same directory with python
When a file is placed in the shared folder of Raspberry Pi, the process is executed.
How to display the modification date of a file in C language up to nanoseconds
A memo that implements the job of loading a GCS file into BigQuery in Python
Convert a path string that uses a symbolic link in the middle to an absolute path
Get the file name in a folder using glob
The story of building the fastest Linux environment in the world
[Python] Get the official file path of the shortcut file (.lnk)
Get the absolute path of the script you are running
File renaming using the full path received by the shell
Output the output result of sklearn.metrics.classification_report as a CSV file
A reminder about the implementation of recommendations in Python
Linux (about directory path)
Python> library> os> os.walk ()> Get directory structure / Implementation to get each file path in the specified directory
[Sublime Text 2] Always execute a specific file in the project
[Ubuntu] How to delete the entire contents of a directory
Avoiding the pitfalls of using a Mac (for Linux users?)
A note on the default behavior of collate_fn in PyTorch
Save the pystan model and results in a pickle file
Find out the apparent width of a string in python
Change the standard output destination to a file in Python
[Linux] Grep multiple gzip files in a directory at once
Access the file with a relative path from the execution script.
A note for embedding the scripting language in a bash script
Note 2 for embedding the scripting language in a bash script
Get the number of specific elements in a python list
A rough summary of the differences between Windows and Linux
[Python] Get the script execution directory with an absolute path
Mac Linux Check the capacity directly under a specific directory
Google search for the last line of the file in Python
Find the eigenvalues of a real symmetric matrix in Python
Download the file in Python
Handling of quotes in [bash]
File access under the directory
Linux file and directory permissions
[Linux] Directory under the root