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:

#!/bin/sh
curdir=`pwd`
if [ $# -eq 1 ]
then
usrdir="$1"
ls | xargs -I % echo `pwd`/% | sed -e s!$usrdir!!
else
ls | xargs -I % echo `pwd`/%
fi

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