LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-22-2015, 11:18 PM   #1
cilbuper
Member
 
Registered: Mar 2008
Posts: 141

Rep: Reputation: 0
Moving files with wildcard - mv -r doesn't work and cp keeps original copy..


Ok, so what I want ot do is take all files, say *.mp3 from all sub folders within a folder.

Let's say the folder is /home/user/music and there are 100's of sub-folders in music. I want to move all the files to /home/user/mp3

I know that
Code:
mv -rfv /home/user/music/*.mp3 /home/user/mp3
doesn't work as no usage or "r" works with the mv command.

Is there a way to do this? I want to move all mp3, wma, ogg and others to a destination folder.

Also, I have tried using the copy command (with R and r)

Code:
$ cp -Rfv /home/user/music/*.mp3 home/user/mp3/
I know I have successfully copied specific files, recursively, from within a folder and sub-folders to another folder, but can't seem to figure it out again.

Can anyone here help with this?
 
Old 11-23-2015, 12:30 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
You can list all MP3 files and copy them to /home/user/mp3. There are various ways to do this:
Code:
find /home/user/music -name '*.mp3' -exec mv {} /home/user/mp3 \;
will work but may be slow because for each move, you start a new mv process.

Code:
mv $(find /home/user/music -name '*.mp3') /home/user/mp3
may work, but if there are too many MP3s, the command line may be too long and the command fail. So:

Code:
find /home/user/music -name '*.mp3' | xargs -I file mv file /home/user/mp3
Fourth option: Find also has a "+" operator after the -exec option; since I don't know it by heart, please look it up in the man page.

As an exercise, read the man pages for find and xargs to figure out what all that means.
 
Old 11-23-2015, 04:12 AM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by berndbausch View Post
You can list all MP3 files and copy them to /home/user/mp3. There are various ways to do this:
Code:
find /home/user/music -name '*.mp3' -exec mv {} /home/user/mp3 \;
will work but may be slow because for each move, you start a new mv process.
Using
Code:
find /home/user/music -name '*.mp3' -exec mv {} /home/user/mp3 +
instead will minimize that problem.
If you happen to use Zsh instead of Bash all you need to do is this:
Code:
mv -v /home/user/music/**/*.mp3 /home/user/mp3
if you have the extended_glob option set in your .zshrc.
 
Old 11-27-2015, 07:49 AM   #4
serverpoint.com
Member
 
Registered: Oct 2015
Posts: 52

Rep: Reputation: 6
Hi,

Try this

find source -name "*.xxx" -exec mv -i {} -t ~/dst \;

Where *.xxx is your file extension and ~/dst is the destination path.
 
Old 12-09-2015, 06:09 AM   #5
cilbuper
Member
 
Registered: Mar 2008
Posts: 141

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TobiSGD View Post
Using
Code:
find /home/user/music -name '*.mp3' -exec mv {} /home/user/mp3 +
instead will minimize that problem.
If you happen to use Zsh instead of Bash all you need to do is this:
Code:
mv -v /home/user/music/**/*.mp3 /home/user/mp3
if you have the extended_glob option set in your .zshrc.




Thanks for the suggestion/tip. I did something like this on my OS and i'm not sure I ever installed Zsh (is that standard on Kubuntu 14.10?). This will work well once i get the other aspect of the problem working as well.

If I have a folder, with say 10 sub folders. Each subfoler has an archive set or rars (or possibly zips, but most likely rars). Instead of having to navigate to each folder, right clicc and select "extract here", is there a way to do every rar within a subfolder? I had this done before as well but I just can't seem to get the syntax correct or possibly commands. Once the file is extracted I can use the command that you posted above to scavenge the folders for the file types that I want to move to a permanent location.
 
Old 12-09-2015, 06:37 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Something like this ?
Code:
cd topdir
for subdir in $(find . -type d)
do
    cd $subdir
    for file in *.rar
    do
        unrar $file
    done
done
NB UNTESTED...
 
Old 12-10-2015, 04:18 AM   #7
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by chrism01 View Post
Something like this ?
Code:
cd topdir
for subdir in $(find . -type d)
do
    cd $subdir
    for file in *.rar
    do
        unrar e $file
    done
done
NB UNTESTED...
Minor fix, unrar needs you to specify a command, either use the e or the x command, depending if preserving paths when unpacking is necessary.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can I use sudo with a wildcard in file names? (* doesn't work) abefroman Linux - Newbie 7 04-19-2012 01:40 PM
copy wildcard files from directories to matching ones in another dir akufoo Linux - Newbie 1 02-11-2011 11:56 PM
Copy/Paste via ssh doesn't work ccargo Linux - Newbie 6 03-08-2009 05:30 PM
Why doesn't a wildcard chmod change "dot" files/directories? jht2k Linux - General 1 08-09-2004 02:31 PM
Moving to new network doesn't work iainr Linux - Networking 5 08-01-2004 05:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:57 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
Open Source Consulting | Domain Registration