1

I have a number of files that are saved as

Year -> Month -> Day -> bunch of .nc files

I would like to generate a list of all of the directories that contain the nc files. I can list the path of each nc file with:

find /Year/ -name *.nc | sort > directory_list.txt

which finds each .nc file in the sub directories of these folder in this main file. These results are then saved in a text file 'directory_lists'.

/2000/01/01/nc_file1.nc
/2000/01/01/nc_file2.nc
/2000/01/01/nc_file3.nc
/2000/01/02/nc_file3.nc
/2000/01/03/nc_file3.nc

and so on... How is it possible to slightly modify this so that I have a list of each 'Day' directory? This would be similar to the results I get with the command above, but without the information on the nc file included.

I have tried:

find /Year/ | sort > directory_list.txt

but this returns each path

/Year/
/Year/Month
/Year/Month/Day

I would like the outcome to be:

/2000/01/01/
/2000/01/02/
/2000/01/03/

and so on... without the directory name being repeated

I think this is the same as trying to get the directory of the third level folder within the Year directory? Any advice would be appreciated.

1
find /Year/ -name '*.nc' | sed -e 's:/[^/]*$:/:' | sort -u

Will give you the list of directories which contains at least one file whose name match '*.nc'

  • This is nearly what I need. I need another '/' at the end, so that I can end up inside the final directory. Is there a way of adding '/' to the end of the string? – Emma Tebbs Dec 1 '15 at 10:08
  • @EmmaTebbs, updated to keep the final /. – AProgrammer Dec 1 '15 at 10:37
0

egrep '/[^/]+/[^/]+/' directory_list.txt

This gives you all lines that contain exactly 3 /, which happens to be the directory depth you want.

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.