Otaqui.com Blog

Listing files with the path in Bash

This little script will print a list of the current files in a directory, prefixed with the full path:

if [ $# -eq 1 ]
ls | xargs -I % echo `pwd`/% | sed -e s!$usrdir!!
ls | xargs -I % echo `pwd`/%

You can optionally provide an initial part of the path to ignore. I use this when writing code change emails where I have to list the updated files within a codebase (and I’ve touched most files in a given directory) and I clip the local path up to the root of the codeset. Assuming the script is saved as “lspath”:

lspath /Users/pete/Sites/project/trunk

Even handier is piping it into the OS X clipboard for easy pasting:

lspath /Users/pete/Sites/project/trunk | pbcopy