• Post Reply Bookmark Topic Watch Topic
  • New Topic

Archiving files in UNIX  RSS feed

 
Lubosh Bazant
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In unix I want to move all the files in one directory to another directory. The files in new directory should have the same name but also a date attached to the name of the file. I tried this but it won't work:
mv file_name.* dir/file_name.*.`date "+%m%d%y_%H%M%S"`
please help, I am not too skilled in UNIX
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The asterisk that you use in your incantation does what is called globbing, or filename expansion. It matches every file that fits your criteria and replaces it with the result of the glob. So you end up with all the asterisks being replaced first with the results being interpreted as arguments to mv. In mv, the last argument is the target; there is no internal facility to iterate through two sets of globbing expressions and copy an element name from one into an element name of the other.
What you need is a loop structure that treats every source file one at a time. The following would do the trick in the Bourne or Korn shell:

The value i stands in for a file, one at a time, no that a mv operation can be performed on each one.
 
Cindy Li
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a tar file, then copy tar to the directory, then extract the tar file.
e.g.
#cd /firstdir
#tar cvf my.tar .
#cp my.tar /seconddir
#cd /seconddir
#tar xvf my.tar
It retrieve all files and maintains the date and time.
I am seeing the light ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!