Searching for directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for directory
# 1  
Old 04-07-2005
Question Searching for directory

Hi,

I am new to Unix and am using csh to create a script where all files in a directory of a particular type are changed. As an example, if rename.csh tmp .tab .txt is entered at the command line, all of the .tab files in the tmp directory will be changed to .txt files. I have created the code to rename the files but it only changed the files in the directory that I am in. I need help finding and getting to the directory where the user wants to change the file extensions. Also, if you think my code could be improved, I would appreciate knowing how. I would really appreciate any help that you can give me. Thank you.

Here is my code so far:
Code:
#!/usr/bin/csh

set USAGE="USAGE: rename.csh NAME_OF_DIR .OLD_EXTENSION .NEW_EXTENSION"


foreach i ( *$argv[2]* )
  mv $i `echo $i | sed -n s/$argv[2]/$argv[3]/p`
else
echo $USAGE
endif
exit 0

# 2  
Old 04-08-2005
your code doesn't show you going to the directory specified as an argument ...
# 3  
Old 04-08-2005
Tools Add cd before u rename

#!/usr/bin/csh

set USAGE="USAGE: rename.csh NAME_OF_DIR .OLD_EXTENSION .NEW_EXTENSION"

cur=$PWD
cd $argv[1]
foreach i ( *$argv[2]* )
mv $i `echo $i | sed -n s/$argv[2]/$argv[3]/p`
else
echo $USAGE
endif
cd $cur
exit 0
# 4  
Old 04-08-2005
Thanks, that gets me to the directory but for some reason now it only changes one of the files and not all of them. Can you help me with that?
# 5  
Old 04-08-2005
in ksh i'd do something like this ...

Code:
#! /bin/ksh
dir=$1
oldext=$2
newext=$3

cd $dir
for file in *$oldext
do
    mv $file `echo $file | sed -n "s/$oldext/$newext/p"`
done

exit 0


Last edited by Just Ice; 04-08-2005 at 03:22 PM.. Reason: prettier this way ...
# 6  
Old 04-08-2005
Thanks but I need to use csh in this particular case so if I can get an example like that in csh I would appreciate it.
# 7  
Old 04-08-2005
i don't know why a simple script requires csh to be used unless it is homework ... is it?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Searching the file in a directory

Hi Folks, I am using the putty as I need to check the logs, My query is that I know the location of my logs ..that is cd /var /logs/abc.log so I can reach to this place and open the logs in putty, But what About if I do not the location only thing I know the name of the abc.log , and I have... (1 Reply)
Discussion started by: KAREENA18
1 Replies

2. Shell Programming and Scripting

Copy file after searching in a directory

Hi, I am looking for an answer for following senario: I have a text file (base.txt) which consist list of files to be searched like: base.txt abc.txt def.txt fgh.txt Now i am going to search all the listed files in another directory after reading them one by one, once i found the... (10 Replies)
Discussion started by: apjneeraj
10 Replies

3. Shell Programming and Scripting

Searching directory

The command is to search all the matched directory under $HOME, the input i directory is the last argument it works perfectly by itself but after a method call, it wont work cant figure out why dirNameCheck(){ for i in $* do test $1 done noMatch=`find HOME -type d -name $i |... (0 Replies)
Discussion started by: cryogen
0 Replies

4. Solaris

Searching for files in a Directory

Hi, I am trying to write a script that will search in a particular directory and tell me how many files are in there. I then want to be able to email certain users of how many files are in that directory and what the file names are? any help would be great as i am getting confused. thanks (3 Replies)
Discussion started by: Pablo_beezo
3 Replies

5. Shell Programming and Scripting

Searching directory tree

I'm currently trying to write a script that will do the following: search a given directory tree for a file with MMDDYYYY in the name. delete those files only. I can't figure out how to make the script delete the files with the MMDDYYYY in the filename after finding them. Should I export... (7 Replies)
Discussion started by: blane
7 Replies

6. Shell Programming and Scripting

searching each file in a directory for text

what command can i use to search the files in a directory for a text. the output would list the files containing the text. ive tried this but it is not exactly what im looking to do: find . -name "*.xml" -exec agrep searchstring {} \; (2 Replies)
Discussion started by: jim majors
2 Replies

7. Shell Programming and Scripting

Searching files in a directory.Urgent

Hi everyone, I need to search files starting with RPT_0, RPT_1,........ in a directory. How can I implement that through a shell script. Also I want to read the last line of each file after searching them. Can someone help me out in this regard. One more thing how I can extract a particular... (7 Replies)
Discussion started by: srivsn
7 Replies

8. Filesystems, Disks and Memory

Searching for a pattern in a Directory

How to search a given pattern in the files which are present in my current working directory and its subdirectories recursively (1 Reply)
Discussion started by: gandhevinod
1 Replies

9. Shell Programming and Scripting

directory searching

i have a set of directories, all with a set of files inside ie name/week1 there is a selection of files in each directory, and i need to read each one, and find data in each one, although not all the numbers are there can anyone help me please (i know this is kinda vague, just pm... (2 Replies)
Discussion started by: st00pid_llama
2 Replies

10. UNIX for Dummies Questions & Answers

searching for a string in directory

Hi, I have a directory with a couple of thousand logs in it. The log files are created every 5 minutes. I want to search these logs grep for a specific sting and more importantly print the name of the files where that sting was found. e.g. all logs begin om20020927 what I have been... (4 Replies)
Discussion started by: warrend
4 Replies
Login or Register to Ask a Question