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.

2 Answers 2

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'

2
  • 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? Commented Dec 1, 2015 at 10:08
  • @EmmaTebbs, updated to keep the final /. Commented Dec 1, 2015 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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.