How do I delete all Folders with a specific name (e.g. 128Kbps_Songs) without deleting its files

For example if I have directory "MP3_SONGS" with subdirectories "A", "B", "C" and each subdirectory has MANY directory Contains Film Name "FILM_NAME1", "FILM_NAME2", "FILM_NAME3" in it, In That Each Film NAme I have specific Folder NAme (128Kbps_Songs) in this "128Kbps_Songs" Folder I have That Film mp3 Songs Files

how can I remove the Folder "128Kbps_Songs", in Each Film_Name directory and Have to get these mp3 songs in that FILM_NAME.. ( Have to Delete 128Kbps_Songs Folder in all FILM_NAME without deleting the mp3 Songs in it, have to get the mp3 in FILM_NAME Directory..

MP3_SONGS (Parent Directory) ------ A , B, C

A (Sub Directory1) ------ FILM_NAME1 , FILM_NAME2
B (Sub Directory2) ------ FILM_NAME3 , FILM_NAME4
c (Sub Directory3) ------ FILM_NAME5 , FILM_NAME6

FILM_NAME1 (Directory) ---- 128Kbps_Songs (Directory) ------ FILM_NAME1 (MP3 Files)
FILM_NAME2 (Directory) ---- 128Kbps_Songs (Directory) ------ FILM_NAME2 (MP3 Files)

FILM_NAME3 (Directory) ---- 128Kbps_Songs (Directory) ------ FILM_NAME3 (MP3 Files)
FILM_NAME4 (Directory) ---- 128Kbps_Songs (Directory) ------ FILM_NAME4 (MP3 Files)

FILM_NAME5 (Directory) ---- 128Kbps_Songs (Directory) ------ FILM_NAME5 (MP3 Files)
FILM_NAME6 (Directory) ---- 128Kbps_Songs (Directory) ------ FILM_NAME6 (MP3 Files)

Need Files in:
FILM_NAME1 (Directory)  ------ FILM_NAME1 (MP3 Files)
FILM_NAME2 (Directory)  ------ FILM_NAME2 (MP3 Files)

FILM_NAME3 (Directory)  ------ FILM_NAME3 (MP3 Files)
FILM_NAME4 (Directory)  ------ FILM_NAME4 (MP3 Files)

FILM_NAME5 (Directory)  ------ FILM_NAME5 (MP3 Files)
FILM_NAME6 (Directory)  ------ FILM_NAME6 (MP3 Files)
  • If I understand correctly you want to move all "MP3 Files" from last sub-directory and then delete that sub-dir, right? if yes, you can use find command to find all files with -maxdepth 3 and move them into dest directory and then delete the empty dir – αғsнιη Jan 17 '15 at 8:06
  • Yes , Can You Provide the Code For That ??? – Chatcafe Jan 17 '15 at 8:20
  • Sorry For That :-( in Simple - want to move all "MP3 Files" from last sub-directory – Chatcafe Jan 17 '15 at 8:31
  • want to move all "MP3 Files" from last sub-directory – Chatcafe Jan 17 '15 at 8:35
  • no, have to delete 128Kbps_Songs directory, but have to move mp3 file from in 128Kbps_Songs to last sub-directory, if 128Kbps_Songs directory exits there only want this changes .. – Chatcafe Jan 17 '15 at 9:05

Create a simple script with following contents


while IFS= read -r -d '' n; 
   dir=$(dirname "$n") ; 
   mv "${n}"/* "${dir}"
   rmdir "${n}"
done < <(find -type d -name "128Kbps_Songs" -print0)

Save it and make it executable using chmod +x your_script

Now run it using ./your_script

Here IFS= is done to preserve the space in the filename or path.

  • -r - disables interpretion of backslash escapes and line-continuation in the read data.

  • -d - recognize delimiter as data-end. Here it is emptying out the IFS

  • dirname is use to remove the last folder name i.e 128Kbps_Songs from the find command output. (from ./temp/MP3_SONGS/128Kbps_Songs to ./temp/MP3_SONGS)

  • Thank You so Much ..... You saved my Time lot :-) Its Workingggggggggggggggggg – Chatcafe Jan 18 '15 at 6:37

First move all MP3 files up one level by the command as following:

find MP3_SONGS -type f -name '*.mp3' -execdir echo mv -v '{}' .. \;
  • The -type f option looks for all files
  • The -name '*.mp3' filter files which end with .mp3
  • The command mv -v '{}' .. will be run for each subdirectory containing the matched file, which is not normally the directory in which you started find (action of the -execdir).
  • The string '{}' specifies the current file name being processed everywhere it occurs in the arguments to the command.

Note: After running above command, remove the echo command to perform running actual mv command.

Then try to delete all empty directories (128Kbps_Songs directories):

find MP3_SONGS -empty -type d -name '128Kbps_Songs' -delete
  • The -empty : File is empty and is either a regular file or a directory.
  • The -type d option as the same as -type f but this looks for all directories instead.
  • The -delete, deletes the found file or directory.

See man find for more info.

  • in this i have one problem .... :-( Some of FILM_NAME having Mp3 Directly in it, FILM_NAME1 -----> fileNum6_1.mp3 Suppose i move all MP3 files up one level i will get all mp3 out of FILM_NAME1 directory .. in this case what should i do !!!!!! – Chatcafe Jan 18 '15 at 6:23
  • @Chatcafe please run tree MP3_SONGS and post the result on paste.ubuntu.com and then give me the link – αғsнιη Jan 18 '15 at 6:26
  • Thanks Brother for your Effort :-) its Really Helped me lot :-) – Chatcafe Jan 18 '15 at 6:38
  • @Chatcafe the g_p's answer do the same which my answer do! then what is your problem with this????? – αғsнιη Jan 18 '15 at 6:42
  • paste.ubuntu.com/9773304 i made the changes like this :-) – Chatcafe Jan 18 '15 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.