LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-18-2011, 12:49 PM   #1
moyorakkhi
Member
 
Registered: Jan 2011
Location: Dhaka
Posts: 80

Rep: Reputation: 1
Find files with specific extension and move to a directory


Hello,

I need little help. I want to find all files with extension "*.tar" "*.gz" and "*.zip" and move all those files into "/opt/old" directory.

I've tried this command:

Quote:
find . -type f -name "*.tar" "*.gz" "*.zip" -print0 | xargs -0 -r mv /opt/test
It's not working, something wrong after "mv" i guess. Please rectify me. Thanks.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-18-2011, 12:55 PM   #2
Nickjpost
LQ Newbie
 
Registered: Dec 2010
Location: Boulder, CO
Distribution: Ubuntu
Posts: 27

Rep: Reputation: 0
Have you looked into the grep utility? In your terminal, type 'man grep' w/o quotations, I believe there is a section of the manual that covers this scenario.
 
Old 04-18-2011, 12:57 PM   #3
TwistOneUp
LQ Newbie
 
Registered: Sep 2010
Location: San Francisco, USHey
Distribution: Centos 5.5
Posts: 1

Rep: Reputation: 1
try this:

find . -name '*.ext' -exec mv '{}' newdir/ \;

where:

"ext" is the extension you want (tar, pdf, etc.) to move; and
"newdir" is the location where you want them moved.
 
1 members found this post helpful.
Old 04-18-2011, 12:57 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Actually you should have a look at man find. Try taking out the piped action and see what messages you get:
Code:
find . -type f -name "*.tar" "*.gz" "*.zip"
Once you solved this issue slowly put your command together.
 
Old 04-18-2011, 12:59 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Or find . -type f -name "*.tar" -o -name "*.gz" -o -name "*.zip" -print0 ...
 
Old 04-18-2011, 01:05 PM   #6
lrtward
Member
 
Registered: Feb 2011
Distribution: CentOS, Ubuntu
Posts: 97

Rep: Reputation: 9
Code:
# find . \( -name "*.tar" -o -name "*.gz" -o name "*.zip" \) -exec mv {} /opt/test \;
 
2 members found this post helpful.
Old 04-18-2011, 01:14 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by lrtward View Post
Code:
# find . \( -name "*.tar" -o -name "*.gz" -o name "*.zip" \) -exec mv {} /opt/test \;
I was just going to thank you for the correction but tested my form and it worked.

@moyorakkhi: it is prudent to use -exec echo mv {} /opt/test \; when testing.
 
2 members found this post helpful.
Old 04-18-2011, 01:19 PM   #8
Nickjpost
LQ Newbie
 
Registered: Dec 2010
Location: Boulder, CO
Distribution: Ubuntu
Posts: 27

Rep: Reputation: 0
Wink

Quote:
Originally Posted by grail View Post
Actually you should have a look at man find. Try taking out the piped action and see what messages you get:
Code:
find . -type f -name "*.tar" "*.gz" "*.zip"
Once you solved this issue slowly put your command together.
My bad....sorry if you went digging under my tree
 
Old 04-18-2011, 01:21 PM   #9
moyorakkhi
Member
 
Registered: Jan 2011
Location: Dhaka
Posts: 80

Original Poster
Rep: Reputation: 1
Thanks everyone for your help.
Quote:
find / -name "*.gz" -exec mv '{}' /opt/test/ \;
worked!

Thanks again!
 
Old 04-18-2011, 10:48 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Just as long as you are aware that the issue with your original find was how you were grouping them after -name as the above responses
show you the alternate correct ways. I would also show extreme caution with the example you have given:
Quote:
find / -name "*.gz" -exec mv '{}' /opt/test/ \;
I can tell you for a fact that there are gzipped files on your system that will definitely affect other programs once moved!!
Quote:
keymaps
consolefonts
charmaps
These are a small few on a minimal system.
 
Old 04-19-2011, 09:50 AM   #11
lrtward
Member
 
Registered: Feb 2011
Distribution: CentOS, Ubuntu
Posts: 97

Rep: Reputation: 9
Quote:
Originally Posted by catkin View Post
I was just going to thank you for the correction but tested my form and it worked.

@moyorakkhi: it is prudent to use -exec echo mv {} /opt/test \; when testing.
I hadn't seen your post when I posted. I was mucking about trying to get my own syntax to work. When I saw your post I tried it out and it works as well. I think I like yours better. The -print0 was nice.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete/move/copy files of specific date imsajjadali Red Hat 26 11-08-2013 12:34 AM
[SOLVED] Check folder for specific files than move script BlackCrowe Programming 3 11-23-2010 06:59 AM
Write a script to move specific files in various folders to one folder linuxisthedevil Linux - General 13 11-18-2010 07:29 AM
[SOLVED] shell script to search for files of specific extension and delete them all rhklinux Linux - Newbie 12 07-12-2010 01:14 AM
Searching a specific directory for a specific extension? RoaCh Of DisCor Linux - Newbie 3 08-13-2005 04:28 PM


All times are GMT -5. The time now is 12:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration