tags:

views:

47

answers:

5

The directories are variables set to the full-path

for e in "$DIR_0" "$DIR_1" "$DIR_2"
do
    for i in $e/*
    do
        echo $i
    done

The output for each line is the full path. I want only the name of each file

+2  A: 

You are looking for basename.

joschi
A: 

If you truly do not wish to recurse you can achieve that more succinctly with this find command:

find "$DIR_0" "$DIR_1" "$DIR_2" -type f -maxdepth 1 -exec basename{} \;

If you wish to recurse over subdirs simply leave out maxdepth:

find "$DIR_0" "$DIR_1" "$DIR_2" -type f  -exec basename{} \;
ennuikiller
A: 

to traveling a directory recursively with bash

try this you can find it here

#! /bin/bash


    indent_print()
    {
        for((i=0; i < $1; i++)); do
        echo -ne "\t"
        done
        echo "$2"
    }

    walk_tree()
    {
        local oldifs bn lev pr pmat
        if [[ $# -lt 3 ]]; then
        if [[ $# -lt 2 ]]; then
            pmat=".*"
        else
            pmat="$2"
        fi
        walk_tree "$1" "$pmat" 0
        return
        fi
        lev=$3
        [ -d "$1" ] || return
        oldifs=$IFS
        IFS=""
        for el in $1/ *; do
        bn=$(basename "$el")
        if [[ -d "$el" ]]; then
            indent_print $lev "$bn/"
            pr=$( walk_tree "$el" "$2" $(( lev + 1)) )
            echo "$pr"
        else
            if [[ "$bn" =~ $2 ]]; then
            indent_print $lev "$bn"
            fi
        fi
        done
        IFS=$oldifs
    }

    walk_tree "$1" "\.sh$"
chahedous
It never finishes. It gets stuck in the recursion.
Dennis Williamson
A: 

This is the Bash equivalent of basename:

echo "${i##*/}"

It strips off everything before and including the last slash.

Dennis Williamson
At the moment i prefer this solution, as it seems like the most easy one.I got enough to think about. Thanks for all the posts.
tornow
A: 

See also the POSIX compliant Bash functions to replace basename & dirname here:

http://cfaj.freeshell.org/src/scripts/

jackIT