Skip to content
Advertisement

Centos shell script – Group files starting with the same string

I have a directory with many pdf files. Each file name starts with a numeric ID (5 chars fixed length), as follows:

JavaScript

I’m trying to figure out how to write a shell script in CentOS that reads files in such directory and list them depending on the ID. The shell script should return the following output (on 3 rows) as following:

JavaScript

and assign to each row a variable name (ie: $rowstring).

I would like to group in a string all the files starting with the same ID, and attach them to the same email using mutt email client by command line. So, following the above example:

for each unique $rowstring the command would be:

JavaScript

where $rowstring (the attachments) should look like as follows
For the first email:

JavaScript

For the second email:

JavaScript

For the third email:

JavaScript

I tried to combine ls and awk commands but I’m too newbie to succeed. Thanks everybody.

Advertisement

Answer

The following command:

JavaScript

outputs:

JavaScript

You can then use each line one at a time and send mail.

Advertisement