Rename file with certain conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename file with certain conditions
# 1  
Old 07-17-2012
Rename file with certain conditions

Hi,

Trying to rename a file nexus-1234 to nexus-$randomnumber.

This is what I have so far...


Code:
number=$RANDOM$RANDOM$RANDOM$RANDOM;
let "number %= 10000000000000000";
LOGFILE=test.log

for filename in ./*
 do	

   if [ -f "$filename" ]   
       then
	    file="${filename#*/}";            
	    if [[ "$file" == nexus* ]]
	    then
              var="nexus-1234"
		new_var= "${var/[0-9]*//}$number";
              echo $new_var >> $LOGFILE
		mv ./$var ./$newvar             
  	    fi  
   fi   
 done;

When I run this, I am getting.....

Code:
Command '"./changeBatchID.sh"'
failed with return code 0 and error message
./changeBatchID.sh: line 14: nexus-/1515109127742028: No such file or directory
mv: `./nexus-1234' and `./nexus-1234' are the same file.

Any help is appreciated. Thanks!

Last edited by zaxxon; 07-17-2012 at 02:31 PM.. Reason: some more code tags
# 2  
Old 07-17-2012
Code:
for FILE in nexus-*
do
        echo mv "$FILE" nexus-${RANDOM}
done

# 3  
Old 07-17-2012
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print lines that meet conditions and have value in another file

I am trying to use awk to print lines that satisfy either of the two conditions below: condition 1: $2 equals CNV and the split of $3, the value in red, is greater than or equal to 4. ---- this is a or so I think condition 2: $2 equals CNV and the split of $3, the value in red --- this is a... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Script to change the file content using some conditions

Hello, I would like to change the content of the file that has few blocks starts with 10 and ends with ";" File1.txt 10 bonuses D 20 MATCHED 30 UPD COL 40 (SOL=30) 20 NOT MATCHED 30 INS COL 40 (SOL=30) ; 10 bonuses D 20 MATCHED 30 UPD COL 40... (5 Replies)
Discussion started by: Mannu2525
5 Replies

3. Shell Programming and Scripting

awk to update file based on 5 conditions

I am trying to use awk to update the below tab-delimited file based on 5 different rules/conditions. The final output is also tab-delimited and each line in the file will meet one of the conditions. My attemp is below as well though I am not very confident in it. Thank you :). Condition 1: The... (10 Replies)
Discussion started by: cmccabe
10 Replies

4. Shell Programming and Scripting

awk to filter file based on seperate conditions

The below awk will filter a list of 30,000 lines in the tab-delimited file. What I am having trouble with is adding a condition to SVTYPE=CNV that will only print that line if CI= must be >.05 . The other condition to add is if SVTYPE=Fusion, then in order to print that line READ_COUNT must... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

Help with Creating file based on conditions

Can anyone please assist? I have a .txt file(File1.txt) and a property file(propertyfile.txt) . I have to read the vales from the property file and .txt file and create the output file(outputfile.txt) mentioned in the attachment. For each record in .txt file,the below mentioned values shall be... (20 Replies)
Discussion started by: vinus
20 Replies

6. Shell Programming and Scripting

Split File based on different conditions

I need to split the file Conditions: Ignore any record that either starts with 1 or 9 Split the file at position 404 , if position 404 is abc or def then write all the records in a file > File 1 , the remaining records should go in to a file > File 2 Further I want to split the... (7 Replies)
Discussion started by: protech
7 Replies

7. Shell Programming and Scripting

While loop reading file with multiple conditions

Hi Am trying to print the PIDs of process in a file and trying to grep any PID from that file I set the if condition as $value != "PID" and $value != "-" Assign that number to a variable Am confused since am using while loop to read the line from file and again if condition to check those... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

8. Shell Programming and Scripting

[Script] Conditions on parsing file

Hello, I open a new POST, i consider that this is resolved https://www.unix.com/shell-programming-scripting/215803-create-file-comment-script.html But i wish improve it. In case 1, I would like to test the input file $1. If $1 exist with no parameters but only comments, then send a message... (2 Replies)
Discussion started by: amazigh42
2 Replies

9. UNIX for Dummies Questions & Answers

How to get remove duplicate of a file based on many conditions

Hii Friends.. I have a huge set of data stored in a file.Which is as shown below a.dat: RAO 1869 12 19 0 0 0.00 17.9000 82.3000 10.0 0 0.00 0 3.70 0.00 0.00 0 0.00 3.70 4 NULL LEE 1870 4 11 1 0 0.00 30.0000 99.0000 0.0 0 0.00 0 0.00 0.00 0.00 0 ... (3 Replies)
Discussion started by: reva
3 Replies

10. Shell Programming and Scripting

validating a file based on conditions

i have a file in unix in which the records are like this aaa 123 233 aaa 234 222 aaa 242 222 bbb 122 111 bbb 122 123 ccc 124 222 In the output i want only the below records aaa ccc The validation logic is 1st column and 2nd column need to be considered if both columns values are... (8 Replies)
Discussion started by: trichyselva
8 Replies
Login or Register to Ask a Question