cancel
Showing results for 
Search instead for 
Did you mean: 
jeesshnasree
Starfighter Starfighter
Starfighter
  • 1,142 Views

how to find only 24 hours files in Linux for copy from one path to another path

Hello All,

 

My resources:

1)Linux OS 7  (execution via terminal)

Question :

folders & files copy both copy from one X FS path to Y FS path

Note : if  same name file created next day then not overwrite original file in Y FS 

scenario:

X file system path 

/opt/x/jboss/jvm/log/{any type log}

/opt/x/tomcat/logs/{any type log}

 

Y file system path 

/archive/y/jboss/jvm{any type log}

/archive/y/tomcat/logs/{any type log}

-----------------

copy folders & files from X path FS to Y path FS  ( while copy from X path if any newly folder creates then copy newly created folder  with file  from X path to Y path ) .

Requirement is copy folder already exists ( not overwrite) & file (not overwrite but if same name  already exists comes from X FS path  then keep old as backup  then copy new file in Y FS path)

 

Note : condition 

copy only 24 hours generated files as per folder

Please provide me if any comand or script for above mentioned criteria .

 

Please provide your valuable suggestions & guidance .

I am waiting for your valuable information.

 

Regards,

Jeesshnasree

 

1 Reply
Chetan_Tiwary_
Community Manager
Community Manager
  • 1,139 Views

Hello @jeesshnasree !
Thanks for reaching out !

Getting a pre-configured shell script or command from others here will not help in your learning process. 

I will try to help you with the skeleton of the things that you need to ponder over here :

1. You have a source path : src/opt/x/jboss/jvm/log/

2. You have a destination path : dest/archive/y/jboss/jvm/

3. Copying one file from src to dest : cp command :  cp /filesourcepath /filedestinationpath

4. Rename a file( backing up a file )  : mv command : mv oldfilename newfilename

5. Find command  to find files ( for 24 hours ago modified time ) you can use :                                         find "$src" -type f -mtime -1     

refer this command man page here for -m directive : https://man7.org/linux/man-pages/man1/find.1.html       under positional options --> -daystart

Now if you know these concepts then you will have to create a shell script which will have loop & conditionals eg.  forif-else :

  •    Get a list of all files in the source directory that were generated within the past 24 hours : assign a variable file to it eg.  file=$(find "$src" -type f -mtime -1)
  • Then use for loop for all the files in the list above and copy it to the destination directory using if-else conditional statement. Refer for loop here : https://www.redhat.com/sysadmin/bash-scripting-loops 
  • Check if the file already exists in the destination directory using if statement  refer : https://linuxize.com/post/bash-check-if-file-exists/ 
  • If yes then rename the file using mv command
  • use Else statement to copy the file to destination dir : cp $file $dest/$file

Please note that there can be multiple ways we can achieve it - but the above is the basic logic to achieve this objective. 

This is a basic shell script and you should be able to formulate it if you understand the above topics.

 

Join the discussion
You must log in to join this conversation.