LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 12-09-2018, 08:20 PM   #1
chripy811
Member
 
Registered: Mar 2017
Posts: 31

Rep: Reputation: Disabled
How to delete multiple files ranging from 1 to 10 using rm command


How can i delete a range of files,

my file names look like this: Myvideo - My Car (34).avi

i figured out how to delete one by one

rm *\(1\)*
rm *\(2\)*
rm *\(3\)*
rm *\(4\)*


but that takes to long,

i want to delete up to the number from 1 to 100,


how can i achieve this?

Last edited by chripy811; 12-09-2018 at 08:26 PM.
 
Old 12-09-2018, 08:42 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
I'm not a big fan of using wildcards in things like "rm", but it's your data. You need to loop over the file(name)s in question. See if this gives you an idea of how to start.
Code:
for i in {1..10} ; do echo $i ; done
 
Old 12-09-2018, 09:03 PM   #3
chripy811
Member
 
Registered: Mar 2017
Posts: 31

Original Poster
Rep: Reputation: Disabled
that didn't work, it didn't delete anything
 
Old 12-09-2018, 09:14 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
It wasn't supposed to - it was an attempt to get you thinking about how to solve it for yourself.
 
Old 12-09-2018, 09:16 PM   #5
chripy811
Member
 
Registered: Mar 2017
Posts: 31

Original Poster
Rep: Reputation: Disabled
but your example is like programming, php, javascript,

i don't know how to do programming from the terminal, i never learned that before
 
Old 12-09-2018, 10:57 PM   #6
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
Have a look at

ls *[1-10]*

See what happens. If it's ok use

rm *[1-10]*
 
Old 12-10-2018, 12:08 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by chripy811 View Post
but your example is like programming, php, javascript,

i don't know how to do programming from the terminal, i never learned that before
then it's time you started.

but meanwhile, you can use any script on the command line; either by entering it just as it is (will evoke a secondary prompt, might be confusing) - or by chaining the commands with semicolons, which is what syg00 did.
in other words, you can copy-paste that example straight into your terminal.
i will extend it a little:
Code:
for i in {1..10} ; do echo rm *\($i\)* ; done
this will NOT delete anything.
 
Old 12-10-2018, 03:10 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Quote:
Originally Posted by ArfaSmif View Post
Have a look at

ls *[1-10]*

See what happens. If it's ok use

rm *[1-10]*
That does not work. [ ] can only return one character!
In fact it is identical with [01-1] or [01] or [0-1].
You must either use a loop or
Code:
(shopt -s nullglob; echo \rm *\({1..10}\).*)
Notes:
{x..y} and shopt require bash.
I put a backslash before rm to bypass any alias or function (in case it's an interactive shell).
I put echo before the \rm to show what it would do; remove the echo to really do it!
I put ( ) around the whole thing to run it in a subshell, so the nullglob effect does not remain in the main(=current) shell.
I put a . after the \) to require exactly that. Always be most specific to prevent from a false match(=deletion)!

Last edited by MadeInGermany; 12-10-2018 at 03:12 AM.
 
2 members found this post helpful.
Old 12-10-2018, 03:21 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,332
Blog Entries: 3

Rep: Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727
Quote:
Originally Posted by chripy811 View Post
but your example is like programming, ...
That's because the shell is a scripting language, not just a user interface. It's very well worth the effort of learning. The beginning may be hardest but the return on effort starts to show quickly once you start to feel a little comfortable with it. I highly recommend and encourage it.
 
Old 12-10-2018, 03:28 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Quote:
Originally Posted by ondoho View Post
then it's time you started.
...
Code:
for i in {1..10} ; do echo rm *\($i\)* ; done
Not precise enough. $i must be in "quotes"
Code:
for i in {1..10} ; do echo rm *\("$i"\)* ; done
And then you you can move the ( ) into the "quotes", looks nicer doesn't it:
Code:
for i in {1..10} ; do echo rm *"($i)"* ; done
And maybe you can demand a . after the )
The . has no special meaning, so it can be within the "quotes" or outside:
Code:
for i in {1..10} ; do echo rm *"($i)".* ; done
 
Old 12-10-2018, 06:17 AM   #11
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,833
Blog Entries: 17

Rep: Reputation: 640Reputation: 640Reputation: 640Reputation: 640Reputation: 640Reputation: 640
Quote:
Originally Posted by chripy811 View Post
How can i delete a range of files,

my file names look like this: Myvideo - My Car (34).avi

i figured out how to delete one by one

rm *\(1\)*
rm *\(2\)*
rm *\(3\)*
rm *\(4\)*


but that takes to long,

i want to delete up to the number from 1 to 100,


how can i achieve this?
Range or subset of a range?

If you simply have a range
file1
file2
file3

you can just do rm fi*. This deletes anything in that place which starts with fi.

Or rm *.avi, deletes all files ending with .avi

For a subset, this is obviously not a good option, if you want to delete file1 and file3, but not file 2.

For multiple files you could add -r to the rm as an option.


https://askubuntu.com/questions/8067...two-characters
https://stackoverflow.com/questions/...-bash-on-linux

These two threads probably explain what you want. This specifies with curly brackets. But please, do test these things before actually doing them. Make some files with ranges and test out how these works before actually doing it on real files.

Last edited by zeebra; 12-10-2018 at 06:28 AM.
 
Old 12-10-2018, 07:25 AM   #12
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by chripy811 View Post
How can i delete a range of files,

my file names look like this: Myvideo - My Car (34).avi

i want to delete up to the number from 1 to 100,
Look at the 'seq' command in combination with a for loop, like
Code:
for n in `seq 1 100`; do <whatever>;done
If the filenames have prefix 0's (like 001, 010 etc), use the -w option to seq
 
Old 12-10-2018, 07:49 AM   #13
chripy811
Member
 
Registered: Mar 2017
Posts: 31

Original Poster
Rep: Reputation: Disabled
i don't want to delete all the files with .avi or files starting with myvideo.

What i'm trying to do is remove duplicates



Myvideo - My Car.avi
Myvideo - My Car (1).avi
Myvideo - My Car (2).avi
Myvideo - My Car (3).avi
Myvideo - My Car (4).avi
Myvideo - My Car (5).avi


Coolvideo - My Dog.avi
Coolvideo - My Dog (1).avi
Coolvideo - My Dog (2).avi
Coolvideo - My Dog (3).avi
Coolvideo - My Dog (4).avi
Coolvideo - My Dog (5).avi
Coolvideo - My Dog (6).avi



so what im trying to delete is all the files with brackets (1) (2) (3)

Last edited by chripy811; 12-10-2018 at 07:51 AM.
 
Old 12-10-2018, 08:18 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
did you try anything posted? Which one? What's happened?
 
Old 12-10-2018, 08:46 AM   #15
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
As you only want to remove duplicates (whatever its duplicate number is), it seems that you just need:
Code:
rm *\([[:digit:]]*\)*
Disclaimer: you must NOT have in the same folder some files with "(some_number)" pattern that you would want to keep as they will be erased as well with that command...

EDIT: even better, you could use:
Code:
shopt -s extglob
rm *\(+([[:digit:]])\)*

Last edited by l0f4r0; 12-10-2018 at 09:10 AM.
 
  


Reply



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
Bash script to ping a range or own IP-range ugurgazi Programming 13 05-02-2016 09:08 AM
[SOLVED] iptables port forward not working for port range mapping to anohter Port range in Linux 2.6.39 kinghong66 Linux - Networking 2 06-17-2015 07:17 PM
Gnuplot - Combining a linear range and log-scale range in the same X-axis llauro Linux - Software 2 04-26-2011 12:55 PM
[SOLVED] What are short range link and long range links in routing? mq15 Linux - Networking 6 06-26-2009 11:16 PM
Delete specific Range of lines Using sed , awk, grep etc. joyds219 Linux - Newbie 4 03-28-2008 08:59 AM

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

All times are GMT -5. The time now is 10:21 PM.

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