How to append timestamp in the filenames using find?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to append timestamp in the filenames using find?
# 1  
Old 07-12-2013
How to append timestamp in the filenames using find?

Hi,

How to change the filenames with timestamp in sub folders
I have the following code to select the records.

Code:
find . -type f -name '*pqr*' -ctime 1 -print

The following is the example
Code:
app_root_dir="/`echo $ScriptDir | cut -d'/' -f2`"

$app_root_dir/../BadFiles directory
uvw.bad
xyz.bad
mno.bad

$app_root_dir/../TgtFiles directory
uvw.out
xyz.out
mno.out

I like to rename those files with ..
Code:
$app_root_dir/../BadFiles directory
uvw.20130712163028.bad
xyz.20130712163028.bad
mno.20130712163028.bad

$app_root_dir/../TgtFiles directory
uvw.20130712163028.out
xyz.20130712163028.out
mno.20130712163028.out

I already have dt=`date +%Y%m%d%H%M%S`


I have to rename the filenames as in each environment my path(folder name) differs.

Appreciate advice on other commands/options to use.

Last edited by Scott; 07-15-2013 at 04:31 PM.. Reason: Code tags not quote tags
# 2  
Old 07-14-2013
First off, is suggest you do away with these backticks. They really, really, REALLY should not be used any more. Use $(command) instead of `command`.

About your question: use the "-exec" clause of "find", which uses "{}" as a placeholder for the found file. For instance:

Code:
find . -type f -name '*pqr*' -ctime 1 -exec echo "== {} ==" \;

Which would work like "-print" but surround the filenames with double equal signs.

Use this to pass the filename to a small script, which takes the filename as parameter, adds the timestamp and does a "mv" operation:

Code:
timestamp="$(date +%Y%m%d%H%M%S)"
find . -type f -name '*pqr*' -ctime 1 -exec /path/to/script "{}" $timestamp \;

where "script" could look like:

Code:
#! /bin/sh
fSrc="$1"
chTimeStamp="$2"
fTgt="${fSrc#.*}${chTimeStamp}${fSrc%*.}"

mv "$fSrc" "$fTgt"

exit $?

I hope this helps.

bakunin
# 3  
Old 07-15-2013
@bakunin

Your code gives me the following result
Code:
/<path>/uvw.out20130715101359/<path>/uvw.out  and not
/<path>/uvw.20130715101359.out

I could able to google to change it in one line.
Code:
echo "mv ${file} `echo \"$file\" | sed \"s/\(.*\)\./\1\.${dt}./\"`"

The echo above works fine. So I changed it to the following
Code:
dt="$(date +%Y%m%d%H%M%S)"
for file in $(find /app_root_dir -type f -name '*test*' -size 72c)
do
  echo "mv ${file} `echo \"$file\" | sed \"s/\(.*\)\./\1\.${dt}./\"`"
  mv ${file} `echo $file | sed "s/\(.*\)\./\1\.${dt}./`
done

I like to use the above mv command along with sed in find itself.

Thanks
# 4  
Old 07-16-2013
Quote:
Originally Posted by bobbygsk
Your code gives me the following result
Code:
/<path>/uvw.out20130715101359/<path>/uvw.out  and not
/<path>/uvw.20130715101359.out

Then your input is different from the example you gave, which doesn't have absolute paths at all ("find . ..."). I suggest you consult the man page of "ksh" or "bash" about "variable expansion" and modify my code accordingly to get the results you want.

Your solution is quite inefficient as well as inherently dangerous, because:

Quote:
Originally Posted by bobbygsk
Code:
echo "mv ${file} `echo \"$file\" | sed \"s/\(.*\)\./\1\.${dt}./\"`"

This will not work once the path name of the file contains spaces or more than one period character, both perfectly legal possibilities. In addition, the backticks are deprecated and the whole construction with an external program ("sed") which is fed another process ("echo ...|") will consume 2 fork()s for every single file (and the backticks a 3rd fork()), which is quite taxing in terms of resources: memory and CPU time.

Quote:
Originally Posted by bobbygsk
So I changed it to the following
Code:
dt="$(date +%Y%m%d%H%M%S)"
for file in $(find /app_root_dir -type f -name '*test*' -size 72c)
do
  echo "mv ${file} `echo \"$file\" | sed \"s/\(.*\)\./\1\.${dt}./\"`"
  mv ${file} `echo $file | sed "s/\(.*\)\./\1\.${dt}./`
done

This adds even more external calls and the expansion in the subshell ("$(...)") at the head of the for-loop will break once the number of files reach a certain limit. The reason being the way the shell evaluates input lines: subshell substitutions like yours are first executed in a subshell, then their result (=output on stdout) is replaced for them. If your "find" creates a long list you eventually hit the maximum number of characters for input lines, which is a kernel constant (MAX_LIN) defined in "/usr/sys/include/limits.h".

I suggest replacing the for-loop with a while-loop and a pipeline if you insist doing it this way, better yet, do it like i suggested.

I hope this helps.

bakunin
This User Gave Thanks to bakunin 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

Append timestamp create .trg file for all content of an unzipped archive

Hi, I have a test.zip archive that contains test.zip --> (file_1.txt, file_2.txt , file_3.txt) I need to unzip the file like this, file_1_timestamp.txt file_1_timestamp.trg file_2_timestamp.txt file_2_timestamp.trg file_3_timestamp.txt file_3_timestamp.trg Could you please let me know... (7 Replies)
Discussion started by: Shandel
7 Replies

2. Shell Programming and Scripting

Get filenames without timestamp

Hi, In my previous post I looked for timestamp to be added to the filename https://www.unix.com/shell-programming-scripting/230603-how-append-timestamp-filenames-using-find.html Now how do I select those files that do not have timestamp in the filenames. I tried the following. My file has... (3 Replies)
Discussion started by: bobbygsk
3 Replies

3. Shell Programming and Scripting

Use sed to append text to filenames if text not already present

I have some html with hrefs that contain local links to pdf filenames. These filenames should have standardised names, i.e. there should be a label prior to the ".pdf" filename suffix. There can be many of these links on a single line of text and some may already have the label. For example ... (13 Replies)
Discussion started by: adb
13 Replies

4. UNIX for Dummies Questions & Answers

find & remove characters in filenames

I have a group of files in different directories with characters such as " ? : in the file names. How do I find these files and remove these characters on mass? Thanks (19 Replies)
Discussion started by: barrydocks
19 Replies

5. Shell Programming and Scripting

Print and Append the matching filenames

Hi, I want to check all files in a folder for some specific start string and append all matching filenames with _1, _2 .... for each file found in the directory. But, $file below gives me all details of the files like access permissions, owner and filename etc. I just want all the filenames... (3 Replies)
Discussion started by: chetancrsp18
3 Replies

6. Shell Programming and Scripting

awk help with find command and filenames with spaces

I have the following code: find /usr/local/test5 -type f -mtime +30 -exec ls -l {} \; | awk '{print $5, $6, $7, $8, $9}' I have this as output: 14 Aug 12 00:00 /usr/local/test5/file1 14 Aug 12 00:00 /usr/local/test5/lastname, The bolded part is where I run into trouble. The actual... (4 Replies)
Discussion started by: streetfighter2
4 Replies

7. UNIX for Dummies Questions & Answers

Append file with grep output but add timestamp?

I've setup a cron job that greps a file every five minutes and then writes (appends) the grep output/result to another file: grep "monkey" zoo.log | tail -1 >> cron-zoo-log Is there any way I can add the date and time (timestamp) to the cron-zoo-log file for each time a new line was added? ... (12 Replies)
Discussion started by: Sepia
12 Replies

8. Solaris

feeding filenames to find command

Hi, I am having set of files whose names are stored in a file say "filelist.txt" Now, I want to find all files contained in "filelist.txt" from my parent directory. Is there any way to let find command understand "filelist.txt" just like we have option -f in awk. I donot want to run a... (4 Replies)
Discussion started by: sanjay1979
4 Replies

9. Shell Programming and Scripting

find filenames like unix commands

Hi, I need to write a small script to search in some specific directories to check if any file is present with a unix command name... Means if the directory contains any files like cat, vi, grep, find etc i need to list those files into a file. Please help Thanks, D (6 Replies)
Discussion started by: deepakgang
6 Replies

10. UNIX for Dummies Questions & Answers

find lowercase filenames

How do I write the command to find all files with any lower case letters in the filename? I have tried find . -name *\(a-z\) and a lot of combinations like that, without success. thanks JP:confused: (4 Replies)
Discussion started by: jpprial
4 Replies
Login or Register to Ask a Question