0

My data structure is something like:

dir1/subdir1_level1/subdir1_level2.../subdir1_leveln
dir1/subdir2_level1/subdir2_level2.../subdir2_leveln
...

Which means that the levels I'm working with are not structured properly, nevertheless. I managed to accomplish my objective of getting the filename according to the first 2 levels of the path, and output them to a text file named dirn_subdirn_fname.txt with:

find dir1/subdir1 -type f -printf "%f\n">dir1_subdir1_fname.txt

But I was wondering if there was a way to Iterate this process across a list of several dir/subdir's

edit: I found a way to do the first part:

#!/bin/bash

shopt -s dotglob nullglob

topdir= './Dir1'

 for subdir in "$topdir"; do
    find "$subdir" -type f -printf "%f\n"
done
  • Probably something like for pathname in dir1/*/ ; do find "$pathname" -type f > "$pathname"_fname.txt; done ? Or find dir1/ -type f -exec sh -c 'echo "$1" > "$(basename "$1" )"_fname.txt' sh {} \; ? – Sergiy Kolodyazhnyy Feb 20 at 0:44
  • Can you edit to add a sample of the output you'd like? As in what output you want to what files? I think you want the contents of dir1/foo/ to dir1_foo.txt, and the contents of dir1/bar/ to dir1_bar.txt, etc. but I'm not sure? – ilkkachu Feb 20 at 10:13
  • @ilkkachu you are correct in that assumption. The output is what -printf "%f\n" shows, just a list of the filenames (with extension) stored in dir/subdir. – Faustino Delgado Feb 20 at 14:21
0
Kindly try with below script

it will print up to 9 subdirectories  files using find command keyword maxdepth


    for i in {1..9}; do echo "below are files under $i steep afer parent path subdirectory"; find . -maxdepth $i -type f;echo "================================"; done

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.