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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-17-2017, 09:39 AM
|
#1
|
Member
Registered: May 2016
Distribution: MX Linux
Posts: 254
Rep: 
|
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.
|
|
|
01-17-2017, 10:04 AM
|
#2
|
Senior Member
Registered: Jan 2008
Location: Urbana IL
Distribution: Slackware, Slacko,
Posts: 3,716
|
rm -rf /your/directory/*.txt
every file that ends in .txt will be deleted
|
|
|
01-17-2017, 10:06 AM
|
#3
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep: 
|
Code:
for i in `foo`; do bar; done
|
|
1 members found this post helpful.
|
01-17-2017, 10:07 AM
|
#4
|
Member
Registered: Aug 2016
Distribution: Mint 19.1
Posts: 419
Rep: 
|
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 10:08 AM.
|
|
|
01-17-2017, 10:07 AM
|
#5
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
[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 10:09 AM.
|
|
|
01-17-2017, 10:14 AM
|
#6
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
|
|
01-17-2017, 10:18 AM
|
#7
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep: 
|
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 10:22 AM.
|
|
|
01-17-2017, 10:22 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
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.
|
01-17-2017, 10:30 AM
|
#9
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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.
|
|
|
01-17-2017, 11:00 AM
|
#10
|
Member
Registered: May 2016
Distribution: MX Linux
Posts: 254
Original Poster
Rep: 
|
Quote:
Originally Posted by agillator
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?
|
|
|
01-17-2017, 11:28 AM
|
#11
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep: 
|
Quote:
Originally Posted by peter7089
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 11:32 AM.
|
|
|
01-17-2017, 11:43 AM
|
#12
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
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)
|
|
|
01-17-2017, 01:32 PM
|
#13
|
Member
Registered: Aug 2016
Distribution: Mint 19.1
Posts: 419
Rep: 
|
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.
|
|
|
All times are GMT -5. The time now is 04:02 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|