0

I want to list absolute path of all folders in a given path using shell script.

What I've tried is :

ls -l /var/www/temp

But I couldn't find an option for ls command which will list the absolute path.

I found a related question in StackOverflow : how to list full paths of folders inside a directory in linux?

But what I need is in a single command itself I need to list all folders in the given path (this path varies) with their absolute path.

Can anyone help me to do this? Thanks in advance.

Community
  • 1
  • 1
Jenz
  • 8,280
  • 7
  • 44
  • 77
  • 1
    Possible duplicate of [Unix ls command: show full path when using options](http://stackoverflow.com/questions/5580828/unix-ls-command-show-full-path-when-using-options) – Dymen1 May 22 '17 at 11:24
  • Doesn't [this anwser](https://stackoverflow.com/a/16768547) in the question that's marked as duplicate of the question you linked to, already do what you need? – Decent Dabbler May 22 '17 at 11:24
  • @Dymen1..In that questions solutions where can I specify the path? – Jenz May 22 '17 at 11:27
  • @DecentDabbler..Tried with that answer. But it is listing files also. I need only folders. – Jenz May 22 '17 at 11:37
  • Are you sure about that? It works fine with me. Do you have an `ls` alias with extra options defined in your environment, by any chance? If so, do a `which ls` and use that output (`/bin/ls`, for instance) as the command: `/bin/ls -d /your/path/*` – Decent Dabbler May 22 '17 at 11:42
  • And if you want each entry on a new line add the `-1` option: `/bin/ls -d -1 /your/path/*` – Decent Dabbler May 22 '17 at 11:44
  • @DecentDabbler..Tried with `/bin/ls -d /your/path/*`. Now also it is returning files. – Jenz May 22 '17 at 11:50

4 Answers4

0

This will help but it's not using "ls"

You can use the substitution of pwd with find.

   find /var/www/temp -type d

But be advised that this will be outputting the list on the screen.

Nikhil Fadnis
  • 787
  • 5
  • 14
0

How about a shell function?

dabspath () {
if [ -d "$1" ]
then
    cd "$1"
    find "$PWD" -type d
    cd "$OLDPWD"
else
    echo "$0: $1: No such directory"
fi
}

Usage: dabspath foo

If foo is a directory relative to the current working directory then it will print the absolute path of foo and any sub-directories.

fd0
  • 289
  • 7
  • 10
0

for D in `find . -maxdepth 1 -type d`; do echo $PWD${D#.}; done

How it works:

  1. First, bash will run the find command within the backticks and replace the part in backticks with what find returns, which are names of all direct subdirectories of the working directory.
  2. Next, the for loop will go through all the subdirectories and for each subdirectory, stored in variable D, it will print the path to the working directory ($PWD) followed by the subdirectory D, from which it removes the prexif ".". That way, the absolute path is printed as expected.

Note: As every directory has a hard link to itself (the "." subdirectory), it will also print the path to the working directory.

Joe Samanek
  • 1,644
  • 12
  • 16
0

This script lists all directories, or optionally, all of any type recognized by the -type T option of find. By default, if no arguments are provided, it lists all directories in the current dir. To list absolute paths, pass an absolute path as a target directory.

#!/bin/bash

# usage: ${0} [-type [fd]] [-l] <directory>

_TYPE="d" # if f, list only files, if d, list only directories
let _long=0
let _typeflag=0

# collect dirs and files
DIRS=( ) ; FILS=( )
for A in "$@" ; do
  if [ $_typeflag -eq 1 ]; then
    _TYPE=$A
    let _typeflag=0
  elif [ -d "$A" ]; then
    DIRS=( ${DIRS[@]} "$A" )
  elif [ -f "$A" ]; then
    FILS=( ${FILS[@]} "$A" )
  else
    case "$A" in
    "-type") let _typeflag=1 ;;
    "-l") let _long=1 ;;
    *) echo "not a directory: [$A]" 1>&2
      exit 1
      ;;
    esac
  fi
done

# list files in current dir, if nothing specified
[ ${#DIRS[@]} -eq 0 ] && DIRS=( "$(pwd -P)" )

if [ $_long -eq 0 ]; then
  find ${DIRS[@]} -maxdepth 1 -type $_TYPE | while read F ; do
    if [[ "$F" != "." && "$F" != ".." ]]; then
      echo "\"$F\""
    fi
  done | xargs ls -ltrad --time-style=long-iso | sed 's#.*:[0-9][0-9] ##'
else
  find ${DIRS[@]} -maxdepth 1 -type $_TYPE | while read F ; do
    echo "\"$F\""
  done | xargs ls -ltrad --time-style=long-iso
fi
philwalk
  • 634
  • 1
  • 7
  • 15