Find and Copy file of specific location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and Copy file of specific location
# 1  
Old 03-26-2015
RedHat Find and Copy file of specific location

Dear All,
I need to transfer all files present in one location to another but those files should be of specific extension like.

Find and copy all files of extension .xls, .pdf, .txt from location usr/tmp to location /per/Treat
# 2  
Old 03-26-2015
Please show what you have tried so far.
# 3  
Old 03-26-2015
RedHat

I tried below 2 things

Code:
ALL_ACC_EXTNS=(dat, txt, pdf)
for i in "${ALL_ACC_EXTNS[@]}"; do
cp ${Source}/*.$i $Destination
done

and second try with
Code:
ACC_EXT=".TXT$|.PDF$|.XLSX$"
find . -name $(echo $AACC_EXT) -type f -exec cp -t '$Destination' {} +

Both didn't work for me.
# 4  
Old 03-26-2015
You could try something like:
Code:
find .....  -name "*.txt" -o -name "*.pdf" -o -name "*.xls*" -exec cp .......

# 5  
Old 03-26-2015
Code:
sudo find . -regextype posix-extended -regex ".*\.(txt|xls|pdf)" -exec cp {} dest_dir \;

or

Code:
cp `find . | egrep "*\.(txt|pdf|xls)"` dest_dir

Find in conjunction with the period(.) will start looking from the current directory and prune the rest of the dirs. Meaning it will look in the cwd you are in. Then when it finds a folder it will look in that folder as well.

Find in conjunction with the forward slash(/) will start looking from the top of your directory tree.

Last edited by codecaine21; 03-26-2015 at 01:22 PM..
# 6  
Old 03-26-2015
RedHat

Actually I wanted to put all Extension in a Variable and then search it.

Code:
like ACC_EXT=".TXT$|.DOC$|.XLS$|.XLSX$|.DOCX$|.PDF$"

Would someone would be able to help. where i am wrong.

---------- Post updated at 11:36 AM ---------- Previous update was at 11:25 AM ----------

Quote:
Originally Posted by codecaine21
Code:
sudo find . -regextype posix-extended -regex ".*\.(txt|xls|pdf)" -exec cp {} dest_dir \;

or

Code:
cp `find . | egrep "*\.(txt|pdf|xls)"` dest_dir

Find in conjunction with the period(.) will start looking from the current directory and prune the rest of the dirs. Meaning it will look in the cwd you are in. Then when it finds a folder it will look in that folder as well.

Find in conjunction with the forward slash(/) will start looking from the top of your directory tree.

Actually I want to transfer from only one single dir. neither the top directory nor any prune required. Thanks for your reply.

looking forward for your reply.
# 7  
Old 03-26-2015
Quote:
Originally Posted by yadavricky
Actually I wanted to put all Extension in a Variable and then search it.

Code:
like ACC_EXT=".TXT$|.DOC$|.XLS$|.XLSX$|.DOCX$|.PDF$"

Would someone would be able to help. where i am wrong.

---------- Post updated at 11:36 AM ---------- Previous update was at 11:25 AM ----------




Actually I want to transfer from only one single dir. neither the top directory nor any prune required. Thanks for your reply.

looking forward for your reply.
Then do
Code:
sudo find dir_to_copy_from -regextype posix-extended -regex ".*\.(txt|xls|pdf)" -exec cp {} dir_to_copy_to \;

Example:
Code:
sudo find /home/codecaine21/Documents/ -regextype posix-extended -regex ".*\.(txt|xls|pdf)" -exec cp {} /home/codecaine21/Documents/Files/ \;

or

Code:
cp `find dir_to_copy_from  | egrep "*\.(txt|pdf|xls)"` dir_to_copy_to

Example:
Code:
cp `find /home/codecaine21/Documents/  | egrep "*\.(txt|pdf|xls)"` /home/codecaine21/Documents/Files/

You should read up on Bash programming. Your understanding of the fundamentals is a bit skewed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

2. 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

3. Shell Programming and Scripting

How to find a word and move it a specific location in xml file using perl?

Hi friends, I have one XML file having below structure :- INput XML file :- <?xml version="1.0" encoding="UTF-8"?> <START> <A=value1> <attr name1="a1"> </A> <B=value2> <attr name2="b1"> <attr name3="c1"> </B> </START> output xml file should be === (3 Replies)
Discussion started by: harpal singh
3 Replies

4. Shell Programming and Scripting

How to find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

5. Shell Programming and Scripting

How to copy a file from one location to another location?

I have file file1.txt in location 'loc1'. Now i want a copy of this file in location 'loc2' with a new file called test.txt. Please help me how to do this in shell script. (1 Reply)
Discussion started by: vel4ever
1 Replies

6. Shell Programming and Scripting

Find and replace a string a specific value in specific location in AIX

Hi, I have following samp.txt file in unix. samp.txt 01Roy2D3M000000 02Rad2D3M222222 . . . . 10Mik0A2M343443 Desired Output 01Roy2A3M000000 02Rad2A3M222222 . . (5 Replies)
Discussion started by: techmoris
5 Replies

7. Shell Programming and Scripting

Shell Script for Copy files from one location to another location

Create a script that copies files from one specified directory to another specified directory, in the order they were created in the original directory between specified times. Copy the files at a specified interval. (2 Replies)
Discussion started by: allways4u21
2 Replies

8. Shell Programming and Scripting

find pid of process run in specific location

Hello, I have a process a.out that runs from /a and /b How can I get the pid of the one running from /a ps -C /a/a.out does not work Thanks! (4 Replies)
Discussion started by: JCR
4 Replies

9. Shell Programming and Scripting

tail copy of a file to remote location

Hello, I have a solaris box and a windows server. The windows server runs cygwin for ssh service. I have an audit log in solaris box. When ever new records are added to the log file, this delta has to be trasported to a remote file in windows. I can do a ssh once in a while, but want the... (7 Replies)
Discussion started by: unori
7 Replies

10. Shell Programming and Scripting

Bash copy file contents into an existing file at a specific location

Hi all I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far: File A line 1 line 2 line 3 line 4 ... (6 Replies)
Discussion started by: gshepherd7
6 Replies
Login or Register to Ask a Question