LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-17-2017, 10:39 AM   #1
peter7089
Member
 
Registered: May 2016
Posts: 86

Rep: Reputation: Disabled
rm - delete list of files and directories from .txt file?


I have a .txt file with paths to files and directories that i want to delete. Is it possible to use the rm, or other command, and delete the files and folders that are inside this file? Somehow the delete command to be able to read the information from this .txt file.
 
Old 01-17-2017, 11:04 AM   #2
Drakeo
Senior Member
 
Registered: Jan 2008
Location: Urbana IL
Distribution: Slackware, Slacko,
Posts: 3,716
Blog Entries: 3

Rep: Reputation: 480Reputation: 480Reputation: 480Reputation: 480Reputation: 480
rm -rf /your/directory/*.txt
every file that ends in .txt will be deleted
 
Old 01-17-2017, 11:06 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
for i in `foo`; do bar; done
 
1 members found this post helpful.
Old 01-17-2017, 11:07 AM   #4
agillator
Member
 
Registered: Aug 2016
Distribution: Mint 19.1
Posts: 274

Rep: Reputation: Disabled
There is no single command I am aware of. It is simple enough to convert the text file to a script, though, if it contains only the list of full paths to remove. Open a copy of the file for editing in an editor (I use vim, emacs will work, or any number of others). Do a mass search and replace on the lines containing the paths or filenames to add 'rm' and a space at the beginning of each line (or rm -i and a space to be prompted before each removal). Add a 'shebang' (#!/bin/bash) as the first line. Then close the file, chmod to make it executable and execute it. Sounds complicated but really isn't if you know how to do the search and replace in the editor you are using. If you have vim loaded on your computer I can give you a specific list of the actions if you wish and have never done something like this before.

Habitual method is simpler, much simpler, by the way.

Last edited by agillator; 01-17-2017 at 11:08 AM.
 
Old 01-17-2017, 11:07 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,281

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
[untested]:
Code:
rm `cat files-to-delete.lst`
alternatively:
Code:
cat files-to-delete.lst | while read f
do
 rm $f
done

Last edited by schneidz; 01-17-2017 at 11:09 AM.
 
Old 01-17-2017, 11:14 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137
This is what xargs is built for
Code:
cat "files.txt" | xargs rm
Spaces in the filenames will cause problems though, if that's an issue you'll probably want to loop and use quotes in your rm.
 
Old 01-17-2017, 11:18 AM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Measure with a micrometer.
Mark it with chalk.
Cut it with an axe.
I love Linux.

This is one of my cornerstone go-to bash nuggets:
Code:
for i in `cat file` ; do rm -fr $i ; done
See also http://www.tldp.org/LDP/abs/html/varassignment.html

Last edited by Habitual; 01-17-2017 at 11:22 AM.
 
Old 01-17-2017, 11:22 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
For all those using for loops, I hope there is no white space in the names or paths to be deleted .. or this could get ugly from errors or the wrong thing being deleted real fast
 
1 members found this post helpful.
Old 01-17-2017, 11:30 AM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653
Have a pretty good explanation of how to do this over here: http://www.linuxquestions.org/questi...7/#post5639390

Just change the cp to mv or rm as needed.
 
Old 01-17-2017, 12:00 PM   #10
peter7089
Member
 
Registered: May 2016
Posts: 86

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by agillator View Post
There is no single command I am aware of. It is simple enough to convert the text file to a script, though, if it contains only the list of full paths to remove. Open a copy of the file for editing in an editor (I use vim, emacs will work, or any number of others). Do a mass search and replace on the lines containing the paths or filenames to add 'rm' and a space at the beginning of each line (or rm -i and a space to be prompted before each removal). Add a 'shebang' (#!/bin/bash) as the first line. Then close the file, chmod to make it executable and execute it. Sounds complicated but really isn't if you know how to do the search and replace in the editor you are using. If you have vim loaded on your computer I can give you a specific list of the actions if you wish and have never done something like this before.

Habitual method is simpler, much simpler, by the way.
Can i uninstall a package this way? For example, if i run "locate 'package-name' > file.txt" and then convert the file.txt file to script can i uninstall the package manually?
 
Old 01-17-2017, 12:28 PM   #11
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by peter7089 View Post
Can i uninstall a package this way?
You can, "sort of", but I don't recommend it.

http://xyproblem.info/
and https://ubuntuforums.org/showthread.php?t=2349683

It's best that you don't.

Last edited by Habitual; 01-17-2017 at 12:32 PM.
 
Old 01-17-2017, 12:43 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
Question would be why you would even want to? Think about it like this, if we used you locate idea on the gawk package for my machine, you would remove 36 files of the 116 installed for that one package.
As you can see, this is not ideal.

Perhaps you could explain further what your actual question is? (as pointed out above the xyproblem is a good read for you)
 
Old 01-17-2017, 02:32 PM   #13
agillator
Member
 
Registered: Aug 2016
Distribution: Mint 19.1
Posts: 274

Rep: Reputation: Disabled
By a package do you mean an installation from a distribution's repositories? If so and you installed it by that distribution's package manager, you should use that package manager to remove it. If you are talking about something else then as others have asked, exactly what are you talking about? You are starting to edge into areas that could do nasty things to your system if your aren't careful or if someone thinks you are talking about one thing and you are actually asking about something else.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Add a FileID and delete one line for 900 txt files Migasi Linux - Newbie 2 03-04-2014 04:52 PM
Question about delete/update record in txt file in C++ YouNoobsDie Programming 2 05-11-2013 09:01 AM
Script to delete a list of Directories quanvu07 Linux - Newbie 8 10-25-2010 09:26 PM
[SOLVED] remove directories that only contain .txt and .log files? jmark Linux - Newbie 13 08-09-2010 04:55 AM
Copy the contents of a txt file to other txt files (with similar names) by cp command TheIndependentAquarius Linux - Newbie 7 07-03-2010 01:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration