conditional renaming of files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers conditional renaming of files
# 1  
Old 01-27-2010
conditional renaming of files

Hello,

I want to rename all the files in my directory according to the following criterion:

IF the original filename does not end in 0
THEN rename the file to "original filname" but with the last character preceded by the string: _copy

IF the original filename ends in 0
THEN take the last character off of the filename


For example:

If my directory contains the following two files:

test0
test1

After running my command I should have them renamed to:

test
test_copy1

Is this possible or too complicated ??
# 2  
Old 01-27-2010
Code:
$ ls test*
test0 test1

$ ls test* | sed -e p -e 's/0$//;s/[1-9]$/_copy&/' | xargs -n2 mv

$ ls test*
test test_copy1

# 3  
Old 01-28-2010
Thank you very much Scottn,

I actually had to reverse the order of those substitution commands to make it work (because removing the 0 first, causes the last character to be preceded by '_copy' if the second last character was a number)

But I figured it out, thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX - 2 tab delimited files, conditional column extraction

Please know that I am very new to unix and trying to learn 'on the job'. I'm only manipulating large tab-delimited files (millions of rows), but I'm stuck and don't know how to proceed with the following. Hoping for some friendly advice :) I have 2 tab-delimited files - with differing column &... (10 Replies)
Discussion started by: GTed
10 Replies

2. Shell Programming and Scripting

Conditional deletion of files based on extension

Hello All, I have some files like file, file.chk, file.sem and file.temp in huge. I would like to delete some files based on following criteria. 1. Unconditionally delete .sem and .temp files 2. If we found the actual file, don't remove .chk file, otherwise remove .chk file as well for... (5 Replies)
Discussion started by: VasuKukkapalli
5 Replies

3. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

4. Shell Programming and Scripting

Conditional replacement in CSV files

Hello, I have many CSV files with variable number of rows and columns. Sample of few problematic CSV files. ,,Price,Price,Price,Price,Price,Price,Price,Price,Price,Qty Date,Sl,AAA,BBB,CCC,DDD,EEE,FFF,GGG,HHH,PriQueue,%busy 30/07/2014,1,AAA,BBB,CCC,DDD,EEE,FFF,GGG,HHH,NA,0... (8 Replies)
Discussion started by: reddyr
8 Replies

5. Red Hat

RPM package, conditional adding files in "%files" section

Hello, Is it possible to specify a list of files to be included in an RPM package (section "% files") according to some condtions. For example, if a particular condition is true, do not include the file "X". If not include it. Thank you for your help. Regards (1 Reply)
Discussion started by: louzorios
1 Replies

6. Shell Programming and Scripting

Conditional Looping In Files

I have a req. where i need to read data from multiple files and take counts of row which satisfy the condition. e.g.: FILE1: Col1 Col2 Col3 12 ab cd 15 de fg 25 gh tm FILE2: Col1 Col2 Col3 21 ab1 cd1 13 de1 fg1 25 gh1 tm1 --- --- FILE-N... i need to find the count of rows... (6 Replies)
Discussion started by: kunal007
6 Replies

7. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

8. UNIX for Dummies Questions & Answers

Need help renaming files

I just can't figure this one out. I have a lot of files name (for example) ABC1234.5678.ext I need to rename these files U0105678PQRS So I'm removing everything before the first "." I'm keeping "5678" in the file name Adding "U010" to the front of "5678" Dropping the ".ext" extension ... (5 Replies)
Discussion started by: bbbngowc
5 Replies

9. Shell Programming and Scripting

Renaming Files

Hi Alll, I have a script that we use on the servers to change the name of files that have spaces in the name: #!/bin/tcsh set n = 0 foreach f ( * ) echo $f | grep " " if ( $? == 0 ) then mv "$f" `echo $f | sed -e "s/ /_/g"` @ n += 1 endif end echo $n changed I need to write a... (2 Replies)
Discussion started by: abch624
2 Replies

10. UNIX for Dummies Questions & Answers

conditional deletion of log files

i am a newbie and learning the ropes.........want to know how can i include a piece of code in a script (which redirects log files to a company standard out file) to delete the log files which are empty but should retain only those which has some process information in it......this should happen... (3 Replies)
Discussion started by: sonali007
3 Replies
Login or Register to Ask a Question