I have many xyz files that I want to run through awk. I just want to create a new file with an "_s" appended, for each one. Having a little trouble getting the basename syntax correct. Here is what I have.

for i in *.xyz; do awk -F" " '{printf $1 $2 $3}' > $(basename $i)_s.xyz ; done
  • im not sure either, i edited it out
    – lbeazy
    Commented Feb 23, 2016 at 22:11

2 Answers 2


The proper syntax for writing the output of processed files to new files with _s added to the filename part before the extension would be:

for i in *.xyz; do awk -F" " '{printf $1 $2 $3}' $i > ${i%\.*}_s.xyz ; done

the ${i%\.*} part takes trims the $i string from the last occurence of . to the end effectively removing the extension.

In your example you could use ${i%\.xyz}, but that would be less universal.

  • Thanks, this gets it done. I still would like to get it figured out using basename. Just because I have used that before, and with other commands using the "for" loop
    – lbeazy
    Commented Feb 23, 2016 at 22:23
  • Why would you need to call basename in this example? Adding it would create *_s.xyz files in your current directory for *.xyz files in subdirectories. Is that what you want?
    – techraf
    Commented Feb 23, 2016 at 22:27

You need to pass the files names as 'ls *.xyz' and then rename $i.

for i in `ls *.xyz`; do
  mv $i $i_s

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .