How to find files and then copy them to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find files and then copy them to another
# 1  
Old 11-22-2009
How to find files and then copy them to another

I must write any shell script.
I want find files which have .txt extension and then copy them to other, whithout this extension, for example:
I found linux.out.txt file, and now it must be copy to new, linux.out.
So: linux.out.txt -> linux.out
ubuntu.config.txt -> ubuntu.config
...
I tried many solutions..

Code:
find directory -type f -name *.txt | sed 's/.txt//'>file.txt

and here i have good new file names in file.txt file, but how should i run cp command ? Smilie
Any ideas ? HELP
# 2  
Old 11-22-2009
Hi,

Try This
Code:
 
#!/bin/sh
find directory -name "*.txt" | while read FILE
do
        newfilename=`echo $FILE | sed 's/.txt//g' | awk -F'/' '{print $NF}'`
        mv $FILE directory/$newfilename
done


Last edited by pravin27; 11-22-2009 at 02:45 AM..
# 3  
Old 11-22-2009
ls -1 *out.txt | while read rec
do
mv $rec /your/destination/dir/${rec%.*}
done

Last edited by jambesh; 11-22-2009 at 02:35 AM.. Reason: removed copy options
# 4  
Old 11-22-2009
I've seen this magic recently
Code:
SRC=/source/directory
DST=/destination/directory
find $SRC -type f -iname "*.txt" -exec sh -c 'dest=$2; cp "$1" "$dest/$(basename ${1%.*})"' {} {} $DST \;


Last edited by daPeach; 11-22-2009 at 03:02 AM..
# 5  
Old 11-22-2009
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use find with cp and sed in ksh to copy files to a slightly different location

Hello there wonderful people, I am running on Solaris 10 and with the following ksh version: strings /bin/ksh | grep Version | tail -2 @(#)Version M-11/16/88i Suppose I want to copy files that end in _v2 from underneath /dir1/dir2/save directory to /dir1/dir2. Basically, what I’m... (12 Replies)
Discussion started by: ejianu
12 Replies

2. Shell Programming and Scripting

Find and copy files with field lower than a value, awk?

Hi all! I have 10.000 files having generally this format: text text text text num text num text num text text text GAP number text text text num text num text num RMS num text num text num text num ... what I want is to copy the files if the GAP number is lower than a value e.g. <100... (5 Replies)
Discussion started by: phaethon
5 Replies

3. Shell Programming and Scripting

Writte a script to copy the files older than 7 days using find and cp

Hi I'm trying to writte a script (crontab) to copy files from one location to another... this is what i have: find . -name "VPN_CALLRECORD_20130422*" | xargs cp "{}" /home/sysadm/patrick_temp/ but that is not working this is the ouput: cp: Target... (5 Replies)
Discussion started by: patricio181
5 Replies

4. Shell Programming and Scripting

Find and copy these files to particular directory

RedHat Enterprise Linux 5.4 I have some files with the extension .cdp in several directories in various mountpoints(filesystems) . I would like to find and copy all these files into a single directory /u03/diagnore/data. How can I do this ? (3 Replies)
Discussion started by: kraljic
3 Replies

5. Linux

Find MSWord doc Files by Year and then Copy

I need to be able to find all *.doc files by year last modified and then perform an action such as copy to folder: /documents/2011 the 'find' command seems to show the path but not the full file details, which includes the date modified as the ls command does. I got this far with ls, but have... (2 Replies)
Discussion started by: jamarsh
2 Replies

6. Shell Programming and Scripting

Find files of type within folder copy multiple results to different folders

Ok question number two: I'd like to search a directory for multiple file types (rar, txt, deb) and depending on what's found, copy those files to folders named Rar, TextFiles, and Debs. I'm looking for speed here so the faster the script the better. I want it to be a function that I pass 1 argument... (4 Replies)
Discussion started by: DC Slick
4 Replies

7. UNIX for Dummies Questions & Answers

How to find and copy files from one directory to another

Ok i have three directories Destination - /u/dir1 (has subdirectories dir2 which also has subdirectory dir3) Source1 - /u/test/files/dir1/dir2/dir3 Source2 - /u/out/images/dir1/dir2/dir3 What i would like to do is copy everything from Source1 and Source2 into the Destination directory.... (3 Replies)
Discussion started by: ziggy25
3 Replies

8. UNIX for Dummies Questions & Answers

Find & Copy Selected files to another Directory

I am wanting to find files within a directory that are over a certain number of days old and copy them to another directory. And unfortunately not having much luck.......is someone able to help. Would also like to add that there are literally thousands of files that I am wanting to copy in one... (3 Replies)
Discussion started by: hellfyre
3 Replies

9. Shell Programming and Scripting

find & copy files with absolute path

hi all, can i get script find file & copy that file with path for an example sourse : /home/abc/ destination : /home/backup/ files which need to find : tmp* copy these files with its absolute path inside like :- /home/abc/x/y/z/tmp.txt to /home/backup/date/x/y/z/tmp.txt thanks in... (15 Replies)
Discussion started by: jagnikam
15 Replies

10. Shell Programming and Scripting

find files and copy into a directory

hi all, can u please help me in finding all ksh file in directory and including all subdirectories and then copy those files into another directory. thanks in advance -bali (4 Replies)
Discussion started by: balireddy_77
4 Replies
Login or Register to Ask a Question