LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   *BSD (http://www.linuxquestions.org/questions/%2Absd-17/)
-   -   need help with find command and rar/unrar command line (http://www.linuxquestions.org/questions/%2Absd-17/need-help-with-find-command-and-rar-unrar-command-line-4175426224/)

krakatoa1 09-08-2012 05:15 AM

need help with find command and rar/unrar command line
 
Greetings,
What began as a simple desire has turned into a couple days of staring at various forums, help websites, man pages, and blogs. I have really, really tried to serve myself and not have to actually start a new thread so if by some chance this has been answered somewhere else my sincere apologies, but I just can't devote any more time to trying to solve this myself and I really need someone to just tell me the answer.
What I'm trying to do with find: I am trying to find *.mp3 files in subdirs named CD* (where the * is a number) and pass the results to the mv command which will then mv said mp3s to the parent directory. I have been able to construct an argument for the first half of this which is: find . -type d -name 'CD?' -print (I'm just printing to test the output). The issues that I need to guard against are that I already have mp3s in an artist directory and I don't want those just moved into the mp3 directory; I only want the mp3s that are in dirs named CD* to be moved. I have tried find . -type d -name 'CD?' -and -type f -name '*.mp3' -print but nothing happens. I even looked at http://www.gnu.org/software/findutil...ttern-Matching and tried to do:
find . -name './CD*mp3' -print
but the command line returns nothing - no errors, no suggestions, just as if I hit the enter key without typing a damn thing.

My find does not seem to have the -iwholename or -wholename switch fwiw. This is stemming from an issue that I was not able to solve with rar/unrar. I do not know how to script and while I believe I found a couple scripts that seemed to address my issue, I couldn't understand how to compile the script (my most command line familiarity unfortunately is with DOS, so, what's the *nix equivalent of a .bat file?), so I will ask this question as an addendum...

I had .rar files in my /mp3/ArtistName-AlbumName/CD*/ dirs, and I wanted to just unrar the contents to the parent dir, thus negating the need for the above operation with mv (although at this point I would like to know how to do it for pure and simple edification...). I was able to come up with this:
cd mp3
find . -name "*.rar" -exec rar x {} "../" \;

which I thought would do what I wanted but no dice. Believe me I tried moving those dots and slashes, i.e. "../" in all sorts of permutations to no avail. The best I could do that would get me halfway there and actually put me in a postition to use this other find expression was:
find . -name "*.rar" -execdir unrar e {} \;
which will unrar into the same dir as the archives.

So if anyone could answer one or both of these questions I would be immensely grateful. Thank you.

pixellany 09-08-2012 05:55 AM

Quote:

find . -name './CD*mp3' -print
but the command line returns nothing - no errors, no suggestions, just as if I hit the enter key without typing a damn thing.
That command is looking for the filename CD*mp3, where "*" means "any characters". It is looking in the current directory, which is redundant, since that was specified earlier.

If you want filename in the current directory, in a sub-directory "CD-something", and with the filename ending in "mp3", then try this:
Code:

find . -name "CD*/*mp3"
<<EDIT--this is wrong---see below>>

krakatoa1 09-08-2012 06:04 AM

Thank you for your quick response... I just pasted what you suggested and it again returns nothing, like I didn't type anything at all:banghead:

pixellany 09-08-2012 06:29 AM

My mistake!! The -name option works only on the filename. Take a look at the -path option.

Here is a test--I created directories beginning in CD, and then put files ending in mp3 into each:
Code:

[mherring@herring_desk ~]$ ls -R CD*
CD1:
abc.mp3  def.mp3

CD2:
xyz.mp3

CD3:
qrs.mp3
[mherring@herring_desk ~]$ find . -path "*CD*mp3"
./CD3/qrs.mp3
./CD2/xyz.mp3
./CD1/def.mp3
./CD1/abc.mp3
[mherring@herring_desk ~]$


krakatoa1 09-08-2012 10:17 AM

Still NOT working
 
Thank you for replying again, however your second suggestion gave me the exact same results; that is to say, nothing. Not an error, not incorrect output, just... nothing. FYI, this is BSD Unix and I am in a bash shell. Yes the OS X implementation. Thank you for trying though :doh:

colucix 09-08-2012 11:01 AM

I'm not used to BSD and I cannot test, but undoubtfully a simple and explicit loop might solve your issue without much effort in seeking a complex one-liner. Let's try something like this:
Code:

#!/bin/bash
while read dir
do
  cd "$dir"
  pwd
  :
done < <(find /mp3/*/ -type d -name 'CD[0-9]*' -print)

This simple bash script has the solely purpose to show that you can easily change directory if you provide the absolute path. The pwd command will show you in which directory you are (just for testing). Once you're inside the directory, do whatever you need to accomplish your task.

After having verified it loops over the correct directories, you can try to put your own commands in place of the null command (see the colon inside the loop).

Sincerely, my only doubt about this simple bash script is if BSD supports process substitution, which is the part that contains the find command and feeds the loop:
Code:

<(find /mp3/*/ -type d -name 'CD[0-9]*' -print)
By the way, you can safely try it (as is) since it doesn't contain any potentially dangerous command (like mv, rm, cp, ln, mkdir, touch and so on) nor the find command contains any other action than -print. Hope this helps.

Reuti 09-11-2012 03:21 PM

The find in OS X is a different one. But you can easily compile the GNU find on your own on OS X and have the same options this way.


All times are GMT -5. The time now is 12:55 AM.