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
  • 66,199
  • 35
  • 114
  • 250
vinod kumar
  • 101
  • 1

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
Martin Nyolt
  • 434
  • 3
  • 11
  • Thank you for the solution but when i execute the following code i am getting syntax error `(' unexpected – vinod kumar Aug 30 '16 at 18:16
  • Can you debug that error? Try to isolate the statement, i.e. which "(" is unexpected? What shell and version are you using? – Martin Nyolt Aug 30 '16 at 21:49
  • Which shell are you using? /bin/sh on Solaris 10 will not work with $(), as the answer specified you need to use bash for that. – alanc Aug 30 '16 at 22:38
  • GNOME Terminal 2.6.1 – vinod kumar Aug 31 '16 at 00:07
  • GNOME Terminal is a terminal emulator, not a shell. Please *first* read [Are terminal and shell the same?](http://askubuntu.com/questions/111144/are-terminal-and-shell-the-same) and [What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'console'?](http://unix.stackexchange.com/questions/4126/what-is-the-exact-difference-between-a-terminal-a-shell-a-tty-and-a-con). *Then* answer (for yourself) the question: can you use the `bash` shell? If "yes", type `bash` in your terminal and try my solution again. If "no", we need to know which shell you use (try `echo $SHELL`). – Martin Nyolt Aug 31 '16 at 08:09