Shell script to copy files from on folder to another

I am trying to copy files with specific date and name to another folder. I am very new to shell scripting so i am finding it hard to do that. see the sample code i have written below.

srcdir="/media/ubuntu/CA52057F5205720D/Users/st4r8_000/Desktop/office work/26 nov"
dstdir="/media/ubuntu/ubuntu"
source=/home/bmsc/HOME/data/haguard.xml
destination=/home/bmsc/backup/
find_res = find ./ -type f -name '12*.csv' -mtime -1

so the question is, is it possible to put that find into a if block so that I can copy the files found by find . Any help would be greatly appreciated.

Is this a homework assignment? (Homework and coursework questions can only be posted in the Homework and Coursework Questions forum under special homework rules described here.)

What shell are you using?

If you want to save the output from a command in a variable, you need to use command substitution to run the command and capture its output. Just assigning a command string to a variable does not cause that command to be executed.

Look more closely at the man page for find . The -exec primary is probably going to be much more useful for you than saving the output from find in a variable and trying to figure out how to use that in an if statement. If find ... -exec ... won't work for you, you probably want to pipe the output from find into a while read loop rather than an if command.

Nope this isn't a home work.

however i will try to find out how -exec works. thanks for giving thoughts on this.

Making some very wild guesses from what you showed us before, you might want something like:

srcdir="/media/ubuntu/CA52057F5205720D/Users/st4r8_000/Desktop/office work/26 nov"
dstdir="/media/ubuntu/ubuntu"

find "$srcdir" -type f -name '12*.csv' -mtme -1 -exec cp {} "$dstdir" \;

But I have no idea why you need both a srcdir and a source variable nor why you need both a dstdir and a destination variable???

I am trying to write the script in a natural flow. Like we do in c++ or c# etc. so may be my approach is like that. However I also saw an example of copying files from one folder to another some where that is why i have used those variables.

Please tell us in English what files you want to move from what file hierarchy and to what directory.

What you have said so far seems to be saying that you want to find regular files modified within the last 24 hours with names starting with 12 and ending with .csv where those files are found in the file hierarchy rooted in the current directory (or maybe in the file hierarchy rooted in $srcdir or maybe in the file hierarchy rooted in $source ) and make a copy of the files that meet that criteria to the directory named by $dstdir (or maybe by $destination or some other directory).

You already guessed that right. this command find "$srcdir" -type f -name '12*.csv' -mtime -1 -exec cp {} "$dstdir" \; is doing the work for me. however now what i want is that if the command copies something than i would like to print copy successfully. else unsuccessful.

The cp utility will always give you a diagnostic message if a copy fails. You haven't told us what operating system you're using, but if my guess (based on the names of some of your filesystems) that you're using a Linux operating system is correct, changing:

-exec cp {} "$dstdir" \;

to:

-exec cp -v {} "$dstdir" \;

should give you what you want. If you're using a system where cp doesn't support the -v option, you could change it to:

-exec cp {} "$dstdir" \; -exec printf '%s copied successfully\n' {} \;
1 Like

I am using ubuntu 14.04 lts. I will give a try to the advices you have given. and will let you know how the things are going :slight_smile:
I really appreciate you help in this regard :slight_smile:

If you find that someone's post has helped you solve your problem, you can hit the ":b: Thanks" button at the lower left corner of that post to express your appreciation.

I hope it is working for you. It's past time for me to go to bed...

1 Like

@Don Cragun I hope you are having a pleasurable sleep. Do guide me on one more thing that i also want to create a log file as well. Like it saves when a file is copied with its source and destination locations. Also i want to include in the log file when a copy does not happen. the log file will help me to find out if the things are going smoothly or not.

Does this mean you want a log file written on top of having the messages on screen? Try piping through the tee command. If you wish to catch error msgs as well, you'll need to combine stdout and stderr upfront.

Can you please show me an example ? It would be great :slight_smile:

(untested)

cp -v * $destdir 2>&1 | tee $logfile

will show progress and errors on screen and save them to the logfile at the same time.

1 Like