I am on a Windows machine. However, I installed Cygwin and updated my environmental variable path which allows me to run Linux commands through a Windows terminal.

I would like to perform the following command:

wc -l * > total 

in many folders. My setup is this:

Parent Folder has 24 subfolders each named PKA1, PKA2, etc., up to PKA24

I want to go into each subfolder. But within each subfolder I need to go further. I need to go down three more levels (only one subfolder in each directory at this point), but the path looks like:


Then in each subfolder at this depth, there are 4 folders:


I would like to go into each one and perform my command. Is there a quick way to do this?


If you know how many nested folders are there, you could try

wc -l * > file
wc -l */* >> file
wc -l */*/* >> file


  • this did not work. I need the file to be put into each directory, not just one large file in the parent directory – Jackson Hart Jul 11 '16 at 16:09
  • this creates one file which is fine but it creates really long columns. Can it create new columns for each file that makes up the final file? – Jackson Hart Jul 11 '16 at 16:40
  • you can create a file for each directory and then use paste file1 file2 file2 > final_file – altroware Jul 11 '16 at 20:27
  • I have been using this a lot. I like actually that it puts them all in one file. Is there a way in the commands above, I can paste them horizontally into the final folder? As opposed to file 1 above file 2 etc.? – Jackson Hart Jul 19 '16 at 21:12

Use a FOR loop.

From your parent folder, do:

FOR /R %G IN (*) DO wc -l %G >>total

This should do the command for every file in that directory.

The /R switch makes it run through every file and >> appends, instead of overwrites the output to your file.

More info:

http://ss64.com/nt/for_r.html http://ss64.com/nt/syntax-redirection.html

  • Have you actually tried this? Cygwin does not understand the Windows format file paths without conversion ... – DavidPostill Jul 9 '16 at 8:24

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.