LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to delete selected files? (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-delete-selected-files-788152/)

thomas2004ch 02-10-2010 01:22 AM

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?

acid_kewpie 02-10-2010 01:23 AM

rm test12.txt test13.txt test14.txt

magic.

Tinkster 02-10-2010 01:33 AM

or

rm test1[234].txt

Slightly less typing.

thomas2004ch 02-10-2010 01:56 AM

Quote:

Originally Posted by Tinkster (Post 3858680)
or

rm test1[234].txt

Slightly less typing.


This is the intelligent solution!

thomas2004ch 02-10-2010 01:57 AM

Quote:

Originally Posted by acid_kewpie (Post 3858668)
rm test12.txt test13.txt test14.txt

magic.

if there more than hundreds of files?

jschiwal 02-10-2010 03:10 AM

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

colucix 02-10-2010 03:10 AM

Quote:

Originally Posted by thomas2004ch (Post 3858686)
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.

acid_kewpie 02-10-2010 11:46 AM

Quote:

Originally Posted by thomas2004ch (Post 3858684)
This is the intelligent solution!

Congrats Tink! You're officially a worthy human!

acid_kewpie 02-10-2010 11:50 AM

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 02:50 AM.