0

I have a List.txt with the list of thousand of folders and files of them which looks similar to this

cat List.txt

folder1, filestar
folder2, filefish
folder3, filedoll
folder4, filegoose

and this goes on. And I have filenames in separated directories (names of directories: sample1, sample2, sample3 …) which match the filenames on a List.txt. So the question is how to find the matching filenames with the files of folders in a list? And I found the command which lists only files inside of directories.

ls -lA sample* | awk -F':[0-9]* ' '/:/{print $2}' | awk 'BEGIN { ORS = " " } { print }'

I just don't know how to print only the files which match with the filenames of List.txt

steffen
  • 16,138
  • 4
  • 42
  • 81
Sunny
  • 103
  • 5

1 Answers1

0

Try this:

awk 'NR==FNR{a[$2]=1;next}$NF in a' List.txt FS=/ <(find)
steffen
  • 16,138
  • 4
  • 42
  • 81
  • woow, its working!! Thank you! And I have one more question, what if in that list there are folder, filename, creation time, modification time, ownerofthefile, looks similar to this – Sunny Oct 28 '18 at 00:56
  • `folder1, filestar, 1990/12/12, 1999/10/09 10:35, john` `folder2, filefish, 1989/10/19, 1989/12/12 11:54, Henrik.` `folder3, filedoll, 2000/07/03, 2005/04/09 10:12, jenny` `folder4, filegoose, 1994/09/10, 1997/11/11 10:20, sarah` – Sunny Oct 28 '18 at 00:59
  • @Asel By default, awk splits records by transitions from whitespace to non-whitespace. If that's suitable for your file format, you just count. In your example the file name is the second field, so `$2`. – steffen Oct 28 '18 at 01:00
  • So my task is to find matching file and if they match, to change the modification time to that which is inside of List.txt... I found how to change the timestamp of the file of another file with the command `touch -t ` but I don't know how to combine the code which you wrote and touch command.. Is there a way to do that?? Thank you – Sunny Oct 28 '18 at 01:03
  • @Asel That's a complete different question. You should try and ask another question when you got stuck. Please tick the green check mark if this answer helped you with the (orignial) question. – steffen Oct 28 '18 at 01:05
  • @Asel One last thing: If you want to deal with `, ` (comma plus space) as field separator, you could `awk 'NR==FNR{a[$2]=1;next}$NF in a' FS=', ' file FS=/ <(find)`. – steffen Oct 28 '18 at 01:08
  • oh okey, got it. Thanks! – Sunny Oct 28 '18 at 01:58
  • I tried to research about the command you provided me, but couldn't find proper answer. Can you please briefly explain me this part please? `NR==FNR{a[$2]=1;next}$NF` Thank you @steffen!! – Sunny Oct 30 '18 at 19:17
  • @Asel Sure! `NR==FNR`: https://stackoverflow.com/q/32481877/845034; `a[$2]=1` builds an index on field 2, so we can check if something is indexed later with `... in a`. Then `$NR` is the last field (`$1` is the first element, $2 the second, `NF` contains the number of fields, so `$NF` is the last field). For the second input, we've changed the field separator to `/`, so the last field would be the filename. – steffen Oct 30 '18 at 23:33