You are not logged in.

#1 2012-03-12 01:34:56

guvery
Member
Registered: 2011-10-30
Posts: 6

Removing backup files

I have a script that runs

if [ -f ~/bin/*~ ]; then
	rm ~/bin/*~
fi

In theory it checks to see if backup files exist in the bin directory, and then deletes them if they do.
But it seems that this does not work correctly and I haven't the slightest clue why. Any help?

Offline

#2 2012-03-12 03:43:25

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,152

Re: Removing backup files

Use

set -x

at the start to see what it does.


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#3 2012-03-13 10:05:41

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: Removing backup files

Maybe it doesn't work when there are several files ending with ~. "man test" says "-f FILE", not "FILES".

Edit: you can also remove the test and use "rm -f", though it might be risky.

Last edited by stqn (2012-03-13 10:07:09)

Offline

#4 2012-03-13 10:34:42

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Removing backup files

You can't check for the existence of multiple files using -f in test.
What you can do is either get a list of files and iterate through the list of files ending in ~ or use find.

eg:

for file in *~; do rm $file; done

Probably best to use echo rather than rm to test.


Edit: also to prevent future ones appearing this in vimrc will work:

set backupdir=/tmp

Or you can just turn them off.
If you use something like nano there may be a similar setting, I can't remember.

Last edited by skanky (2012-03-13 10:37:51)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#5 2012-03-13 12:45:21

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Removing backup files

I must be missing something...

rm -f ~/bin/*~

edit: if you really want to see if files exist matching a given pattern, it's not entirely straight forward:

shopt -s nullglob
files=("$HOME"/bin/*~)
if (( ${#files[*]} )); then
  # do stuff with "${files[@]}"
else
  # you has no files
fi

Last edited by falconindy (2012-03-13 12:46:24)

Offline

#6 2012-03-13 13:40:52

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Removing backup files

falconindy wrote:

I must be missing something...

Hmmm, no but I was seeing something that wasn't there (that is, a reason for the extra step). roll


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#7 2012-03-13 16:01:08

guvery
Member
Registered: 2011-10-30
Posts: 6

Re: Removing backup files

Thank you very much for your replies.. I will try some of what you guy's suggested later on. I did end up turning off backups though. I just like to have a clean bin folder smile

Offline

Board footer

Powered by FluxBB