copy all files matching the request and change the extension at the same time


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers copy all files matching the request and change the extension at the same time
# 15  
Old 12-23-2011
Quote:
---------- Post updated at 11:55 PM ---------- Previous update was at 11:54 PM ----------

if I change the code a little bit to this:

Code:
ls *$3*.$1|while read FILE
do
NEWFILE=$(echo $FILE|cut -d"." -f1)
echo $FILE": " $FILE " "$NEWFILE: " " $NEWFILE.$2
cp $FILE $NEWFILE.$2
done

then it copies only the files, including string, now I just need to change $3 in the new file to $4

---------- Post updated 24-12-11 at 12:29 AM ---------- Previous update was 23-12-11 at 11:55 PM ----------

I've tried this, but it's not working:

Code:
sed 's/$3/$4/g' *.$2



Try this: ( I should be in bed...)
Code:
TOFILE=$(printf $NEWFILE|sed s/$3/$4/)
cp $FILE $TOFILE.$2

I will have to leave now... my eye sight is hopeless...

Last edited by vbe; 12-24-2011 at 04:48 AM.. Reason: quotes
# 16  
Old 12-23-2011
this only creates one ".ext2" in the same directory, without any name, with only the second extension

but I found another solution now, thank's!

---------- Post updated at 04:45 AM ---------- Previous update was at 01:15 AM ----------

how do I actually remove my threads?
could you please do it, if only the moderators can?
thanks

Moderator's Comments:
Mod Comment You cannot remove your threads. They are property of the forums and also copyright by the forums.

Last edited by vacuity93; 12-23-2011 at 07:50 PM..
# 17  
Old 12-24-2011
Quote:
this only creates one ".ext2" in the same directory, without any name, with only the second extension
The solution works, though not the most elegant except to demonstrate that a goal can be achieved in unix by many means using all sorts of commands, some are more efficient, some, more accessible, some, very tricky...
I would imagine the most straightforward would have been using sed to rename in one pass... Only I avoided the solution for I have not found all the chars on this keyboard (powerbookG4... still looking for backslash andmore important the tilde!!! - How do mac users writes scripts?) and find myself very uncomfortable.

Final demo:

Code:
$  #--- create the sample files ---#
$
$ touch tst001.txt tst002.txt file.txt
$ vi rename002
$ chmod 774 rename002
$ r vi
ls *$3*.$1|while read FILE
do
   NEWFILE=$(echo $FILE|cut -d"." -f1)
   echo $FILE": " $FILE " "$NEWFILE: " " $NEWFILE.$2 "\$3: "$3" \$4: "$4
   #cp $FILE $NEWFILE.$2
   TOFILE=$(printf $NEWFILE|sed s/$3/$4/)
   echo TOFILE: " "$TOFILE
   cp $FILE $TOFILE.$2
   sleep 1
done

~                                                                                                                    
~    
"rename002" 12L, 273C
$ ./rename002 txt bin tst change
tst001.txt:  tst001.txt  tst001:   tst001.bin $3: tst $4: change
TOFILE:  change001
tst002.txt:  tst002.txt  tst002:   tst002.bin $3: tst $4: change
TOFILE:  change002
$ ll *.bin
-ksh: ll: not found [No such file or directory]
$ ls *.bin
change001.bin	change002.bin
$

This User Gave Thanks to vbe For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to copy subfolder and files to matching directory

The bash executes but returns no results and the set -xv showed while the $run variable in blue, was extracted correctly, the $match value in green, was not, rather both values in home/cmccabe/Desktop/f1 were extracted not just the matching. There will always be an exact match from the $run to... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

3. Shell Programming and Scripting

Copying multiple files and appending time stamp before file extension

Hi, I have multiple files that read: Asa.txt Bad.txt Gnu.txt And I want to rename them using awk to Asa_ddmmyytt.txt and so on ... If there is a single command or more efficient executable please share! Thanks! (4 Replies)
Discussion started by: Jesshelle David
4 Replies

4. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

5. Shell Programming and Scripting

Copy files matching multiple conditions

Hello How do i copy files matching multiple conditions. Requirement is to search files starting with name abc* and def* and created on a particular date or date range given by the user and copy it to the destination folder. i tried with different commands. below one will give the list ,... (5 Replies)
Discussion started by: NarayanaPrakash
5 Replies

6. Shell Programming and Scripting

Copy files with extension .sh

Hi all... I am trying to copy all my shell script to some directory using following command, I want to simplify it by not using awk..please some one help me.... find -name "*.sh" | awk -F"/" '{a=$NF;gsub(".sh",x,a);cmd="cp"" " $0" ""/home/akshay/MY_ALL/"a"_"++i".sh";system(cmd)}' (3 Replies)
Discussion started by: Akshay Hegde
3 Replies

7. UNIX for Dummies Questions & Answers

Copy files with same name but different extension from 2 different directory

Hi all, i have 2 directory of files, the first directory(ext1directory) contain files of extension .ext1 and the second directory(allextdirectory) contains files of multiple extensions (.ext1,.ext2,.ext3,..) so i want to copy the files from directory 2(allextdirectory) that have the same name... (8 Replies)
Discussion started by: shelladdict
8 Replies

8. UNIX for Dummies Questions & Answers

File access time does not change on some files

Hey All, I want to get the access time of files in a directory. I used ls -lu on a directory and picked a file that had the access time of Mar 1 and used cat to get the contents of the file. Then I used the ls -lu again and the access time changed on that file. Perfect !! Now if I cat a... (10 Replies)
Discussion started by: vipulgupta0
10 Replies

9. Shell Programming and Scripting

copy files with new extension in same directory

I've been able to find all the extensionless files named photos using the command: find /usr/local/apache/htdocs -name photos -print0 I need to copy those files to the name photos.php in their same directory. I've found a bunch of xarg examples for moving to other directories but I wasn't... (7 Replies)
Discussion started by: dheian
7 Replies

10. Shell Programming and Scripting

[Help]RegEx, Putty, Copy Files Insensitive Matching, SSH

Alright, basically we're in the whole where we can't tar/gzip a folder since its to big so how do I copy files to a new folder for example I got files from a-Z, i want to copy all files which starts with a A or a into another folder heres file structure ./backups/A ./backups/B... (11 Replies)
Discussion started by: Lamonte
11 Replies
Login or Register to Ask a Question