Script to move files with similar names to folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to move files with similar names to folder
# 1  
Old 03-31-2011
Script to move files with similar names to folder

I have in directory /media/AUDIO/WAVE many .mp3 files with names like:
my filename_01of02.mp3
my filename_02of02.mp3
Your File_01of06.mp3
Your File_02of06.mp3
etc....

In the same directory, /media/AUDIO/WAVE, I have many folders with names like
9780743579490
9780743579491
etc..

Inside the folders I have files:
(Inside folder 9780743579490 is)
9780743579490_01of6_Your File.wav
9780743579490_02of6_Your File.wav

What I need, is a script to find what folder the .wav files that have the same filename at the .mp3 files in /media/AUDIO/WAVE, and move the mp3 files into the folders with the wav files that share the same file name.

To be clear the mp3 part "01of06" is in a different place than the .wav file and the wav file has a 13 digit number in the beginning that the mp3 doesnt, so you will have to match what happens before the first underscore on the mp3 file with what happens between the last underscore and the file extension in the .wav file!

Last edited by glev2005; 04-01-2011 at 10:47 AM..
# 2  
Old 03-31-2011
Your example dosn't share the same file name: even excluding the directory_name + _ prefix we still have Your File_01of06 and 01of6_Your File.
Do all the files names have this NNofMM part moved in this way?

Last edited by Chubler_XL; 03-31-2011 at 10:54 PM..
# 3  
Old 03-31-2011
Hi glev2005,

After several tests, I get the script showed below, it works, but only with filenames without spaces.


Instead of filenames like:
Code:
my filename_01of02.mp3
Your File_01of06.mp3
9780743579490_01of6_Your File.wav

You would have to use the format as you said before, but adding "_" instead of spaces in filename:
Code:
my_filename_01of02.mp3
 Your_File_01of06.mp3
 9780743579490_Your_File_01of06.wav

Script (Maybe somebody could adapt it to handle filenames with spaces too)
Code:
mp3=$(find . -name "*.mp3" | awk -F"/" '/[0-9]/{print}')
wav=$(find . -name "*.wav" | awk -F"/" '/[0-9]/{sub(/\.wav/,"");print $NF}')
for i in $mp3
 do
  for j in $wav
   do
    temp_m=$(basename $i)
    temp_w=$(basename $j)
   if [ ${temp_m%.*} == ${temp_w#*_} ]; then
     cp ${temp_m} ~/media/AUDIO/WAVE/${temp_w%%_*} /${temp_m} # Copy file found to test, when you test it, change to "mv"
   else
     echo "There is no $temp_m in any subfolder or has been moved before"
   fi
  done
done


I hope it helps.

Regards

Last edited by cgkmal; 03-31-2011 at 10:17 PM..
# 4  
Old 03-31-2011
Here is one that works with spaces in file name, but we still have issue with NNofMM in wrong place (see my previous question).

Code:
WAVDIR="~/media/AUDIO/WAVE/"
find . -name "*.mp3" | while read file
do
   temp_m=$(basename "$file" .mp3)
   look=$(echo "$WAVDIR/*/*${temp_m}.wav" | sed 's/ /\\ /g')
   look=$(eval echo $look)
   if [ -f "$look" ]
   then
       todir="${look%/*}"
       echo mv "${temp_m}.mp3" ${todir}/ # echo action to perform - after testing remove echo
   else
       echo "No unique match for $file ($look)"
   fi
done

If the position and format of NNofMM is changed on all files as shown in the example, replace line 5 above with:
Code:
   look=$(echo "$WAVDIR/*/*_${temp_m}.wav" |
       sed -e 's/ /\\ /g' -e 's/_\(.*\)_\([0-9][0-9]of[0-9]*\)/_\2_\1/' -e 's/of0/of/' )


Last edited by Chubler_XL; 03-31-2011 at 10:59 PM..
# 5  
Old 04-01-2011
Quote:
Originally Posted by Chubler_XL
Here is one that works with spaces in file name, but we still have issue with NNofMM in wrong place (see my previous question).

Hi Chubler_XL,


Thanks for show other way to do it and solve spaces issue!

Yes, It works with spaces in filename, but when I replace 5 line with code you suggest I didn't receive any file moved.

I'm not sure what if the scenario to change in file names, I did it like glev2005 said in firts post:
Under /media/AUDIO/WAVE/ a file named "my filename_02of02.mp3" and under /media/AUDIO/WAVE/9780743579491/
a file named "9780743579491_02of02_my filename.wav".

Regards
# 6  
Old 04-01-2011
Have a look at his post again the .wav files don't have leading zero in the "of" part eg: 9780743579491_02of2_my filename.wav.

It's a nasty situation and I feel some sort of fuzzy name matching is needed here.
# 7  
Old 04-01-2011
Yes Chubler, I saw with lens and I get it now what you mean Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

Script to move files and Creation of folder structure

We are receiving few zipped files in one location say : apple/oranges/incoming All .zip files are placed here in incoming folder. So few of the files are password encrypted. There are only 10 zipped files, so we are planning to create a script which will pick that zip file from incoming... (1 Reply)
Discussion started by: Sidhant
1 Replies

3. Shell Programming and Scripting

How to move the files older than x days with similar directory structure?

Hello, I need to move all the files inside /XYZ (has multi-depth sub directories) that are older than 14 days to/ABC directory but with retaining the SAME directory structure. for example: /XYZ/1/2/3/A/b.txt should be moved as /ABC/1/2/3/A/b.txt I know about find /XYZ -type f -mtime +14... (3 Replies)
Discussion started by: prvnrk
3 Replies

4. Shell Programming and Scripting

Move files from Space Folder to other folder

I want to move a folder with spaces from one folder to another. I have two folders like this, 1).RT_032-222 -4444-01/ 2). RT_032-555 -7777-01/ I want to move files from 2 to 1 through shell script.Here I want to assign this like a user defined variable like as Source branch... (2 Replies)
Discussion started by: kannansoft1985
2 Replies

5. Shell Programming and Scripting

Copying files from various folders to similar folder structure in another location

Hi, I need to write a script the has to copy the files from folders and subfolders to the same folder structure located in another location. Ex: mainfolder1 file1,file2,file3 subfolder1(file1,etc) subfolder2(file1,etc) to another folder location of same folder structure. rsync is not... (7 Replies)
Discussion started by: Raji Perumal
7 Replies

6. Shell Programming and Scripting

Finding files in directory with similar names

So, I have a directory tree that has many files named thusly: X_REVY.PDF I need to find any files that have the same X portion (which can be nearly anything) as any another file (in any directory) but have different Y portions (which can be any number from 1-99). I then need it to return... (3 Replies)
Discussion started by: Kamezero
3 Replies

7. Shell Programming and Scripting

Script to move one folder to multiple folder...

Hi All, I have to requirement to write a shell script to move file from one folder (A) to another five folder (B,C,D,E,F) and destination folder should be blank. In not blank just skip. This script will run as a scheduler every 2 minutes. It will check number of files in folder A and move 1 to... (9 Replies)
Discussion started by: peekuabc
9 Replies

8. Shell Programming and Scripting

Merging two columns from two files with similar names into a loop

I have two files like this: fileA.net A B C fileA.dat 1 2 3 and I want the output output_expected A 1 B 2 C 3 I know that the easier way is to do a paste fileA.net fileA.dat, but the problem is that I have 10,000 couple of files (fileB.net with fileB.dat; fileC.net with... (3 Replies)
Discussion started by: valente
3 Replies

9. Linux

Create folder by script & move files in it

Hi Team, I have over 1 lakh pdf files. I want to create folders like Disk-1, Disk-2 ..... & want to move 3000 pdfs per folder. Can i do it by script? Please help me. Thanks & Regards Parag Nehete (4 Replies)
Discussion started by: paragnehete
4 Replies

10. Shell Programming and Scripting

parsing file names and then grouping similar files

Hello Friends, I have .tar files which exists under different directories after the below code is run: find . -name "*" -type f -print | grep .tar > tmp.txt cat tmp.txt ./dir1/subdir1/subdir2/database-db1_28112009.tar ./dir2/subdir3/database-db2_28112009.tar... (2 Replies)
Discussion started by: EAGL€
2 Replies
Login or Register to Ask a Question