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.
|
 |
02-10-2010, 01:22 AM
|
#1
|
Member
Registered: Aug 2009
Posts: 539
Rep:
|
How to delete selected files?
Assumed I have the following files:
test12.txt
test13.txt
test14.txt
test15.txt
I want to delete the first 3 files with one command. How to do that?
|
|
|
02-10-2010, 01:23 AM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
rm test12.txt test13.txt test14.txt
magic.
|
|
|
02-10-2010, 01:33 AM
|
#3
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
or
rm test1[234].txt
Slightly less typing.
|
|
1 members found this post helpful.
|
02-10-2010, 01:56 AM
|
#4
|
Member
Registered: Aug 2009
Posts: 539
Original Poster
Rep:
|
Quote:
Originally Posted by Tinkster
or
rm test1[234].txt
Slightly less typing.
|
This is the intelligent solution!
|
|
|
02-10-2010, 01:57 AM
|
#5
|
Member
Registered: Aug 2009
Posts: 539
Original Poster
Rep:
|
Quote:
Originally Posted by acid_kewpie
rm test12.txt test13.txt test14.txt
magic.
|
if there more than hundreds of files?
|
|
|
02-10-2010, 03:10 AM
|
#6
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
rm test1{103-300}.txt
rm test1*.txt
for num in $(seq 234 434); do
rm test${num}.txt
done
find . -maxdepth 1 -name "test1*.txt" -delete
|
|
1 members found this post helpful.
|
02-10-2010, 03:10 AM
|
#7
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by thomas2004ch
if there more than hundreds of files?
|
What acid_kewpie wanted to make you notice is that you did not provide many details about your requirement. In a real case you have to establish some criteria to select only a partial list of the files, using regular expressions and/or globbing. You can test your "selection" using ls and after you've carefully checked the result, you can do the same using the rm command.
If something goes wrong you can always restore the accidentally deleted files from your backup (do you have a backup, don't you?).
If you need to select files, based on numbering you can either use a character list as in the example by Tinkster, a numeric interval, shell's brace expansion or command substitution with seq.
|
|
|
02-10-2010, 11:46 AM
|
#8
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
Quote:
Originally Posted by thomas2004ch
This is the intelligent solution!
|
Congrats Tink! You're officially a worthy human!
|
|
|
02-10-2010, 11:50 AM
|
#9
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
OK, I still like my solution. It did what was asked. In terms of key presses, i'd do...
rm [tab]2[tab][tab]3[tab][tab]4[tab][enter] so 13 in total.
for Tinks version that'd be:
rm [tab][234][tab][enter] so 10... Hmm, maybe you do win. that's 2.4 calories you saved!
|
|
|
All times are GMT -5. The time now is 07:54 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
|
|