Actually, I have a folder which contains several sub folders with lots of images in each.

I am trying to gather all the names of the files in each sub folder in a text file (filesNames.txt) in that sub folder in the form of:


So, I wrote an script in the parent folder:


for dir in "$PWD"/*/; do
arr=( "$dir"* )
cd "$dir"
printf "%s 1\n" "$PWD/${arr[@]##*/}" > "$dir"filesNames.txt
cd ..

My problem is that:

I have the absolute address just for the first file in each sub folder. For the rest there is only filenames without absolute address:


I think it is related to $PWD variable which I iterate over it just once for each sub folder. How can I change the script in a suitable form?

Thanks in advance.

  • @don_crissti To have sub folder names in the printed absolute path of each file.
    – Ali
    May 23 '16 at 18:16
  • Why didn't you say you need full paths ? Just replace ${arr[@]##*/}" in my answer here with "${arr[@]}" May 23 '16 at 18:17
  • @don_crissti You're right, sorry.
    – Ali
    May 23 '16 at 18:26

Single find command will output all the files with absolute path

find $(pwd) -type f

And if you want to do that using pure shell script and dont want to use find, try this little one:

#Simple bash recursive loop search - Luciano A. Martini =) 
for f in *; do 
   if [ -d "$f" ] && [ ! -L "$f" ]; then
        #echo "Inside folder: $PWD/$f"
        nextdir "$f"
        cd ..
        echo "$PWD/$f"
#call the function for the first time...

Save for example as recursive-loop and execute in the folder you want to do the search. It will do exactly what the find or other recursive mechanisms does, but using a for loop, and you can customize for your needs!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.