Delete files in several (sub)directories

Forum rules
Before you post please read how to get help
Post Reply
Mr.October
Level 4
Level 4
Posts: 211
Joined: Sat Oct 24, 2015 1:27 am

Delete files in several (sub)directories

Post by Mr.October » Tue Mar 22, 2016 8:26 am

Hi all,

I have a large music collection and today I am playing around with Amarok, the KDE music player.
In Amarok I saw I have 323 ".m3u" files in the collection, scattered all over the folders and sub-folders.
When I use:

Code: Select all

locate *.m3u > m3u.list
I get all files mentioned in one list with the complete file path where to find the files.
How I can I, using this list or maybe even without this list, remove all those files?
Mind you they are several levels deep in the folders hierarchy, plus they contain spaces in the file names.

Example of some files:

Code: Select all

/Music/Film Muziek/Back to the Future/Back To The Future Part I/Back To The Future Part I.m3u
/Music/Film Muziek/Back to the Future/Back To The Future Part II/Back To The Future Part II.m3u
/Music/Film Muziek/Back to the Future/Back To The Future Part III/Back to the Future Part III.m3u
/Music/Film Muziek/Back to the Future/Back to the Future Trilogy/Back to the Future Trilogy.m3u
/Music/Klassieke Muziek/Classic FM Relax/CD1/Classic FM Relax CD1.m3u
/Music/Klassieke Muziek/Classic FM Relax/CD2/Classic FM Relax CD2.m3u
/Music/Klassieke Muziek/Classic FM Relax/CD3/Classic FM Relax CD3.m3u
I also like to do that with other file types, like .jpg to reduce the size of the folders or else I need larger disks soon. When I know how it works with the *.m3u files I can do the rest alone. I hope.
Thank you for your help.

User avatar
Pjotr
Level 21
Level 21
Posts: 13531
Joined: Mon Mar 07, 2011 10:18 am
Location: The Netherlands (Holland)
Contact:

Re: Delete files in several (sub)directories

Post by Pjotr » Tue Mar 22, 2016 9:17 am

There are many ways, but I suggest this one:

1. Install catfish:

Code: Select all

sudo apt-get install catfish
2. Launch Catfish and let it search for *.m3u

3. Ctrl-a to select all search results

4. Right-click mouse - Delete
Tip: 10 things to do after installing Linux Mint 19.2 Tina
Keep your Linux Mint healthy: Avoid these 10 fatal mistakes
Twitter: twitter.com/easylinuxtips
All in all, horse sense simply makes sense.

Mr.October
Level 4
Level 4
Posts: 211
Joined: Sat Oct 24, 2015 1:27 am

Re: Delete files in several (sub)directories

Post by Mr.October » Tue Mar 22, 2016 9:51 am

Thank you Pjotr. This was not what I expected to get in a scripting forum but hey, it's 2016, time to use GUI.
I installed catfish as you wrote, searched and found my files, but deleting them is more difficult than you wrote: 4. Right-click mouse - Delete
When I use ctrl-a to select all and then use right click to chose delete, I end up with only one selected file which will be deleted. It's the file I used to right-click on, all others are de-selected.
Is there another way to delete? I saw the delete key on the keyboard doesn't do it.

I have to say I am using Mint 17.3 KDE, don't hope this has anything to do with it.

User avatar
cholq
Level 3
Level 3
Posts: 113
Joined: Tue Jul 08, 2014 9:27 pm
Location: New Jersey, USA

Re: Delete files in several (sub)directories

Post by cholq » Tue Mar 22, 2016 10:35 am

If you have all the files listed in a text file, you should be able to do it from the command line. Here is a quick test I did to confirm:

Here are the files to be deleted:

Code: Select all


[corey@localhost ~]$ ls -l ~/delete-test/sub-dir1/*
-rw-rw-r-- 1 corey corey 0 Mar 22 10:29 /home/corey/delete-test/sub-dir1/test-file1.test
-rw-rw-r-- 1 corey corey 0 Mar 22 10:29 /home/corey/delete-test/sub-dir1/test-file2.test
-rw-rw-r-- 1 corey corey 0 Mar 22 10:29 /home/corey/delete-test/sub-dir1/test-file3.test
[corey@localhost ~]$ ls -l ~/delete-test/sub-dir2/*
-rw-rw-r-- 1 corey corey 0 Mar 22 10:29 /home/corey/delete-test/sub-dir2/test-file4.test
-rw-rw-r-- 1 corey corey 0 Mar 22 10:29 /home/corey/delete-test/sub-dir2/test-file5.test
-rw-rw-r-- 1 corey corey 0 Mar 22 10:29 /home/corey/delete-test/sub-dir2/test-file6.test

Here is my input file that lists the full path to each file I want to delete:

Code: Select all


[corey@localhost ~]$ cat ~/delete-test/file-list.txt 
/home/corey/delete-test/sub-dir1/test-file1.test
/home/corey/delete-test/sub-dir1/test-file2.test
/home/corey/delete-test/sub-dir1/test-file3.test
/home/corey/delete-test/sub-dir2/test-file4.test
/home/corey/delete-test/sub-dir2/test-file5.test
/home/corey/delete-test/sub-dir2/test-file6.test


Here is the command to run:

Code: Select all


[corey@localhost ~]$ xargs rm -rf < ~/delete-test/file-list.txt


Here is the result:

Code: Select all


[corey@localhost ~]$ ls -l ~/delete-test/sub-dir1/*
ls: cannot access /home/corey/delete-test/sub-dir1/*: No such file or directory
[corey@localhost ~]$ ls -l ~/delete-test/sub-dir2/*
ls: cannot access /home/corey/delete-test/sub-dir2/*: No such file or directory
[corey@localhost ~]$ 


EDIT: cleaned it up so it wasn't in one huge code block
Last edited by cholq on Tue Mar 22, 2016 10:44 am, edited 2 times in total.

User avatar
Flemur
Level 17
Level 17
Posts: 7218
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Delete files in several (sub)directories

Post by Flemur » Tue Mar 22, 2016 10:35 am

I would do one of two things:

Code: Select all

rm `find -name "*.m3u"`
EDIT: THAT DON'T WORK IF YOU HAVE SPACES IN THE FILE OR DIRECTORY NAMES.
Here's a better way if you use spaces: http://www.cyberciti.biz/faq/linux-unix ... ove-files/

(note the ` is the ~ key, not ' a regular single quote)
or
edit the m3u.list file and put each line in quotes and stick 'rm ' in front of each line, then "sh m3u.list"; easy and fast with 'vi', maybe tedious with other text editors - kind of a kludgey, brute force thing, but you're less likely to get surprises.
Last edited by Flemur on Tue Mar 22, 2016 10:40 am, edited 1 time in total.
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
Mint 19.1 Xfce/fluxbox
Manjaro openbox/fluxbox

Mr.October
Level 4
Level 4
Posts: 211
Joined: Sat Oct 24, 2015 1:27 am

Re: Delete files in several (sub)directories

Post by Mr.October » Tue Mar 22, 2016 10:39 am

Flemur wrote:I would do one of two things:

Code: Select all

rm `find -name "*.m3u"`
EDIT: THAT DON'T WORK IF YOU HAVE SPACES IN THE FILE OR DIRECTORY NAMES.
(note the ` is the ~ key, not ' a regular single quote)
or
edit the m3u.list file and put each line in quotes and stick 'rm ' in front of each line, then "sh m3u.list"; easy and fast with 'vi', maybe tedious with other text editors - kind of a kludgey, brute force thing, but you're less likely to get surprises.
I'm not keen on using vi, how would I do what you are suggesting? I tried it already with a "normal" texteditor but couldn't find the right way.

Mr.October
Level 4
Level 4
Posts: 211
Joined: Sat Oct 24, 2015 1:27 am

Re: Delete files in several (sub)directories

Post by Mr.October » Tue Mar 22, 2016 10:46 am

cholq wrote:If you have all the files listed in a text file, you should be able to do it from the command line. Here is a quick test I did to confirm:

Here are the files to be deleted:

Code: Select all


[corey@localhost ~]$ ls -l ~/delete-test/sub-dir1/*
-rw-rw-r-- 1 corey corey 0 Mar 22 10:29 /home/corey/delete-test/sub-dir1/test-file1.test
-rw-rw-r-- 1 corey corey 0 Mar 22 10:29 /home/corey/delete-test/sub-dir1/test-file2.test
-rw-rw-r-- 1 corey corey 0 Mar 22 10:29 /home/corey/delete-test/sub-dir1/test-file3.test
[corey@localhost ~]$ ls -l ~/delete-test/sub-dir2/*
-rw-rw-r-- 1 corey corey 0 Mar 22 10:29 /home/corey/delete-test/sub-dir2/test-file4.test
-rw-rw-r-- 1 corey corey 0 Mar 22 10:29 /home/corey/delete-test/sub-dir2/test-file5.test
-rw-rw-r-- 1 corey corey 0 Mar 22 10:29 /home/corey/delete-test/sub-dir2/test-file6.test

Here is my input file that lists the full path to each file I want to delete:

Code: Select all


[corey@localhost ~]$ cat ~/delete-test/file-list.txt 
/home/corey/delete-test/sub-dir1/test-file1.test
/home/corey/delete-test/sub-dir1/test-file2.test
/home/corey/delete-test/sub-dir1/test-file3.test
/home/corey/delete-test/sub-dir2/test-file4.test
/home/corey/delete-test/sub-dir2/test-file5.test
/home/corey/delete-test/sub-dir2/test-file6.test


Here is the command to run:

Code: Select all


[corey@localhost ~]$ xargs rm -rf < ~/delete-test/file-list.txt


Here is the result:

Code: Select all


[corey@localhost ~]$ ls -l ~/delete-test/sub-dir1/*
ls: cannot access /home/corey/delete-test/sub-dir1/*: No such file or directory
[corey@localhost ~]$ ls -l ~/delete-test/sub-dir2/*
ls: cannot access /home/corey/delete-test/sub-dir2/*: No such file or directory
[corey@localhost ~]$ 


EDIT: cleaned it up so it wasn't in one huge code block

Thank you so much, this did the trick. I had a text file already so I only needed to use: xargs rm -rf < /Music/m3u.list
Now I will study xargs since this is a code I didn't know.

Thank you again cholq.

kwisher
Level 5
Level 5
Posts: 781
Joined: Wed Mar 05, 2008 12:54 pm
Location: Greentown, Indiana USA

Re: Delete files in several (sub)directories

Post by kwisher » Tue Mar 22, 2016 10:53 am

I'll add another command that just worked for me for a very similar situation.

Code: Select all

find [pathname] -name "*.m3u" -type f -delete
Leave off the -delete to return a list for confirmation. Since I was gun shy when running the command for the first time, I removed the -delete and ran the command as:

Code: Select all

find [pathname] -name "*.m3u" -type f > /path/foo.bar
Which output the results to a text file "foo.bar" so I could easily see the results.
The instructions suggested Windows XP or better, so I installed Linux :)

Mr.October
Level 4
Level 4
Posts: 211
Joined: Sat Oct 24, 2015 1:27 am

Re: Delete files in several (sub)directories

Post by Mr.October » Tue Mar 22, 2016 11:04 am

kwisher wrote:I'll add another command that just worked for me for a very similar situation.

Code: Select all

find [pathname] -name "*.m3u" -type f -delete
Leave off the -delete to return a list for confirmation. Since I was gun shy when running the command for the first time, I removed the -delete and ran the command as:

Code: Select all

find [pathname] -name "*.m3u" -type f > /path/foo.bar
Which output the results to a text file "foo.bar" so I could easily see the results.
Nice one, just one simple line and it is all deleted. Thank you, I will write this down (if I may) and use it more often.
Great help.

User avatar
RichardNL
Level 1
Level 1
Posts: 4
Joined: Wed Jul 06, 2016 4:13 pm

Re: Delete files in several (sub)directories

Post by RichardNL » Sun Jul 10, 2016 8:51 am

Code: Select all
find [pathname] -name "*.m3u" -type f > /path/foo.bar
An alternative
I use this command to show a list of the files:

Code: Select all

find [pathname] -name "*.m3u" -type f -ls

Post Reply

Return to “Scripts & Bash”