Shell script to copy files from on folder to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to copy files from on folder to another
# 1  
Old 12-15-2015
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.

Code:
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.
Moderator's Comments:
Mod Comment Please use CODE tags (note ICODE tags) when displaying full-line and multi-line sample input, sample output, and code segments.

Last edited by Don Cragun; 12-15-2015 at 01:56 AM.. Reason: Change some ICODE tags to CODE tags.
# 2  
Old 12-15-2015
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.
# 3  
Old 12-15-2015
Nope this isn't a home work.

however i will try to find out how -exec works. thanks for giving thoughts on this.
# 4  
Old 12-15-2015
Making some very wild guesses from what you showed us before, you might want something like:
Code:
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???
# 5  
Old 12-15-2015
Quote:
Originally Posted by Don Cragun
Making some very wild guesses from what you showed us before, you might want something like:
Code:
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.
# 6  
Old 12-15-2015
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).
# 7  
Old 12-15-2015
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

Shell script which will check the target file and folder exists and copy it

Hi All, I am a beginner in this and trying to write a shell script in linux which will : 1. Ask for a file name and check if its exists. 2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists. 3. If target folder... (3 Replies)
Discussion started by: ashish_neekhra
3 Replies

3. Shell Programming and Scripting

How to copy files with the same filenames as those in another folder to that same folder?

Hello All A similar question like this was asked before but I need to change part of the question. I've two folders, Folder A contains some image files in 150 subfolders; Folder B contains text files in 350 subfolders. All image files in Folder A have the same filename as the text... (5 Replies)
Discussion started by: chlade
5 Replies

4. Shell Programming and Scripting

Shell script that lists files with different owner than the folder

Hello, I'm trying to write a script which is listing files based on different preferences, like filetype or permissions. All is fine, except for one: I want to list files in /home which has a different owner than the home directory it is in. Here is an example: /home/UserA is the directory, and... (10 Replies)
Discussion started by: Zwiebi
10 Replies

5. Shell Programming and Scripting

How to unzip files from folder in shell script (ksh)?

I have a folder (C:\shellprg\input\) containing .CSV, .zip, .gz files. 1] I want to find all .zip/.gz files from folder (C:\shellprg\input\). 2] unzip/uncompress files into the same folder (C:\shellprg\input\) through shell script. I am using below commands for unzip files, unzip <filename>... (2 Replies)
Discussion started by: Poonamol
2 Replies

6. Shell Programming and Scripting

How to Process input files from folder in shell script?

Hi, I want to process all input files available into folder (C:\ShellPrg\InputFile\) Input files are abc.CSV , XYZ.zip (zip of CSV file), PQR.gz (zip of CSV file). I want to check the extension of file, If its .zip/.gz then need to unzip the file as .CSV I want to parse line by line of... (2 Replies)
Discussion started by: Poonamol
2 Replies

7. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

8. Shell Programming and Scripting

HOW TO CHECK ONLY .C FILES EXISTS OR NOT IN A FOLDER using IF in C shell script?

Hi friends.. I hav a problem.... I dont know how to check .c files exists r not in a folder using IF in C shell script actually i tried like this if(=~ *.c) even though some .c files or there in the current folder..it is not entering int o the if control statement...... (17 Replies)
Discussion started by: p.hemadrireddy
17 Replies

9. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies

10. Shell Programming and Scripting

Shell script to find out 2 last modified files in a folder..PLZ HELP!!!!!!!!!

hi all, I need to find out the last 2 modified files in a folder.There is some way by which,we can check the timestamp and find out..??please help this is urgent. Thanks in Advance Anju (3 Replies)
Discussion started by: anju
3 Replies
Login or Register to Ask a Question