Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can be done coping files that have some suffix at the end, into same dir with the smallest command possible:

Example have directory containing files:

  • cassandra.yml.example
  • database.yml.example
  • facebook.yml.example
  • cache.yml.example
  • system.yml.example

need to copy them and have names like this:

  • cassandra.yml
  • database.yml
  • facebook.yml
  • cache.yml
  • system.yml
share|improve this question
    
I`ve figured about such thing: for x in ./config/*.example; do cp $x ./config/basename $x .example; done –  Vitaliy Yanchuk Dec 7 '12 at 12:12
2  
you can even get rid of the basename call (at least with recent-enough bash): for x in ./config/*.example; do n=${x##*/}; cp $x dest/dir/${n%.example}; done –  peterph Dec 7 '12 at 12:59
    
If you use zsh, zmv '(*).example' '$1' –  Kevin Dec 8 '12 at 0:22
add comment

2 Answers

for x in /path/to/*.example
do
  cp "$x" "${x%%.example}"
done

Will make a copy without the .example into the same folder as the source file.

share|improve this answer
    
This doesn't work with files with spaces –  laebshade Dec 9 '12 at 21:53
    
If the variables are inclosed in double quotes the spaces should be perserved. –  llua Dec 11 '12 at 3:25
add comment

Requires find and a shell that supports string manipulation and process substitution (i.e. Bash and any compatible):

while read file
    do cp $file ${file%%.ext}
done < <(find -type f)

If you want to copy all files without extensions, you can use the glob (*) instead of ext.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.