LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to delete multiple files ranging from 1 to 10 using rm command (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-delete-multiple-files-ranging-from-1-to-10-using-rm-command-4175643965/)

chripy811 12-09-2018 08:20 PM

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?

syg00 12-09-2018 08:42 PM

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

chripy811 12-09-2018 09:03 PM

that didn't work, it didn't delete anything

syg00 12-09-2018 09:14 PM

It wasn't supposed to - it was an attempt to get you thinking about how to solve it for yourself.

chripy811 12-09-2018 09:16 PM

but your example is like programming, php, javascript,

i don't know how to do programming from the terminal, i never learned that before

ArfaSmif 12-09-2018 10:57 PM

Have a look at

ls *[1-10]*

See what happens. If it's ok use

rm *[1-10]*

ondoho 12-10-2018 12:08 AM

Quote:

Originally Posted by chripy811 (Post 5935301)
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.

MadeInGermany 12-10-2018 03:10 AM

Quote:

Originally Posted by ArfaSmif (Post 5935316)
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)!

Turbocapitalist 12-10-2018 03:21 AM

Quote:

Originally Posted by chripy811 (Post 5935301)
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.

MadeInGermany 12-10-2018 03:28 AM

Quote:

Originally Posted by ondoho (Post 5935331)
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

zeebra 12-10-2018 06:17 AM

Quote:

Originally Posted by chripy811 (Post 5935289)
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.

ehartman 12-10-2018 07:25 AM

Quote:

Originally Posted by chripy811 (Post 5935289)
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

chripy811 12-10-2018 07:49 AM

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)

pan64 12-10-2018 08:18 AM

did you try anything posted? Which one? What's happened?

l0f4r0 12-10-2018 08:46 AM

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:]])\)*



All times are GMT -5. The time now is 02:43 PM.