Shell Script for Copy Files and and append modified date

Posted on 2010-04-05
Medium Priority
Last Modified: 2013-12-26
Dear All,

I've asked similar question for Windows and Mac, but I need the same thing for linux.

I need a shell script for this :

I have a folder that contain several files, for example Folder A with 3 files : Susan.log, Mitch.log, Bryan.log. OS : Windows XP

I want to copy those 3 files to folder B, but I need when copied the file name will append date time with format : yyyymmdd. With the yyyymmdd is fetched from the modified date of those 3 files.

File in Source Folder         Modified Date               The result of copy in Folder B
=================        ==============         =============================
Susan.log                           23-March-2010            20100323-Susan.log
Mitch.log                             18-Dec-2009                20091218-Mitch.log
Bryan.log                            7-Dec-2008                 20081207-Bryan.log

Please give me the SH file for these results : copy files from folder A to Folder B, with the files name including the modified date of the corresponding files.
Question by:softbless
  • 2

Expert Comment

ID: 29796611
cp A/Susan.log  B/`date --date '23-March-2010' +%Y%m%d`-Susan.log
cp A/Mitch.log  B/`date --date '18-Dec-2009' +%Y%m%d`-Mitch.log
cp A/Bryan.log  B/`date --date '7-Dec-2008' +%Y%m%d`-Bryan.log

Author Comment

ID: 29796897
I need the script to scan all the files in that folder then copied + append the modified date. Could you complete the script?

Accepted Solution

tty2 earned 2000 total points
ID: 29811049
Append modified date of each file?

cd A; for i in *;do d=`ls -l $i | awk '{print $6, $7, $8}'`; cp $i ../B/`date --date "$d" +%Y%m%d`-$i; done; cd -

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month5 days, 7 hours left to enroll

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question