Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
4  
That reads an awful lot like a homework assignment. –  garyjohn Nov 22 '11 at 1:48
add comment

3 Answers

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
add comment

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
add comment

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

#!/bin/sh

cd /data
list=`ls -rt`

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

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
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.