Find last updated files and rename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find last updated files and rename
# 1  
Old 05-17-2010
Find last updated files and rename

HI
I have a requirement to find the last updated files from a directory whcih has subdirectories and inside them we have files with .txt,.doc,.xls .. extensions. i have to find those files which were updated in the last 1hr and rename the files with respective <sub-directory>_<filename> and copy them to another directory.
i'm able to browse the threads of this forum and found the code to find the last updated files.. but don't know how to find and rename in the mentioned format.

Main directory:
/dataserver/
subdirectories:200906024502,200906024503,200906024520 etc,;
inside /dataserver/200906024502/abc.txt
/dataserver/200906024520 /sample.doc
etc;

Code:
 
ts=`date +%m%d%H%M`; ts=`expr $ts - 250`;touch -t 0$ts stamp1
 find /dataserver/200906024502/ -type f -newer stamp1 -print

please help me with the script.

Thanks in advance.
-Ramesh
# 2  
Old 05-17-2010
Code:
ts=`date +%m%d%H%M`; ts=`expr $ts - 250`;touch -t 0$ts stamp1
for file in $(find /dataserver -type f -newer stamp1 -print)
do  
  Newfile=$(echo $file |awk -F \/ '{for (i=3;i<=NF-1;i++) printf $i "_" ; printf $NF"\n"}' )
  if [ ! -f /Other_folder/$Newfile ] then
     echo cp $Newfile /Other_folder
  fi
done

If the code is succesful, remove echo from the code.

Last edited by rdcwayx; 05-17-2010 at 10:42 PM..
# 3  
Old 05-17-2010
I tried like this.. and it gives erros..
Code:
#!/bin/ksh

ts=`date +%m%d%H%M`; 
ts=`expr $ts - 250`;
touch -t 0$ts stamp1
for file in $(find /dataserver/apps/upload/CARcomment/ -type f -newer stamp1 -print)
do  
  Newfile=$(echo $file | awk -F \/ '{for (i=3;i<=$NF-1;i++) printf $i "_" ; printf $NF"\n"}' )
  
  if [ ! -f /dataserver/apps/upload/tempcopy/$Newfile ] 
  then
     echo cp $Newfile /dataserver/apps/upload/tempcopy/
  fi
  
done

#Remove time_stamp
rm stamp1

exit 0

error:
Code:
awk: syntax error near line 1
awk: bailing out near line 1
cp /dataserver/apps/upload/tempcopy/
awk: syntax error near line 1
awk: bailing out near line 1

# 4  
Old 05-17-2010
change awk --> nawk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to find and rename files

but it's not working. Hello all. I'm running the following command to find files with a specific name and rename them, but the command prompt returns a short 10 seconds after executing and doesn't find or rename anything. What am I doing wrong here? find . -type f | for file in... (3 Replies)
Discussion started by: bbbngowc
3 Replies

2. Shell Programming and Scripting

How to rename lots of files with find?

Can someone help me with this script. I have a bunch of files like this: "2209OS_02_Code" "2209OS_03_Code" "2209OS_04_Code" "2209OS_05_Code" "2209OS_06_Code" "2209OS_07_Code" "2209OS_08_Code" "2209OS_09_Code" "2209OS_10_Code" "2209OS_10_video" and I want to rename them to be like this: ... (2 Replies)
Discussion started by: siegfried
2 Replies

3. Shell Programming and Scripting

How to find the any log which is not updated since particular date?

Hello, Iam running with one issue, since particular date looks like one of the script vanished from the system after restarting of the system. I dont know which scrit it was but definatelt there should be one. but might be some logs would be there which have not updated from that day. so... (2 Replies)
Discussion started by: ajju
2 Replies

4. Shell Programming and Scripting

Find out whether directory has been updated with files in the last 5 minutes or not

Hi, I am trying to work on this script that needs to monitor a Directory. In case there are no files received in that Directory for the last 5 minutes, it has to send out an alert. Could someone please suggest any approach for the same. Note: I did check out various previous psts -... (8 Replies)
Discussion started by: rituparna_gupta
8 Replies

5. 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

6. Shell Programming and Scripting

to find the last updated file from different groups of files.

Hi i have many sets of files as shown below(here i have shown 2 sets) basel_aa_20091030.txt basel_aa_20091130.txt basel_aa_20091230.txt basel_bb_20091030.txt basel_bb_20091130.txt basel_bb_20091230.txt from each set of files i need to select the latest updated file(there are... (3 Replies)
Discussion started by: jagadeeshn04
3 Replies

7. Shell Programming and Scripting

find recently modified/ updated file

hi gurus, i would like to know how can i find logs files which were recently modified or updated? :confused: using this command? find . -name "*.log" -mtime ?? so what should i put for mtime? thanks. wee (9 Replies)
Discussion started by: lweegp
9 Replies

8. Shell Programming and Scripting

Find recently updated files in home directory

Is there a shell command that will allow me to list index files in the /home directory for all users on a server that have been updated within the past 24 hours? (e.g. index.htm .html .php in/home/user1/public_html /home/user2/public_html /home/user3/public_html etc ) (2 Replies)
Discussion started by: Kain
2 Replies

9. UNIX for Dummies Questions & Answers

Find last updated file

Hi all, my problem is teh following: I need to move a file from a folder to another and I usually do it by the get command but in this case I have a list of file in the source folder and I have to select only the lust updated one. Ho to do this? All the files have the same name followed... (4 Replies)
Discussion started by: callimaco0082
4 Replies

10. Shell Programming and Scripting

Find, Append, Move & Rename Multiple Files

Using a bash script, I need to find all files in a folder "except" the newest file. Then I need to insert the contents of one text file into all the files found. This text needs to be placed at the beginning of each file and needs a blank line between it and the current contents of the file. Then I... (5 Replies)
Discussion started by: Trapper
5 Replies
Login or Register to Ask a Question