Renaming of files with different extensions on the same path to .found with the help of loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming of files with different extensions on the same path to .found with the help of loop
# 1  
Old 05-05-2010
Renaming of files with different extensions on the same path to .found with the help of loop

hi ,
I have certain files at the same path with differeent extensions like .dat , .txt etc ...........i need to rename them with extension .found at the same path with the help of loop....

also the files names will be like this ;

abc_2010_c1.dat
abc_2010_c2.dat
xyz_2010_c1.txt
# 2  
Old 05-05-2010
Hi,

I have just print the mv command, u can modify according your requirement.

Code:
#!/bin/sh

ls | while read  first_file
do
ext=`echo $first_file | awk -F'.' '{print $2}'`
bsnm=`basename $first_file ".$ext"`
echo "mv $first_file $bsnm.found"
done

# 3  
Old 05-05-2010
@thegeek: use rename command

The rename utility is not available on all systems.

More possibilities:
Code:
for i in *
do
  mv ${i} ${i%.*}.found
done

with sed:
Code:
ls *|sed 's/\(.*\)\..*/mv & \1.found/' | sh

with awk:
Code:
ls * | awk -F\. '{system("mv " $0 " " $1 ".found')}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop a file containing list of file names until the files are found?

Hi, I have a control file which will contain all filenames(300) files. Loop through all the file names in the control files and check the existence of this file in another directory(same server). I need to infinitely(2 hrs) run this while loop until all the files are found. Once a file is found,... (5 Replies)
Discussion started by: laknar
5 Replies

2. Shell Programming and Scripting

Loop renaming files w/ a count problem

:wall: Hello there, basically in my program where im stuck at is when it comes to rename the files in a loop. - the program counts the number of files w a given name (works!) - and then if the number of files is greater or equal to the MAX_VERSIONS (numbers of files allowed w the... (1 Reply)
Discussion started by: thurft
1 Replies

3. Shell Programming and Scripting

List directory name (only once) after multiple file extensions found

Here is a simplified example of my problem. Say I have the following 3 sub-directories; ./folder1 A.txt A.sh ./folder2 B.txt ./folder3 C.txt C.sh I would like to list the directory names which contain both '.txt' & '.sh' type extensions. I have came up with the following code;... (8 Replies)
Discussion started by: mmab
8 Replies

4. Shell Programming and Scripting

Loop to process 2 files with same name in different path

Hello forum members, I hope you can help me with this I don't know hot to reach. I have a list of files in "/home/MyPath1/" and in "/home/MyPath2/". The files have the same name in both folders. (but different content, the content doesn't matter here I think) /home/MyPath1/ filename1.txt... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

5. UNIX for Dummies Questions & Answers

Renaming duplicate files in a loop

Hello, I have a bunch of files whose names start with 'xx' The first line of each file looks something like: a|...|...|...|... , ... In order to rename all of these files to whatever's between the 4th | and the comma (in the first line of that particular file) , I have been using: for... (2 Replies)
Discussion started by: juliette salexa
2 Replies

6. Shell Programming and Scripting

Loop through found files

Hi I am trying to write a script which will loop through all files that end in ".txt" and ask user if they want to delete the file or not #this print out all files dir=/root/etc/ find $dir -name "*.txt" output: 1.txt 2.txt etc but what i really want is 1.txt delete(Y/N): 2.txt ... (11 Replies)
Discussion started by: Calypso
11 Replies

7. UNIX for Dummies Questions & Answers

Batch Renaming: Change files' extensions in many sub-directories

Hi all - I'm trying to rename a large number of files all at once and need some help figuring out the command line syntax to do it. I've already done quite a bit of research with the rename and mv commands, but so far haven't found a solution that seems to work for me. So: The files exist... (10 Replies)
Discussion started by: dave920
10 Replies

8. Shell Programming and Scripting

find command in while loop - how to get control when no files found?

I have the following statement in script: find ${LANDING_FILE_DIR}${BTIME_FILENAME_PATTERN2} -print | while read file; do ... done When there are no files located by the find comand it returns: "find: bad status-- /home/rnitcher/test/....." to the command line How do I get control in... (3 Replies)
Discussion started by: mavsman
3 Replies

9. UNIX for Dummies Questions & Answers

setEnv.sh is found in which directory(path)

Hi, I am new to Unix.I want to set the path and classpath setting for JDK1.4,Weblogic 9.2 and Oracle 9? So i need to set those settings in setEnv.sh file. But i am unable to find that file in my unix environment? Pls. do tell me where this setEnv.sh is stored? In which directory or path?... (8 Replies)
Discussion started by: sachin.tendulka
8 Replies

10. UNIX for Dummies Questions & Answers

cc path problem - no acceptable path found

Hello everyone, I'm a unix noob. I have a powerbook running mac os x 10.4 and for one of my classes I need to install the latest version of php (5.0.5). I'm following the instructions at http://developer.apple.com/internet/opensource/php.html to install but I've run into a problem. The... (2 Replies)
Discussion started by: kendokendokendo
2 Replies
Login or Register to Ask a Question