0

I have a directory that contains so many sub directories and files in them. I would like to create a file which provides each file name and it's source directory path information

For example:

I have a folder name sample which contains sample1 and sample2 sub directories in it.

I have ex1.csv, ex2.csv in sample 1 and ex3.csv, ex4.csv in sample 2 directory.

I need to create a text file (or) CSV file which gives the information in the following manner.

ex1.csv,sample/sample1
ex2.csv,sample/sample1
ex3.csv,sample/sample2

It would be great if someone help me out to create the script file in UNIX.

I tried: $ find sample -type f -printf '%f,%h\n'

As I am using Solaris it is not allowing me to use printf and I don't have access to install other utilities.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

3

I hope this is compatible with your Solaris - I tested it under a SunOS 5.10 with bash 4.3.26:

find . | while read f; do
    if [ ! -d "$f" ]; then
        echo $(basename "$f"),$(dirname "$f")
    fi
done

This simply uses basename and dirname to split the filename.

$ find test | while read f; do if [ ! -d "$f" ]; then echo $(basename "$f"),$(dirname "$f"); fi; done
ex4,test/sample2
ex3,test/sample2
ex2,test/sample1
ex1,test/sample1