You are viewing ghostdog74

Previous Entry | Next Entry

Recurse directory [Bash]

Example 1: Simple recursion into directories and listing file names
recurse() {
 for i in "$1"/*;do
    if [ -d "$i" ];then
        recurse "$i"
    else
        echo "$i"
    fi
 done
}
recurse /export/home/user1




Example 2: Recurse and change file names with extension txt
recurse() {
 for file in "$1"/*
 do
    if [ -d "$file" ];then
        recurse "$file"
    else
        ext="${file##*.}"
        basename="${file##*/}"
        bname="${basename%.*}"
        dirname=${file%/*}
        case "$ext" in
            txt)
            echo mv "$file" "$dirname/${bname}_${basename}"
            ;;
        esac
    fi
 done
}
recurse /home/path

Profile

ghostdog74
ghostdog74

Latest Month

November 2013
S M T W T F S
     12
3456789
10111213141516
17181920212223
24252627282930

Tags

Powered by LiveJournal.com
Designed by Lilia Ahner