Ceng 328 Operating System Lab Quiz1

Sec1:

 

Write a shell script that copies the files including the specified word in the specified directory to the given destination path with specifed names.

 

Usage of the script: copy ceng ece cengcourses ececourses

 

Means: copy all  files, including ‘ceng’ word, in the cengcourses directory to the ececourses directory with changing the word ‘ceng’ to word ‘ece’.

 

copy: script name

ceng: part of the name of files that will be copied

ece: new part of the name of copied files

cengcourses: directory name

ececoýurses: directory name

 

Answer:

old=$1

new=$2

dir1=$3

dir2=$4

 

cd $dir1

for file in *

do

    echo $file

    if test -f $file

    then

       newfile=`echo $file | sed "s/$old/$new/"`

       if test -f $newfile

       then

            echo "ERROR: $newfile exists already"

       else

           echo "moving $file to the $dir2/$newfile ..."

           cp $file $dir2/$newfile

       fi

    fi

done

 

Sec2:

 

Write a shell script that renames all files including the specified word in the specified directory.

 

Usage of the script: rename ceng ece courses

 

Means: renames the all files that include ‘ceng’ word to the ‘ece’ word in the courses directory

 

rename: script name

ceng: old word which will be find and changed by the new word

ece: new word

courses: directory name

 

 

old=$1

new=$2

dir=$3

cd $dir

for file in *

do

    echo $file

    if test -f $file

    then

         newfile=`echo $file | sed "s/$old/$new/"`

         if test -f $newfile

         then

              echo "ERROR: $newfile exists already"

         else

             echo "renaming $file to $newfile ..."

             mv "$file" "$newfile"

       fi

    fi

done

 

 

Sec3:

 

Write a shell script that moves the files including the specified word in the specified directory to the given destination path with specifed names.

 

Usage of the script: move ceng ece cengcourses ececourses

 

Means: move all  files, including ‘ceng’ word, in the cengcourses directory to the ececourses directory with changing the word ‘ceng’ to word ‘ece’.

 

move: script name

ceng: part of the name of files that will be moved

ece: new part of the name of moved files

cengcourses: directory name

ececoýurses: directory name

 

Answer

 

old=$1

new=$2

dir1=$3

dir2=$4

 

cd $dir1

for file in *

do

    echo $file

    if test -f $file

    then

       newfile=`echo $file | sed "s/$old/$new/"`

       if test -f $newfile

       then

            echo "ERROR: $newfile exists already"

       else

           echo "moving $file to the $dir2/$newfile ..."

           mv $file $dir2/$newfile

       fi

    fi

done