Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Use a for loop to go through the filenames in the directory. In this version, only loop through the filenames that do not have a ".old" extension

Inside the loop, rename the file to the new name.

share|improve this question
That reads an awful lot like a homework assignment. – garyjohn Nov 22 '11 at 1:48

As this isn't a homework question, and you just want to know the best way to do this, here is a way to do it with find:

find /startfolder -not -name '*.old' -exec mv {} newname_{} \;

This will get all files that don't have the suffix 'old' and rename them with the prefix newname_

No for loop required.

share|improve this answer

Assuming access to bash 4+ (check with bash --version) this can be done with a two-liner:

shopt -s extglob
for f in !(*.old); do mv -- "$f" new_"$f"; done
share|improve this answer

You need to replace what's between do and done this is an example of how you can use $f


cd /data
list=`ls -rt`

for f in $list
    c=`grep -c or=90 $f`
    if [ "$c" -gt "0" ]; then    
        echo "file is $f and it has $c"

cd -
share|improve this answer
This will break for any filenames with spaces, since for splits on spaces by default; and for filenames containing newlines, since ls splits on newlines. – evilsoup May 28 '13 at 0:45

Your Answer


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.