LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-24-2010, 05:18 PM   #1
bobkatz
LQ Newbie
 
Registered: Jan 2010
Distribution: Whatever's in my Thecus 5500!
Posts: 22

Rep: Reputation: 1
Help with `find -exec` : Remove selected files from a backup folder


Quote:
Originally Posted by AlucardZero View Post
Code:
find / -name test1 -exec ls -l {} \;
Works for me (note the space and the removal of one \). You may need to enclose the {} in single quotes, as mentioned in find's man page.
Hi, guys, I know this thread is four years old, but life goes on and there's always someone learning. I've just joined your forum and many thanks for being there! My profession is audio engineer and I maintain our NAS servers out of desperation. I would appreciate any help on some command-line syntax as I'm stumped.

Your hint in this thread is the closest I have gotten to getting my syntax working, but unfortunately, I'm still way off...

I have a backup folder which I need to prune and I've been trying to do a find and destroy action on files in this folder which have not been modified for more than 30 days. I figure if no users complain that their files have been missing for more than 30 days, it's safe to delete them from this folder.

My first try produced this result:

root@127.0.0.1:/raid/data/incremental-backup# find -mtime +90 -exec rm -rf {} \;
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
find: ./test/FM_and_Computer_Backups: No such file or directory

Which does not work as I think rm chokes up on spaces in the file names.

Then I tried the alternate form of -exec (which I learned about from

[webaddress] //content.hccfl.edu/pollock/unix/findcmd.htm)

which, combined with a shell feature allows us to solve the names with spaces problem and I entered:

root@127.0.0.1:/raid/data/incremental-backup# root@127.0.0.1:/raid/data/incremental-backup# find -mtime +90 \ -exec sh -c rm -rf {} \ +
-sh: root@127.0.0.1:/raid/data/incremental-backup#: No such file or directory
root@127.0.0.1:/raid/data/incremental-backup#

I've tried different variations on the above and can't seem to get close enough. This is the most complex line I've ever tried to enter in linux!

Thanks in advance for any help.


(By the way, this backup folder is In addition to an incremental backup I conduct to another medium so there's a lot of protection here)
 
Old 01-24-2010, 05:26 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
You are missing the 'path' argument. e.g.
Code:
 find . -exec #...
or more clearly
Code:
find /raid/data/incremental-backup -exec #...
 
Old 01-24-2010, 05:37 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I always use '{}' \; in the -exec argument.

Also, for files and empty directories you can use "-delete" instead of "-exec rm '{}' \;" or "-exec rmdir '{}' \;"

In the line you quoted, you could use:
find . -name test1 -ls

If someone were to delete files I saved at work that were 30 days old, I would be missing things I needed. Such as saved firmware for devices or documents I saved for reference, old backup catalogs, etc.
 
Old 01-24-2010, 07:40 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Indeed; you could check the last atime (access time) and/or have it mv the files to somewhere else for eg 30 days and see if anyone notices. If they do, you can just copy back the reqd files.
Really though, unless you know exactly what you can & cannot rm, setup a regular backup system so you can easily retrieve stuff.
 
Old 01-25-2010, 09:58 AM   #5
bobkatz
LQ Newbie
 
Registered: Jan 2010
Distribution: Whatever's in my Thecus 5500!
Posts: 22

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by chrism01 View Post
Indeed; you could check the last atime (access time) and/or have it mv the files to somewhere else for eg 30 days and see if anyone notices. If they do, you can just copy back the reqd files.
Really though, unless you know exactly what you can & cannot rm, setup a regular backup system so you can easily retrieve stuff.


Dear Chris et al: I'll check out your suggestions and see if it works! I wanted to use the "last accessed" attribute, but for some reason the flavor of Linux in my Thecus NAS does not accept the "last accessed" attribute for the Find command. I'd like to add a new Find command to the box but I fear without knowing what flavor of Linux is in there life could get very bad. So I intend to use the "last modified" and live with it. This backup folder is a spare anyway, meant for protecting from accidental deletions by users on my network which hopefully would be discovered within 30 days.
 
Old 01-26-2010, 09:01 AM   #6
bobkatz
LQ Newbie
 
Registered: Jan 2010
Distribution: Whatever's in my Thecus 5500!
Posts: 22

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by jschiwal View Post
I always use '{}' \; in the -exec argument.

Also, for files and empty directories you can use "-delete" instead of "-exec rm '{}' \;" or "-exec rmdir '{}' \;"

In the line you quoted, you could use:
find . -name test1 -ls

If someone were to delete files I saved at work that were 30 days old, I would be missing things I needed. Such as saved firmware for devices or documents I saved for reference, old backup catalogs, etc.
I am very flattered that the forum moderator has responded to my question! Thanks again.

Well, I'm back after trying EVERYTHING that everyone suggested so far with no success except an expansion of my learning process, which is also good. By the way, if I could use the -atime argument with this FIND command on my Thecus I would, but this FIND command does not accept many of the standard arguments. I may learn enough about the Linux in the Thecus to install a better FIND command if that's the problem.

Firstly one poster suggested that I forgot the files argument, which could be expressed by

find .

I am under the impression that the find command will operate on all directories within and below the current directory so the files argument is optional if you are located where you are. And that appears to be working because I assume that because simpler find commands without the attempts to delete do the job for me. Nevertheless, in my further experiments, the presence or absence of the dot after the find did not appear to help me. Again, I am using the -mtime argument instead of the -atime argument only because the FIND command in this Thecus does not seem to recognize it. The following is a transcript of my recent attempts, from a simple find to a more complex set of attempts. As you can see, in many cases the FIND command in my Thecus Linux simply does not recognize the arguments that you are suggesting to me. I'm hoping it's just a syntax error.

FIND WITH SIMPLE -MTIME ARGUMENT PRODUCES SUCCESSFUL RESULT (CHOOSING 90 DAYS FOR THE MOMENT):

root@127.0.0.1:/raid/data/incremental-backup# find -mtime +90
./Thecus Documents/temp bob's transfer folder/Hamilton order.bmp
./Thecus Documents/temp bob's transfer folder/Sequoia/Crash log.txt
./Thecus Documents/Projects DD/Godaddy-Email-Network-Web/Dreamhost newly formatted.txt
root@127.0.0.1:/raid/data/incremental-backup#

I'M SURE YOU WILL NOTICE THAT OF THE THREE FILES FOUND, THEY ALL HAVE SPACES IN THEIR FILE NAMES.

ATTEMPT USING THE -DELETE ARGUMENT, -DELETE IS APPARENTLY NOT RECOGNIZED:

root@127.0.0.1:/raid/data/incremental-backup# find . -mtime +90 -delete
BusyBox v1.1.0 (2009.03.26-08:00+0000) multi-call binary

Usage: find [PATH...] [EXPRESSION]

Search for files in a directory hierarchy. The default PATH is
the current directory; default EXPRESSION is '-print'

EXPRESSION may consist of:
-follow Dereference symbolic links
-name PATTERN File name (leading directories removed) matches PATTERN
-print Print (default and assumed)

-type X Filetype matches X (where X is one of: f,d,l,b,c,...)
-perm PERMS Permissions match any of (+NNN); all of (-NNN);
or exactly (NNN)
-mtime TIME Modified time is greater than (+N); less than (-N);
or exactly (N) days
-newer FILE Modified time is more recent than FILE's
-inum N File has inode number N
-exec CMD Execute CMD with all instances of {} replaced by the
files matching EXPRESSION



ATTEMPT USING THE -EXEC ARGUMENT PRODUCES ERRORS RELATED TO SPACES WITHIN THE CHOSEN FILE NAMES. (THIS LED ME TO MY OTHER POST WHICH ATTEMPTED THE ALTERNATE FORM OF EXEC TERMINATED WITH A + SIGN, AND SOME VERY COMPLICATED SYNTAX THAT I HAVE NOT CONQUERED):

root@127.0.0.1:/raid/data/incremental-backup# find . -mtime +90 -exec rm '{}' \;
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
rm: cannot remove `./Thecus': No such file or directory
rm: cannot remove `Documents/Projects': No such file or directory
rm: cannot remove `DD/Godaddy-Email-Network-Web/Dreamhost': No such file or directory
rm: cannot remove `newly': No such file or directory
rm: cannot remove `formatted.txt': No such file or directory
root@127.0.0.1:/raid/data/incremental-backup#


SO, THAT'S WHERE I AM RIGHT NOW. YES, I AM A NEWBIE AT THIS. :-(.
 
Old 01-26-2010, 09:40 AM   #7
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
What kind of Linux is this?

I assume that
Code:
find . -mtime +90 -exec ls -l '{}' \;
also fails?

What about
Code:
find . -mtime +90 -exec rm "{}" \;
?
 
Old 01-26-2010, 12:02 PM   #8
bobkatz
LQ Newbie
 
Registered: Jan 2010
Distribution: Whatever's in my Thecus 5500!
Posts: 22

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by AlucardZero View Post
What kind of Linux is this?

I assume that
Code:
find . -mtime +90 -exec ls -l '{}' \;
also fails?

What about
Code:
find . -mtime +90 -exec rm "{}" \;
?

Hi, AlucardZero. This is the "embedded" Linux in a Thecus N5500 or N5200 Pro NAS server. I have not been successful at figuring out which flavor of linux it is, exactly. In PART I below, when I run your command I get the syntax-related response to space characters in the file names. Then, just for fun (Part II below), I renamed all the files and folders it wants to find with underscores, and get different but fewer syntax-related errors. HOPE THIS HELPS!

(same results whether I use -exec ls or -exec rm)

PART I (Prior to renaming the affected folders and files)

root@127.0.0.1:/raid/data/incremental-backup# find . -mtime +90 -exec rm '{}' \;
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
rm: cannot remove `./Thecus': No such file or directory
rm: cannot remove `Documents/Projects': No such file or directory
rm: cannot remove `DD/Godaddy-Email-Network-Web/Dreamhost': No such file or directory
rm: cannot remove `newly': No such file or directory
rm: cannot remove `formatted.txt': No such file or directory
root@127.0.0.1:/raid/data/incremental-backup# find -mtime +90
./Thecus Documents/temp bob's transfer folder/Hamilton order.bmp
./Thecus Documents/temp bob's transfer folder/Sequoia/Crash log.txt
./Thecus Documents/Projects DD/Godaddy-Email-Network-Web/Dreamhost newly formatted.txt
root@127.0.0.1:/raid/data/incremental-backup# find . -mtime +90 -exec ls -l '{}' \;
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
ls: ./Thecus: No such file or directory
ls: Documents/Projects: No such file or directory
ls: DD/Godaddy-Email-Network-Web/Dreamhost: No such file or directory
ls: newly: No such file or directory
ls: formatted.txt: No such file or directory
root@127.0.0.1:/raid/data/incremental-backup#


PART II (after rename). All folder and file names in the tree that I think it's selecting now have underscores in place of spaces. This is the result:

root@127.0.0.1:/raid/data/incremental-backup# find . -mtime +90
./Thecus_Documents/temp_bob's_transfer_folder/Sequoia/Crash_log.txt
./Thecus_Documents/temp_bob's_transfer_folder/Hamilton_order.bmp
./Thecus_Documents/Projects_DD/Godaddy-Email-Network-Web/Dreamhost_newly_formatted.txt
./Thecus_Audio/Sequoia_Files/Sequoia_Latest_Beta/Templates/Bob's_send_return.VIP
root@127.0.0.1:/raid/data/incremental-backup# find . -mtime +90 -exec ls -l '{}' \;
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
-rwxrwxrwx 1 nobody nogroup 1816 Aug 17 01:45 ./Thecus_Documents/Projects_DD/Godaddy-Email-Network-Web/Dreamhost_newly_formatted.txt
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
root@127.0.0.1:/raid/data/incremental-backup#

Last edited by bobkatz; 01-26-2010 at 12:04 PM.
 
Old 01-26-2010, 12:38 PM   #9
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
I wanted to know if using "s instead of 's made a difference, please try
Code:
find . -mtime +90 -exec ls -l "{}" \;
 
Old 01-26-2010, 04:03 PM   #10
rndm_luser
LQ Newbie
 
Registered: Jan 2009
Posts: 8

Rep: Reputation: 1
You can try using xargs. If you have access to it, check the man page as the parameters differ amongst different systems.

gnu findutils xargs:
Code:
find </path/to/dir> -mtime +30 | xargs -l 1 rm -i
Depending on how silly the file names are (e.g., they contain newline characters), you might also need to use find's `-print0' and xargs' `-0' options.
 
Old 01-26-2010, 06:06 PM   #11
bobkatz
LQ Newbie
 
Registered: Jan 2010
Distribution: Whatever's in my Thecus 5500!
Posts: 22

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by AlucardZero View Post
I wanted to know if using "s instead of 's made a difference, please try
Code:
find . -mtime +90 -exec ls -l "{}" \;


I don't get the distinction, but I copied and pasted your line and got the identical errors as with find . -mtime +90 -exec rm "{}" \;


Sorry :-(
 
Old 01-26-2010, 06:19 PM   #12
bobkatz
LQ Newbie
 
Registered: Jan 2010
Distribution: Whatever's in my Thecus 5500!
Posts: 22

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by rndm_luser View Post
You can try using xargs. If you have access to it, check the man page as the parameters differ amongst different systems.

gnu findutils xargs:
Code:
find </path/to/dir> -mtime +30 | xargs -l 1 rm -i
Depending on how silly the file names are (e.g., they contain newline characters), you might also need to use find's `-print0' and xargs' `-0' options.

Earlier in this thread I tried xargs and I didn't have any luck. However, I just tweaked and pasted your line and it did not like the xargs syntax...

Unfortunately, man is not available on this "embedded" linux. Thecus allows adding modules and some people have written modules for it, and I could find out what flavor of Linux is in there if I beg on the Thecus forum :-). A lot of people a lot smarter than me have written modules for the Thecus including Perl. I wish I knew Perl, I might just write this Find thingy in Perl...

xargs did not like the -l (invalid option) so I took it out. Two other variations produce the intriguing error "unmatched single quote":

root@127.0.0.1:/raid/data/incremental-backup# find . -mtime +90 | xargs 1 rm -i
xargs: unmatched single quote


I don't know what that 1 after xargs was supposed to be doing so I tried simpler syntax to no avail:

root@127.0.0.1:/raid/data/incremental-backup# find . -mtime +90 | xargs rm -i
xargs: unmatched single quote
root@127.0.0.1:/raid/data/incremental-backup#


I'm sorry this is all not getting anywhere. You guys are being very helpful, I promise!
 
Old 01-29-2010, 10:29 AM   #13
bobkatz
LQ Newbie
 
Registered: Jan 2010
Distribution: Whatever's in my Thecus 5500!
Posts: 22

Original Poster
Rep: Reputation: 1
(bump)

No other ideas? Did I say something wrong? I'm really stuck at the syntax level here, guys... I'll plug in anything you suggest and see if it works.

Sorry,


Bob
 
  


Reply



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
find the number of files in a folder swift2008 Programming 5 10-25-2010 03:22 AM
Shell script to remove backup ~ files hallamigo Linux - General 3 09-13-2010 03:47 PM
How to use find and remove folders and files Drigo Linux - Newbie 1 05-31-2009 11:53 PM
Sorting all files of home folder modified after a specific date for backup fc6_user Linux - General 2 03-30-2007 02:24 AM
find /folder/* -exec ./file '{}' -nohomedir \; ??? utw-mephisto Linux - General 2 05-10-2005 08:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:05 AM.

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
Open Source Consulting | Domain Registration