LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-10-2010, 01:22 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Rep: Reputation: 33
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?
 
Old 02-10-2010, 01:23 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
rm test12.txt test13.txt test14.txt

magic.
 
Old 02-10-2010, 01:33 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
or

rm test1[234].txt

Slightly less typing.
 
1 members found this post helpful.
Old 02-10-2010, 01:56 AM   #4
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by Tinkster View Post
or

rm test1[234].txt

Slightly less typing.

This is the intelligent solution!
 
Old 02-10-2010, 01:57 AM   #5
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by acid_kewpie View Post
rm test12.txt test13.txt test14.txt

magic.
if there more than hundreds of files?
 
Old 02-10-2010, 03:10 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683
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.
Old 02-10-2010, 03:10 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Quote:
Originally Posted by thomas2004ch View Post
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.
 
Old 02-10-2010, 11:46 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by thomas2004ch View Post
This is the intelligent solution!
Congrats Tink! You're officially a worthy human!
 
Old 02-10-2010, 11:50 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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!
 
  


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
need to rsync only selected files (--files-from) also need to delete files on dest. ? BrianK Linux - General 5 10-22-2009 09:52 PM
Granting selected user access to selected network interface dexen Linux - Networking 2 10-04-2009 03:51 AM
Can I delete files in /mnt/tmp? and Files in the trash can will not delete? M$ISBS Slackware 15 10-02-2009 11:56 PM
Samba - prevent locking of selected files over network? humbletech99 Linux - Server 2 07-16-2009 04:46 AM
why does Amarok open only one of selected files when I hit Enter? newbiesforever Linux - Software 0 05-07-2009 09:24 PM

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

All times are GMT -5. The time now is 07:54 AM.

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
Open Source Consulting | Domain Registration