4

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
  • Ive 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
4
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.

  • This doesn't work with files with spaces – laebshade Dec 9 '12 at 21:53
  • 1
    If the variables are inclosed in double quotes the spaces should be perserved. – llua Dec 11 '12 at 3:25
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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