PDA

View Full Version : Scripting help



thesource
18th April 2006, 08:59 PM
hello all-

I need some help writing a script.

I would like to script to

1. perform a search for file name with exension
2. rename the folder two levels up with the file name found


example

for file in (find -name *.cat ); do
mv .. $filename.

The only problem is that the output of the find wants to name the .. folder the whole path of *.cat

example

\dirone\dirtwo\myfile.cat

How do I tell it to only use "myfile.cat" to rename the granpa directory .. ?

Can I use sed to extract myfile.cat and pipe that output into another command?


confused here?

ezzetabi
18th April 2006, 09:49 PM
do not try it as root...


#!/bin/bash
AF="$PWD"
FOLDER=~
EXT=cat
A=`find "$FOLDER" -iname '*.'"$EXT"`
IFS=$'\n'
for i in $A
do
DIRNAME=`dirname "$i"` #dirname of the file
FILENAME=`basename "$i"` #filename and dest of renaming
cd "$DIRNAME"/../..
CF=`dirname "$PWD"` #folder that keeps the folder we'll rename

mv "$PWD" "$CF"/"$FILENAME" #renaming
done

cd "$AF"

Firewing1
18th April 2006, 10:40 PM
ls -l | awk '{printf $9"\n"}' | grep \.ext$
will list all files ending in ext... Does that help?
Firewing1