Linux RM Command Help - Page 1
Forum section: electronics / general

author: nicknails
author_rank: Regular Contributor
timestamp: December 22, 2021, 03:15:15 pm
content: 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_DIRfind . | grep -E '\.(old0|old1|old2|old3|old4|old5|old6|old7|old8|old9)$' | cpio -vdump --quiet $DATAPATH &> $listNow that those specific files are backed up, I want to delete them. I'm assuming I can dofind . | 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?

author: mag_therm
author_rank: Frequent Contributor
timestamp: December 22, 2021, 04:17:55 pm
content: 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/

author: nctnico
author_rank: Super Contributor
timestamp: December 22, 2021, 05:08:01 pm
content: IIRC find (or a similar command) can be used to delete the files without needing to use rm .

author: Cerebus
author_rank: Super Contributor
timestamp: December 22, 2021, 05:18:48 pm
content: If that's yourcompletepattern for files then "rm .old?" would junk them.

author: eti
author_rank: Super Contributor
timestamp: December 22, 2021, 08:01:41 pm
content: Try this:Code:for f in {0..9}; do rm *.old$f; doneWhat 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 partCode: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:for f in {0..9}; do touch TEST_FILE.old$f; donewhich 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.

author: PaulAm
author_rank: Frequent Contributor
timestamp: December 22, 2021, 08:06:29 pm
content: Something along the lines offind . -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.

author: Nominal Animal
author_rank: Super Contributor
timestamp: December 23, 2021, 05:02:27 am
content: Like PaulAm said, many find versions support-delete:find . -name '*.old[0-9]' -deleteMany 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 usefind . -name '*.old[0-9]' -exec printf 'rm -f %q\n' '{}' ';' > remove-oldto generate a list of commands, one per line, to delete each file. The%qis special for bash printf built-in: it prints the file name, but escapes special characters.Edit theremove-oldfile, verifying it doesn't delete any files you wish to keep, and then run the commands in it by sourcing it,. remove-oldand finally removing it (rm -f remove-old).For other shells, I'd use something likefind . -name '*.old[0-9]' -exec echo "rm -f '{}'" ';' > remove-oldbut be very careful of files with apostrophes (').

author: Ed.Kloonk
author_rank: Super Contributor
timestamp: December 23, 2021, 06:54:18 am
content: Quote from: PaulAm on December 22, 2021, 08:06:29 pmWhat 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.

author: Nominal Animal
author_rank: Super Contributor
timestamp: December 23, 2021, 07:47:32 am
content: Quote from: eti on December 22, 2021, 08:01:41 pmQuote from: eti on December 23, 2021, 07:15:26 amIn 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?, of course.

author: PaulAm
author_rank: Frequent Contributor
timestamp: December 23, 2021, 03:19:29 pm
content: 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 noiseIn 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 rmThere must be 50 different ways to delete your files ...

author: Cerebus
author_rank: Super Contributor
timestamp: December 23, 2021, 03:56:04 pm
content: Quote from: PaulAm on December 23, 2021, 03:19:29 pmI 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.

author: SiliconWizard
author_rank: Super Contributor
timestamp: December 23, 2021, 06:38:27 pm
content: Quote from: PaulAm on December 23, 2021, 03:19:29 pmWhile 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.Quote from: PaulAm on December 23, 2021, 03:19:29 pmI 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.

author: PaulAm
author_rank: Frequent Contributor
timestamp: December 23, 2021, 07:03:57 pm
content: 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.

author: eti
author_rank: Super Contributor
timestamp: December 23, 2021, 08:02:26 pm
content: Quote from: Nominal Animal on December 23, 2021, 07:47:32 amYour method was unknown to me, but now I see it is more elegant, thanks.

author: cdev
author_rank: Super Contributor
timestamp: December 23, 2021, 11:31:53 pm
content: I alias rm to rm -i as a safety measure, usually. It can save your ass.

author: Ed.Kloonk
author_rank: Super Contributor
timestamp: December 23, 2021, 11:33:53 pm
content: Quote from: cdev on December 23, 2021, 11:31:53 pmAre you sure you really want to do that?

author: Cerebus
author_rank: Super Contributor
timestamp: December 23, 2021, 11:44:01 pm
content: Quote from: Ed.Kloonk on December 23, 2021, 11:33:53 pmHi! You look like you're trying to makea witty comment online, can I help you with that?

author: SiliconWizard
author_rank: Super Contributor
timestamp: December 23, 2021, 11:50:34 pm
content: Quote from: Ed.Kloonk on December 23, 2021, 11:33:53 pmAliasing rm to 'sudo rm -rf /' is much more efficient, though.

author: eti
author_rank: Super Contributor
timestamp: December 24, 2021, 07:58:58 pm
content: Quote from: SiliconWizard on December 23, 2021, 11:50:34 pmWhy 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.

author: PaulAm
author_rank: Frequent Contributor
timestamp: December 25, 2021, 12:01:38 am
content: 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 anywayWell, that's how to test whether or not your backup strategy works.