Problem with Find and rename file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with Find and rename file
# 1  
Old 08-10-2011
Problem with Find and rename file

I want to find a file say IIFT and check its size is zero or not. If its zero then I have to rename anothe file say WWFT , which is in another folder to WWFT$Todaysdate.

I tried below command:
Code:
cd dir2 (*File WWFT is in dir2)
 
find dir/ -type f -name 'IIFT*' -size 0 -exec mv WWFT WWFT$Todaydate \;


But its not working,please tell me why and how it should work?
Also, i have to mail this WWFT$Todaydate file in attachment.

Please guide me for the above task.Smilie

Thanks in advance.

Last edited by Franklin52; 08-10-2011 at 06:43 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-10-2011
Works fine for me, so I'm guessing you're not looking for files in the right places:

Code:
$ mkdir dir
$ cd dir
$ touch IIFTa
$ touch WWFT
$ TD=2011-11-11
$ find . -type f -name 'IIFT*' -size 0 -exec mv WWFT WWFT$TD \;
$ ls
IIFTz  WWFT2011-11-11
$

Note that yours excludes the current directory. I did find . and you did find dir/

If this file is buried several levels deep, you'll probably need find, but if it's always in the same place, there might be an easier way to do it in shell. What's your system, what's your shell?
# 3  
Old 08-11-2011
Hi Corona,

Actually both these files IIFT and WWFT are in separate directories altogether i.e. IIFT is in dir1 and WWF is in dir2.
So I tried this command :

find dir1/ -type f -name 'IIFT*' -size 0 -exec mv dir2/WWFT dir2/WWFT$Todaydate \;

A problem here is though the above command finds the file IIFT* with 0 size, it does not rename the file WWFT. The find command's exit status is sucessfull, but when I looked for the file WWF$TD in dir2, i can find the previous WWFT file only i.e. it is not renamed.Whats going wrong here i need to find out as my script checks for this renamed file first and if it does not found it then only it moves ahead?
I am using HPUX ? I am not exactly aware of shell but when i did echo $SHELL it gave o/p "sh".
# 4  
Old 08-11-2011
Quote:
Originally Posted by ammbhhar
Hi Corona,

Actually both these files IIFT and WWFT are in separate directories altogether i.e. IIFT is in dir1 and WWF is in dir2.
Don't cd into anything, then. Stay in the current directory and tell find to only look inside the dir you want. If all else fails, you can always give mv the absolute paths you want (/path/to/file instead of to/file)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find and rename file recursively

Hi, I have a directory which contains multiple files with .txt extension, i want to rename all these file to .bak extension using find command, this is what i've tried, please help me to correct this : find /home/application/test -name '*.txt' -exec rename 's/txt/bak/' {} \; seems to... (8 Replies)
Discussion started by: mukulverma2408
8 Replies

2. Shell Programming and Scripting

Find and rename part of a file

hi, Need your help. I need to write a script for below.. i have two files in directory /home/abc as below: Watch_20140203_abc.dat Watchnow_20140203_abc.dat I have to copy this file from /home/abc to /home01/home02 after that i have to rename the date part in above two files... (1 Reply)
Discussion started by: Vivekit82
1 Replies

3. Shell Programming and Scripting

How to find and rename of particular pattern in file.?

Hi Guys, I have folder called /input/temp. Inside the folder I have lot of files. I need to find the file of pattern Article_????_test_?????????.txt and replace by format below. Article_????_?????????.txt Below is the one which I tried but it doesn't works can you please help us.... (4 Replies)
Discussion started by: Vinoth Kumar G
4 Replies

4. UNIX for Advanced & Expert Users

Find Gzip rename and mv

Hi all, what i'm trying to configure its to the following, find all files older then 1 min,gzip them ,rename/move with date and extension .gz (example tes.log_2012-07-26.gz) and trying to move them to another folder (gzipped),the command i'm typing its this, find /home/charli/Desktop/test/ -type... (4 Replies)
Discussion started by: charli1
4 Replies

5. UNIX for Dummies Questions & Answers

Find and Replace then rename file

Hi, This is probably quite simple for an expert, but I keep getting confused about the best approach, grep, awk, sed. What I have is a range of files numbered 1 to 100. They go file1.txt file2.txt and so on In each file I need to find and replace a couple of items and rename add a... (5 Replies)
Discussion started by: chickenhouse
5 Replies

6. Shell Programming and Scripting

Help with rename header content based on reference file problem

I got long list of reference file >data_tmp_number_22 >data_tmp_number_12 >data_tmp_number_20 . . Input file: >sample_data_1 Math, 5, USA, tmp SDFEWRWERWERWRWER FSFDSFSDFSDGSDGSD >sample_data_2 Math, 15, UK, tmp FDSFSDFF >sample_data_3 Math, 50, USA, tmp ARQERREQR . . Desired... (7 Replies)
Discussion started by: perl_beginner
7 Replies

7. Shell Programming and Scripting

Find and Rename File using Terminal

I need help finding a file through terminal and then renaming it automatically. Here is what I have so far to find the file: cd /User/Applications find . */SourceM.app/banner.png | while read line; do mv "$line" banner-.png; done I want the script to rename the file "banner.png" to... (6 Replies)
Discussion started by: rbisconti97
6 Replies

8. Shell Programming and Scripting

Find and Rename files using (find mv and sed)

In response to a closed thread for degraff63 at https://www.unix.com/shell-programming-scripting/108882-using-mv-find-exec.html the following command might do it as some shells spit it without the "exec bash -c " part: Find . -name "*.model" -exec bash -c "mv {} \`echo {} | sed -e 's//_/g'\`"... (0 Replies)
Discussion started by: rupert160
0 Replies

9. Shell Programming and Scripting

Find and rename long file names (html)

Hi Guys, I need a help. I have 1130 zip files. Each one of them has files including 1 html file with long file name (includes special charactors, Alphabetic and numbers). I have copied all 1130 zip files to my linux system and extracted using below command. Find . -name "*.zip" -exec... (7 Replies)
Discussion started by: Rajmani
7 Replies

10. Shell Programming and Scripting

Find and rename

Hi, I was wondering if there is a way to find a particular file and then give it as an input to a program and then dump it into another file. Something like this: find ./ -name '*.txt' -exec ~/processText {} > mod.<current_file> \; I've been trying all sorts of weird things but not... (2 Replies)
Discussion started by: Legend986
2 Replies
Login or Register to Ask a Question