Author Topic: Linux RM Command Help  (Read 1878 times)

0 Members and 1 Guest are viewing this topic.

Offline nicknailsTopic starter

  • Regular Contributor
  • *
  • Posts: 59
Linux RM Command Help
« on: December 22, 2021, 03:15:15 pm »
I have a machine (linux based) that is running software that creates a lot of backup files (OLD extension) when anything is changed. Right now, there's a file that you load onto a thumb drive, and it copies everything from the machine to the thumb drive (full backup of system). I've modified the code to copy just the backup files (OLD extension) on the machine.

cd $SRC_DIR
find . | grep -E '\.(old0|old1|old2|old3|old4|old5|old6|old7|old8|old9)$' | cpio -vdump --quiet $DATAPATH &> $list

Now that those specific files are backed up, I want to delete them. I'm assuming I can do
find . | grep -E '\.(old0|old1|old2|old3|old4|old5|old6|old7|old8|old9)$' | **RM Command Here**

Can I just to "rm -r"?

I don't think I need the cd $SRC_DIR since it should already be in that directory.

Also, can I just to old* or something to that effect instead of listing them all out?
 

Offline mag_therm

  • Frequent Contributor
  • **
  • Posts: 689
  • Country: us
Re: Linux RM Command Help
« Reply #1 on: December 22, 2021, 04:17:55 pm »
It might be necessary to use the following to avoid a query at each file:
yes| rm ...
or rm -f....

I use the following for silent backup of only the files that have changed since last bak:
yes| cp -uvr  /run/media/Me/Mypaths/* /run/media/Me/SeaGate2TB_A/My_bak path/
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 26907
  • Country: nl
    • NCT Developments
Re: Linux RM Command Help
« Reply #2 on: December 22, 2021, 05:08:01 pm »
IIRC find (or a similar command) can be used to delete the files without needing to use rm .
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline Cerebus

  • Super Contributor
  • ***
  • Posts: 10576
  • Country: gb
Re: Linux RM Command Help
« Reply #3 on: December 22, 2021, 05:18:48 pm »
If that's your complete pattern for files then "rm .old?" would junk them.
Anybody got a syringe I can use to squeeze the magic smoke back into this?
 

Offline eti

  • Super Contributor
  • ***
  • !
  • Posts: 1801
  • Country: gb
  • MOD: a.k.a Unlokia, glossywhite, iamwhoiam etc
Re: Linux RM Command Help
« Reply #4 on: December 22, 2021, 08:01:41 pm »
Try this:

Code: [Select]
for f in {0..9}; do rm *.old$f; done
What this is saying, is this: "For every iteration from 0-9..." (that's the part in braces {0..9}) "... remove any file ending with .old0, old1, old2... etc, up to .old9, and when finished, exit"

The part
Code: [Select]
for f in {0..9} is specifying what the variable "f", subsequently referenced as "$f", will be on the next pass, and the range {0..9} constrains it to that range.

Non-destructive test:

Make a test directory "mkdir test_del" or w/e and enter it, and then do this:

Code: [Select]
for f in {0..9}; do touch TEST_FILE.old$f; done which generates empty test files with the same extensions as you're working with (Screenshot attached)

Now, staying in this test directory, you may run my command and ensure it works as expected, without risking data loss to the ACTUAL files.
« Last Edit: December 22, 2021, 08:18:47 pm by eti »
 

Offline PaulAm

  • Frequent Contributor
  • **
  • Posts: 938
  • Country: us
Re: Linux RM Command Help
« Reply #5 on: December 22, 2021, 08:06:29 pm »
Something along the lines of

find . -name "*.old?" -exec rm {} \;

would do it.  Some versions of find have a -delete option, but not all.  If you use -delete, make sure it's AFTER the -name argument.

If you wanted to restrict that to numeric chars only, use *.old[0-9] as the file pattern.

Deleting mass files makes me nervous.  I would probably use find to make a list of files to delete, verify it and then either turn it into a shell script with an editor macro or feed it into rm through xargs.
« Last Edit: December 22, 2021, 08:12:19 pm by PaulAm »
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6264
  • Country: fi
    • My home page and email address
Re: Linux RM Command Help
« Reply #6 on: December 23, 2021, 05:02:27 am »
Like PaulAm said, many find versions support -delete :
    find . -name '*.old[0-9]' -delete

Many rm versions support -i (for interactive):
    find . -name '*.old[0-9]' -exec rm -vi '{}' ';'
which asks you whether you wish to delete each file.

With Bash (and other shells whose printf supports %q), you can use
    find . -name '*.old[0-9]' -exec printf 'rm -f %q\n' '{}' ';' > remove-old
to generate a list of commands, one per line, to delete each file.  The %q is special for bash printf built-in: it prints the file name, but escapes special characters.
Edit the remove-old file, verifying it doesn't delete any files you wish to keep, and then run the commands in it by sourcing it,
    . remove-old
and finally removing it (rm -f remove-old).

For other shells, I'd use something like
    find . -name '*.old[0-9]' -exec echo "rm -f '{}'" ';' > remove-old
but be very careful of files with apostrophes (').
 

Offline Ed.Kloonk

  • Super Contributor
  • ***
  • Posts: 4000
  • Country: au
  • Cat video aficionado
Re: Linux RM Command Help
« Reply #7 on: December 23, 2021, 06:54:18 am »
Deleting mass files makes me nervous.  I would probably use find to make a list of files to delete, verify it and then either turn it into a shell script with an editor macro or feed it into rm through xargs.

What I do often in this situation is move the files to a temporary location (preferably, on the same partition) with the command rather than delete them. You can delete the temporary tree once satisfied that all that is wanted and nothing unwanted is where it should be.
« Last Edit: December 23, 2021, 06:56:01 am by Ed.Kloonk »
iratus parum formica
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6264
  • Country: fi
    • My home page and email address
Re: Linux RM Command Help
« Reply #8 on: December 23, 2021, 07:47:32 am »
Code: [Select]
for f in {0..9}; do rm *.old$f; done
Keep it simple.
In Bash and POSIX shells,
    rm *.old[0-9]
deletes exactly the same files.  The only difference is that this does not complain if one of the files does not exist, while your loop does complain (assuming POSIX default or typical rm behaviour).

Why are you calling a shell loop "elegant", when a single command suffices?  While telling others to Keep it simple?

>:D, of course.
 
The following users thanked this post: eti

Offline PaulAm

  • Frequent Contributor
  • **
  • Posts: 938
  • Country: us
Re: Linux RM Command Help
« Reply #9 on: December 23, 2021, 03:19:29 pm »
This is Unix, there's at least 20 different ways to achieve the identical result.  It's not elegant unless the command looks like line noise  :-DD

In the FWIW department, the for loop command deletes files in a directory, the OP was looking in a directory tree, so it wouldn't actually do what they wanted.

And to specifically answer the OP's question.  rm does not take arguments (eg pathnames to remove) through stdin.  You'd need to do that with xargs and you'd have to be careful if spaces show up in names.  But you could just replace the ** RM Command Here ** in the original question with xargs rm

There must be 50 different ways to delete your files ...
 

Offline Cerebus

  • Super Contributor
  • ***
  • Posts: 10576
  • Country: gb
Re: Linux RM Command Help
« Reply #10 on: December 23, 2021, 03:56:04 pm »
This is Unix, there's at least 20 different ways to achieve the identical result.  It's not elegant unless the command looks like line noise  :-DD

I know you're joking, but I'll still take strong objection to that. The original Unix design philosophy, and stated as such in the original papers on it, was that a command should do "one thing only, and do that one thing well". Then people ignored that and you got abominations like 'find' where there are options for everything under the sun, and it is far from the worst example there is. Commands like 'find' and 'rdist' are boobytrapped by the range of choices; if you're going to do something non-reversable with them you always need to run a non-destructive test case first to be sure that that you're going to get what you thought you were asking for. "rm -rf" is no less potentially destructive, but you at least get what you should have clearly known you were asking for.
Anybody got a syringe I can use to squeeze the magic smoke back into this?
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14481
  • Country: fr
Re: Linux RM Command Help
« Reply #11 on: December 23, 2021, 06:38:27 pm »
This is Unix, there's at least 20 different ways to achieve the identical result.  It's not elegant unless the command looks like line noise  :-DD

While many commands look a bit cryptic, they are powerful and avoid having to write your own tools most of the time. That's the key point. Take it or leave it. Yes, there is a learning curve.

There must be 50 different ways to delete your files ...

I don't see why this is a problem. Unless you were using a very crippled system, there will always be tons of different ways of solving a given problem.
 

Offline PaulAm

  • Frequent Contributor
  • **
  • Posts: 938
  • Country: us
Re: Linux RM Command Help
« Reply #12 on: December 23, 2021, 07:03:57 pm »
No time to write the lyrics, but if Paul Simon was a Unix nerd he might have penned a song with that name.

Find has been around since Version 5 days so it's part of Unix culture even it it doesn't quite fit the ideal design paradigm (it was written along with cpio).  We could get into a black hole on how the current kernel and system development path diverges from the original goals, but that's too far off the topic.

The point of having "small tools that do one thing" is that multiple tools can be cobbled together to do a task, but different tools can be combined in different ways to achieve the same task.  This is not a bad thing.  Beats the heck out of point and click.
 

Offline eti

  • Super Contributor
  • ***
  • !
  • Posts: 1801
  • Country: gb
  • MOD: a.k.a Unlokia, glossywhite, iamwhoiam etc
Re: Linux RM Command Help
« Reply #13 on: December 23, 2021, 08:02:26 pm »
Code: [Select]
for f in {0..9}; do rm *.old$f; done
Keep it simple.
In Bash and POSIX shells,
    rm *.old[0-9]
deletes exactly the same files.  The only difference is that this does not complain if one of the files does not exist, while your loop does complain (assuming POSIX default or typical rm behaviour).

Why are you calling a shell loop "elegant", when a single command suffices?  While telling others to Keep it simple?

>:D, of course.

Your method was unknown to me, but now I see it is more elegant, thanks.
 

Offline cdev

  • Super Contributor
  • ***
  • !
  • Posts: 7350
  • Country: 00
Re: Linux RM Command Help
« Reply #14 on: December 23, 2021, 11:31:53 pm »
I alias rm to rm -i as a safety measure, usually. It can save your ass.
"What the large print giveth, the small print taketh away."
 

Offline Ed.Kloonk

  • Super Contributor
  • ***
  • Posts: 4000
  • Country: au
  • Cat video aficionado
Re: Linux RM Command Help
« Reply #15 on: December 23, 2021, 11:33:53 pm »
I alias rm to rm -i as a safety measure, usually. It can save your ass.

Are you sure you really want to do that?

 :P
iratus parum formica
 

Offline Cerebus

  • Super Contributor
  • ***
  • Posts: 10576
  • Country: gb
Re: Linux RM Command Help
« Reply #16 on: December 23, 2021, 11:44:01 pm »
I alias rm to rm -i as a safety measure, usually. It can save your ass.

Are you sure you really want to do that?

 :P

Hi! You look like you're trying to make a witty comment online, can I help you with that?

 :P
Anybody got a syringe I can use to squeeze the magic smoke back into this?
 
The following users thanked this post: Ed.Kloonk

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14481
  • Country: fr
Re: Linux RM Command Help
« Reply #17 on: December 23, 2021, 11:50:34 pm »
I alias rm to rm -i as a safety measure, usually. It can save your ass.

Are you sure you really want to do that?

 :P

Aliasing rm to 'sudo rm -rf /' is much more efficient, though.
 
The following users thanked this post: Ed.Kloonk

Offline eti

  • Super Contributor
  • ***
  • !
  • Posts: 1801
  • Country: gb
  • MOD: a.k.a Unlokia, glossywhite, iamwhoiam etc
Re: Linux RM Command Help
« Reply #18 on: December 24, 2021, 07:58:58 pm »
I alias rm to rm -i as a safety measure, usually. It can save your ass.

Are you sure you really want to do that?

 :P

Aliasing rm to 'sudo rm -rf /' is much more efficient, though.

Why on earth would you advise anyone to do that? You have no way of knowing their level of knowledge, and could be responsible for their systems' destruction. Irresponsible and immature.
 

Offline Cerebus

  • Super Contributor
  • ***
  • Posts: 10576
  • Country: gb
Re: Linux RM Command Help
« Reply #19 on: December 24, 2021, 08:13:10 pm »
 :palm:

Anybody got a syringe I can use to squeeze the magic smoke back into this?
 
The following users thanked this post: Ed.Kloonk

Offline PaulAm

  • Frequent Contributor
  • **
  • Posts: 938
  • Country: us
Re: Linux RM Command Help
« Reply #20 on: December 25, 2021, 12:01:38 am »
Heh, I was going to say there's no need to do that cause if you're around systems long enough you're bound to do it at least once anyway  :-DD

Well, that's how to test whether or not your backup strategy works.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf