LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-06-2017, 04:08 PM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 182

Rep: Reputation: Disabled
Have a delete bash script, want to make it "verbose."



The script reads as follows:
Code:
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

# Clean Backup script
# For removing .bak, .backup and "~" files
# created by text editors (some of which are
# unaware they do it.)

find . -type f -name "*bak*" -exec rm -f {} \;
find . -type f -name "*.bak" -exec rm -f {} \;
find . -type f -name "*.backup" -exec rm -f {} \;
find . -type f -name "*~" -exec rm -f {} \;
echo "Done."
#if [  "$(ls *.bak 2>/dev/null)"]; then
#	rm *.bak
#else
#	echo "No files in this folder have the extension \".bak.\""
#fi
#if [  "$(ls *.backup 2>/dev/null)"]; then
#	rm *.backup
#else
#	echo "No files in this folder have the extension \".backup.\""
#fi
#if [ "$(ls *~  2>/dev/null)"]; then
#	rm *.~
#else
#	echo "No files in this folder have a tilde at the end."
#fi

IFS="$SAVEIFS"


You'll notice there are lines commented out. These were my attempts to return the names of files to be deleted to stdout. When these lines were executable, one or another thing did not happen: either the names of the files, by extension, could not be found, or they were printed to stdout and not deleted.

How should I correct this errant behaviour in execution? I want to see the located files returned to stdout by name and actually deleted.

Carver.
 
Old 12-06-2017, 05:31 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 715

Rep: Reputation: 292Reputation: 292Reputation: 292
Will the -print and -delete options of find not do what you want?
Code:
$ find . -type f \( -name "*bak*" -o -name "*~" \)  -print -delete
 
Old 12-11-2017, 09:09 PM   #3
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0, RHEL5,6), CentOS, SuSE (10.x, 11.x, 12.2, 13.2), Solaris (8-10), Tru64, MacOS, Raspian
Posts: 1,160

Rep: Reputation: 67
Quote:
Originally Posted by L_Carver View Post
# Clean Backup script
# For removing .bak, .backup and "~" files
# created by text editors (some of which are
# unaware they do it.)

find . -type f -name "*bak*" -exec rm -f {} \;
find . -type f -name "*.bak" -exec rm -f {} \;
find . -type f -name "*.backup" -exec rm -f {} \;
find . -type f -name "*~" -exec rm -f {} \;
Are you certain that you want that first "find" command? It will find files that do not meet your stated goals. For example, it would find and remove "tom_baker_resume.txt".
 
2 members found this post helpful.
Old 12-22-2017, 10:04 AM   #4
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 182

Original Poster
Rep: Reputation: Disabled
Chops to norobo. That command works very well. I have another script, killoriginal, which writes the file names of the items it deletes, and I would like this script, named cleanback, to do the same. Where would I add the command to 'log' the files this one deletes?

Re mturns suggestion: Should *bak and *~ work to avoid the error of removing files names with those strings (?!) elsewhere, as in your example of "tom_baker.txt"? I think i tried that once and it failed to work; of course then I was using the other find commands (see original script in my OP).

Carver

Last edited by L_Carver; 12-22-2017 at 10:05 AM. Reason: Corrected spellings.
 
Old 12-22-2017, 01:43 PM   #5
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 601

Rep: Reputation: 280Reputation: 280Reputation: 280
A comment to the given answer:
-print -delete prints the files to be deleted. But a deletion can fail, "permission denied" or "read-only filesystem".
Therefore it makes sense to switch the order.
-delete -print prints if the deletion succeeded.
 
Old 12-22-2017, 06:09 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,673

Rep: Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501
At the time you see the filename, it no longer exists.
I wonder about the usefulness of this information
 
Old 12-23-2017, 03:32 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,098

Rep: Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989
you must forget that IFS thing, do you really need it?
instead of
Code:
if [  "$(ls *.bak 2>/dev/null)"]; then
	rm *.bak
else
	echo "No files in this folder have the extension \".bak.\""
fi
you can simply write:
Code:
rm -f *.bak
# --- or ---
rm *.bak
#that will report if that was not found.
# --- and ---
# if you want to modify the error message:
rm *.bak 2>/dev/null || echo Error: '*.bak' was not found >&2
If you want to print filenames, just use:
Code:
echo *.bak
or if you want to check if that exists:
Code:
A=( *.bak )
if [[ "${A[0]}" == '*.bak' ]]; then
    echo *.bak was not found
else
    echo deleting *.bak 
    rm *.bak
fi
(not tested)
 
Old 12-23-2017, 04:19 AM   #8
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 601

Rep: Reputation: 280Reputation: 280Reputation: 280
Quote:
if [[ "${A[0]}" == '*.bak' ]]; then
will not delete a file that is really named *.bak
You also must test if it exists.

A for loop does it correctly, and ensures that all items are files.
Code:
for f in *.bak
do
  if [ -f "$f" ]
  then
    rm "$f" &&
    echo "$f  deleted"
  fi
done
 
Old 12-23-2017, 05:45 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,098

Rep: Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989
for f in *.bak
is deprecated and works only if there was no space or any other "strange" character in the filenames. Would be better to check if ${A[0]} was a real file as you suggested. Another solution would be to use the option failglob (shopt -o failglob).
 
Old 12-24-2017, 10:10 AM   #10
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 601

Rep: Reputation: 280Reputation: 280Reputation: 280
for f in *.bak is not deprecated, and the globbing works well with spaces in filenames (and with other soecial characters).
Probably you mix it up with for f in $var that can be problematic because word splitting (and then globbing) happens after variable substitution.
 
Old 12-27-2017, 03:21 PM   #11
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 182

Original Poster
Rep: Reputation: Disabled
Question

Quote:
Originally Posted by keefaz View Post
At the time you see the filename, it no longer exists.
I wonder about the usefulness of this information
With my killoriginal script, it gives me quick confirmation that that script works. Sometimes I may want to see how many backup files my preferred text editors create (those are Pluma and Xed.).

SO how might I get this script to log the deleted files by name, like killoriginal? Or is it even possible?

Carver
 
Old 12-27-2017, 10:54 PM   #12
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0, RHEL5,6), CentOS, SuSE (10.x, 11.x, 12.2, 13.2), Solaris (8-10), Tru64, MacOS, Raspian
Posts: 1,160

Rep: Reputation: 67
Quote:
Originally Posted by L_Carver View Post
Re mturns suggestion: Should *bak and *~ work to avoid the error of removing files names with those strings (?!) elsewhere, as in your example of "tom_baker.txt"? I think i tried that once and it failed to work; of course then I was using the other find commands (see original script in my OP).
Before deleting files, I tend to want to try running the "find" commands without the "-exec ..." portion. You'll see if any files are being found that you didn't expect/want to be in the list to be deleted. Especially since you're not specifying an exact path for the find command to locate files. (Would this be something you'd want to accidently run on "/", "/usr", etc?) Better to be sure than having to hope your last backup was successful.
 
Old 12-27-2017, 11:13 PM   #13
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0, RHEL5,6), CentOS, SuSE (10.x, 11.x, 12.2, 13.2), Solaris (8-10), Tru64, MacOS, Raspian
Posts: 1,160

Rep: Reputation: 67
Quote:
Originally Posted by L_Carver View Post
# Clean Backup script
# For removing .bak, .backup and "~" files
# created by text editors (some of which are
# unaware they do it.)

find . -type f -name "*bak*" -exec rm -f {} \;
find . -type f -name "*.bak" -exec rm -f {} \;
find . -type f -name "*.backup" -exec rm -f {} \;
find . -type f -name "*~" -exec rm -f {} \;

[snip]
I guess this has been rattling around in the back of my mind since I first saw it on LQ...

What if you did:
Code:
#!/bin/bash

(
find . -type f -name "*bak*"   # I still see a problem w/ this pattern
find . -type f -name "*.bak"
find . -type f -name "*.backup"
find . -type f -name "*~"
) | while read FP
do
   echo "Removing ${FP}..."
   rm ${FP}
done
You could also eliminate the "echo" by using "rm -v". (Is there a reason you don't want to use that switch?)

You could also string the name patterns together in a single "find":
Code:
find . -type f -name "*bak*" -o -name "*.bak" -o -name "*.backup" -o -name "*~"
and forego the enclosing parentheses before the pipe.

Just a couple of things you could consider.

Later...
 
Old 12-28-2017, 05:11 AM   #14
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,098

Rep: Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989
Quote:
Originally Posted by L_Carver View Post
With my killoriginal script, it gives me quick confirmation that that script works. Sometimes I may want to see how many backup files my preferred text editors create (those are Pluma and Xed.).

SO how might I get this script to log the deleted files by name, like killoriginal? Or is it even possible?

Carver
Did you try the solution from MadeInGermany (post #8)?
Did you try the solution from norobro (post #2)?
What is the problem with them? What kind of log do you need? What is that your killoriginal script?

Running 4 different find commands will be relatively slow, so better to use -o as it was already mentioned. But if you have only one single directory (without subdirs) again MadeInGermany gave a good tip:
Code:
for f in *.bak *.backup *~
do
  if [ -f "$f" ]
  then
    rm "$f" &&
    echo "$f  deleted"
  fi
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Shell Script: "bash: Bad Substitution - Script for remove "." " thiagofw Programming 14 12-09-2016 11:04 PM
[SOLVED] Bash - Capture "Child" script output with "Parent" without using files or coproc keyword Jason_25 Programming 2 02-14-2016 08:51 PM
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
[SOLVED] Can you make "grep" non-case sensitive in Bash script? Regnets1 Programming 4 01-06-2013 02:00 PM
How to write a simple BASH script to "test if have folowing files, than delete."? hocheetiong Programming 10 10-01-2009 01:17 PM

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

All times are GMT -5. The time now is 06:17 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration