Home > Net >  HOW TO: Create a file with a list of files via a unix command
HOW TO: Create a file with a list of files via a unix command

Time:10-31

I need to create a file that has a list of files(files already exist with data) using a unix command. Basically move the existing files into a newly created text or csv file.

Syntax: /$dir/$file_name.csv
Ex: /var/data/inbound_data/fusion_po_attachments.txt (or fusion_po_attachments.csv) This path would have n number of files with the same syntax.

/var/data/inbound_data/fusion_po_attachments.txt --main file & this would have below content

/var/data/inbound_data/attachment1.csv
.
.
.
/var/data/inbound_data/attachment50.csv

how can we achieve this? Please point out if any question like this exist. Thanks.

CodePudding user response:

for i in /var/data/inbound_data/*.csv
do
  echo "$i"
done > /var/data/inbound_data/fusion_po_attachments.txt

or the same as a one-liner

for i in /var/data/inbound_data/*.csv ; do echo "$i" ; done > /var/data/inbound_data/fusion_po_attachments.txt

or

find /var/data/inbound_data -maxdepth 1 -name '*.csv' > /var/data/inbound_data/fusion_po_attachments.txt

The condition -maxdepth 1 makes sure to print matching objects only in the starting directory not in subdirectories. If you know there aren't any subdirectories, or if you want files in subdirectories, you can omit this condition.

CodePudding user response:

It's not entirely clear what you want, but it sounds like you want something like:

find /var/data/inbound_data/ -type f -name '*.csv' > /var/data/inbound_data/fusion_po_attachments.txt
  •  Tags:  
  • unix
  • Related