Visit Our UNIX and Linux User Community


Find a file and xarg mv to append date to file


 
Thread Tools Search this Thread
Operating Systems Solaris Find a file and xarg mv to append date to file
# 1  
Old 09-22-2009
Find a file and xarg mv to append date to file

Hello All,

What I would like to do is search for a file and then run a mv command to rename the file to have todays date appended to it. The find when I run it finds eight or so files and I would like to append a date stamp to each file. If possible using one line command would be great. Can xargs do this or is there a way to do it in exec? Any help would be appreciated.

below is what I have so far, they are the pieces just trying to put it together.


Code:
1.   mv test.out  test.out.`date +%Y-%m-%d`
2.   find . \! -name \*gz -mtime +7 -type f | xargs mv


Thank you,
jack

Last edited by jacktravine; 09-22-2009 at 04:23 PM..
# 2  
Old 09-22-2009
Try that one:
Code:
find . \! -name \*gz -mtime +7 -type f -exec sh -c 'mv "$0" "$0".$(date +%Y-%m-%d)' {} \;

# 3  
Old 09-23-2009
jlliagre,

I ran the code but nothing happened. None of the files had the date appended.


-jack
# 4  
Old 09-23-2009
Okay. Are you sure any file match the find conditions ?
What prints the same command without the -exec clause ?
Code:
find . \! -name \*gz -mtime +7 -type f

# 5  
Old 09-23-2009
jlliagre,

My fault the original find did not find anything. So I changed the find param to find something now. I ran the whole script with the find of +0 and I get an error.

Code:
find . \! -name \*gz -mtime +0 -type f -exec sh -c 'mv "$0" "$0".$(date +%Y-%m-%d)' {} \;

Results of running the above
Code:
./wdns: syntax error at line 1: `(' unexpected
./wdns1: syntax error at line 1: `(' unexpected
./wdns2: syntax error at line 1: `(' unexpected
./wdns3: syntax error at line 1: `(' unexpected
./wdns4: syntax error at line 1: `(' unexpected

Running only the find command
Code:
>find . \! -name \*gz -mtime +0 -type f
./wdns
./wdns1
./wdns2
./wdns3
./wdns4

-jack
# 6  
Old 09-23-2009
My mistake. I'm running OpenSolaris with sh being POSIX compliant.
Solaris 10 and older have a non compliant sh.

Just replace sh by ksh.

Code:
find . \! -name \*gz -mtime +0 -type f -exec ksh -c 'mv "$0" "$0".$(date +%Y-%m-%d)' {} \;

# 7  
Old 10-05-2009
Problem solved

jlliagre,

Thank you for your help. What you sent worked great.


much appreciated,
-jack

Previous Thread | Next Thread
Test Your Knowledge in Computers #50
Difficulty: Medium
Matti Makkonen is considered to be the 'Father of SMS'.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append orginal file date to end of file

Trying to process 1000 or so files. Take original date and append to end of file. Like so: tstpls42.bas tstpls42.bas.Sep-11--2011 Been working along these lines: date=`ll tstpls42.bas |cut -c 46-57 |sed -e 's/]/\-/g' | grep -v '^$'` for i in *.bas ; do j=`ll $i /hpdump/b1 | awk... (1 Reply)
Discussion started by: joeadmin
1 Replies

2. Shell Programming and Scripting

Want it to read the file name and then append date stamp at the end of file?

I was thinking something like for i in `find . -name "*.log.Z"`; do mv $i name.log.Z or something like that? (3 Replies)
Discussion started by: xgringo
3 Replies

3. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 Replies

4. UNIX for Dummies Questions & Answers

append file name with date

Hi guys, I created a very basic script that moves a specified file to a directory that I have set using the mv command. What I would like to do is append the file name with the date and time. So if I was to use the script to move a file called abc.txt, the script should rename the file... (1 Reply)
Discussion started by: jjc
1 Replies

5. UNIX for Dummies Questions & Answers

How To Append Date to File Name - AIX 5.3

Hello, I am a novice user on an AIX 5.3 system. I am running some scripts via crontab and redirecting the output to a file. For example: /home/cognos/ppdev_refresh.sh > /home/cognos/ppdev_refresh.txt I want to append the system date to the file name so it becomes:... (7 Replies)
Discussion started by: Maurice Frank
7 Replies

6. UNIX for Dummies Questions & Answers

Find, copy, and append into one file

Hi, I am trying to do something relatively easy, but am having some trouble getting it to work. I have multiple files called "distances.log" in numerous subdirectories and sub-subdirectories within a directory. I would like the contents of each of these "distances.log" files to be appended to a... (2 Replies)
Discussion started by: euspilapteryx
2 Replies

7. Solaris

gzip a file and append creation date stamp to file

I want to gzip a file and append the creation date to the end of the file. How can I accomplish this task. Basically they are log files which need a creation date stamp appended to make sure they do not overwrite other log files. -jack (3 Replies)
Discussion started by: jacktravine
3 Replies

8. Shell Programming and Scripting

append date to file

Hi I need to append date to filename.Wrote script to get the date from table ,take this date filed and append to my i/p file when call the below script.Any help should be appreciated . Exampel If call the below script a4.sh filename o/p should be filename.2008-02-29 .... (6 Replies)
Discussion started by: mohan705
6 Replies

9. Shell Programming and Scripting

Append date to a file from CRON

Hi, I am trying to append date in DAY_Mon_dd_yyyy at the end of a filename from cron. Cron entry looks as below. (script to execute) > test_file_`date +"a_%b_%d_%Y"` File name created after executing the job is test_file_ I am expecting the filename to be something like ... (8 Replies)
Discussion started by: dncs
8 Replies

10. Shell Programming and Scripting

Need a script to Append date to generated .txt file

Hi, Can anyone plz share their experience with - Building shell script to append the file with date in following format- Filename_MMDDYYYY.txt Thanks in advance (2 Replies)
Discussion started by: prince_of_focus
2 Replies

Featured Tech Videos