Gizmo's Freeware Forum

Gizmo's Freeware Forum (https://www.techsupportalert.com/freeware-forum/)
-   Linux (https://www.techsupportalert.com/freeware-forum/linux/)
-   -   Linux Command to find some folders and delete them.... (https://www.techsupportalert.com/freeware-forum/linux/956-linux-command-to-find-some-folders-and-delete-them.html)

Drigo 01. Jun 2009 05:20 AM

Linux Command to find some folders and delete them....
 
Hello,
So I copied my music to a hard drive, I formatted my PC and copied it back. Though it was unsuccesul, there were some zero byte files that were created. I manage to delete them but not the artwork.

So....basically I have many folders with just artwork that I want to delete... my question?

Can you please provide me a command to find folders under a an specific path where there is no *.mp3 files and delete them at the same time? Also, how can I create a file with the names of thes folders?

I hope you can help me!

debtboy 01. Jun 2009 06:17 PM

Quote:

Originally Posted by Drigo (Post 5929)
Hello,
So I copied my music to a hard drive, I formatted my PC and copied it back. Though it was unsuccesul, there were some zero byte files that were created. I manage to delete them but not the artwork.

So....basically I have many folders with just artwork that I want to delete... my question?

Can you please provide me a command to find folders under a an specific path where there is no *.mp3 files and delete them at the same time? Also, how can I create a file with the names of thes folders?

I hope you can help me!

Sure,
but it's a script and not a single line.

Code:

#!/bin/bash
Your_Search_Path="/home/dringo/music"
  find $Your_Search_Path -maxdepth 1 -type d | while read folder
    do
      mp3files=`$folder" | grep -e '.mp3' | wc -l`
      if [ "$folder" != "$Your_Search_Path" ]
      then
        if [ "$mp3files" == "0" ]
        then
          rm -r $folder
          echo "$folder" >> $Your_Search_Path/delete_log
        fi
      fi
    done




Save the script and make it executable (chmod).
Put in your own search path
I'm assuming all your folders are sub-directories of your search path
and are only 1 layer deep.
I also assume you have permission over these files and directories.

Hope this helps. ;)

In english:
shebang #!/bin/bash - how to execute
Set Your_Search_Path variable
Find directories current and 1 sub-directory deep
pipe that into read loop

Take directory name and pipe into grep looking for .mp3
pipe that into print newline count option and put that
number in the mp3files variable

Ignore your starting search folder, directory sub-directories only

If there are no mp3 files found delete the directory and contents

Write the deleted directory path to a delete_log logfile.

man pages will answer in detail the exact switch options used.

For those following the command line thread,
This is a shell script, nothing but a group of Bash commands!!:D


All times are GMT +1. The time now is 06:29 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.