2

I want to make a script that puts filenames from the working directory (with a specific extension) in text file with some additional strings:

For example, if I'm interested in files with extension i and files in my directory are:

1.b  1.i  2.i  2.z 3.i

I want to make a text file like:

command_1
command_2 1.i 1.o && command_2 2.i 2.o && command_2 3.i 3.o &
Zanna
  • 69,223
  • 56
  • 216
  • 327
  • Will the rest of the script be same? You just need the files of successive names from current directory to be replaced as the first arguments of the successions of `command_2` ? – heemayl Apr 21 '15 at 19:02
  • yes, also I need put the same filename but with other extension ("o") as the second argument, and etc for the next file. – Gena Kozyukin Apr 21 '15 at 19:37
  • What is your final goal? Just making a text file in the exact format given or running commands ? – heemayl Apr 21 '15 at 21:42

3 Answers3

1

I found a better solution.

Skip making a text file and just made a script to execute multiple files in a folder

#!/bin/bash

for file in *.i*
do
  [[ "${file##*.}" =~ fchk ]] && continue
  qchem "$file" "${file%.*}".out 
done

clear
echo "DONE!"
Zanna
  • 69,223
  • 56
  • 216
  • 327
1

Considering your answer you don't really want to create a text file but just search for files and process them. You can use the find command for that, for example to rename all files ending in .i replacing i by o do:

find -maxdepth 1 -type f -name "*.i" -exec bash -c 'mv "$0" "${0/i/o}"' "{}" \;

Explanations

  • -maxdepth 1 stay in the current directory
  • -type f search files only
  • -name "*.i" search files ending in .i
  • -exec execute the following command on the file, {} is substituted by the file name, needs to be finished with \;
  • bash -c start a subshell running the following commands, by adding "{}" we give it the filename as argument 0
  • mv "$0" "${0/i/o}" rename the file by substituting i by o – just an example of course, can be replaced by any (chain of) commands you like
  • \; finishes the -exec option

For commands that can take more than one file at a time you can substitute \; by + to chain the arguments, see What is the difference between using '+' (plus) and ';' (semicolon) in -exec command?.

dessert
  • 39,392
  • 12
  • 115
  • 163
0

An alternative if for some reason you want to use a file to store the list of files:

ls *.i  > input.txt
for F in $(cat /path/to/input.txt) ; do
...
done;

or, if you have spaces in your filenames

while read F  ; do
...
done </path/to/input.txt
Bruni
  • 10,180
  • 7
  • 55
  • 97