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, 02:22 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 507

Rep: Reputation: 31
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, 02:23 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

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

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

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
or

rm test1[234].txt

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

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

rm test1[234].txt

Slightly less typing.

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

Original Poster
Rep: Reputation: 31
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, 04:10 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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, 04:10 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 12:46 PM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

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

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
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 10:52 PM
Granting selected user access to selected network interface dexen Linux - Networking 2 10-04-2009 04:51 AM
Can I delete files in /mnt/tmp? and Files in the trash can will not delete? M$ISBS Slackware 15 10-03-2009 12:56 AM
Samba - prevent locking of selected files over network? humbletech99 Linux - Server 2 07-16-2009 05:46 AM
why does Amarok open only one of selected files when I hit Enter? newbiesforever Linux - Software 0 05-07-2009 10:24 PM


All times are GMT -5. The time now is 09:47 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration