Find and removing the old files and zipping the files using shell script

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Find and removing the old files and zipping the files using shell script
# 1  
Old 03-28-2018
Find and removing the old files and zipping the files using shell script

Hi,

I am trying to removing the old files which were older than 10 days and same g zipping the files using the shell script.

script was return as follows.

Code:
find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f | xargs rm -f
find /cer_skyliv??/log -mtime +30 -type f | xargs rm -f
find /cer_skyliv??/log -mtime +10 -a -mtime -30 -type f | xargs gzip -fq
find /cer_skyliv??/work/bil -mtime +10 -type f | xargs gzip -fq

If i am executing using the shell script receiving the below error message as

Code:
./cleanup.sh
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'zip: invalid option -- '
Try `gzip --help' for more information.
'zip: invalid option -- '
Try `gzip --help' for more information.
'zip: invalid option -- '
Try `gzip --help' for more information.
'zip: invalid option -- '

Though if we are executing the command explicitly i.e running the command its working fine . It seems using the shell script its not running .

could you please assist here.
# 2  
Old 03-28-2018
I don't see the reason you use xargs here. Try modifying your code like:

Code:
find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f -exec rm -f {} \;

And the other lines accordingly. It should work.

I hope this helps.

bakunin
# 3  
Old 03-28-2018
i have tried use the above command using shell script. it is throwing the below error message .



Code:
 ./cleanup.sh
find: missing argument to `-exec'

command were added in shell script as follows.
Code:
vi cleanup.sh
find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f -exec rm -f {} \;
find /cer_skyliv??/log -mtime +30 -type f | xargs rm -f
find /cer_skyliv??/log -mtime +10 -a -mtime -30 -type f | xargs gzip -fq
find /cer_skyliv??/work/bil -mtime +10 -type f | xargs gzip -fq

# 4  
Old 03-28-2018
Quote:
Originally Posted by venkat918
i have tried use the above command using shell script. it is throwing the below error message .



Code:
 ./cleanup.sh
find: missing argument to `-exec'

hm, i get no such error on any of my systems. Try to execute the command outside the script by hand and look if it works there. If it does, something in the scripts environment is amiss.

Which OS do you use and which version? If it is Solaris you should set your PATH to /usr/xpg4/bin instead of /usr/bin. Also check the output of

Code:
which find
type find

Because maybe you use an alias, link or whatever instead of the common binary.

I hope this helps.

bakunin

Moderator's Comments:
Mod Comment I transfer this thread to the public boards. This place is for administrative issues.
# 5  
Old 03-28-2018
Please also show us the output you get from the command:
Code:
od -bc cleanup.sh

The diagnostic message you're getting could be caused by some unexpected character sequence between the -exec and the end of that line in your script. Although I don't get that diagnostic with the BSD-based find on macOS version 10.13.3, one might expect something like that if there is a null byte anywhere after the -exec or if there is a <carriage-return> just after the \;.
# 6  
Old 04-24-2018
Can any one help the issue. Tried with many ways getting the same error message.

Code:
 ./cleanup.sh
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'zip: invalid option -- '

Code:
 vi ./cleanup.sh
find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f -exec rm -f {} \;
find /cer_skyliv??/log -mtime +30 -type f | xargs rm -f
find /cer_skyliv??/log -mtime +10 -a -mtime -30 -type f | xargs gzip -fq
find /cer_skyliv??/work/bil -mtime +10 -type f | xargs gzip -fq

# 7  
Old 04-24-2018
xargs has pitfalls with special characters in file names. Risky with rm!
Replace them in find with -exec ... {} \;.
Code:
find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f -exec rm -f {} \;
find /cer_skyliv??/log -mtime +30 -type f -exec rm -f {} \;
find /cer_skyliv??/log -mtime +10 -type f -exec gzip -fq {} \;
find /cer_skyliv??/work/bil -mtime +10 -type f -exec gzip -fq {} \;

Perhaps you can replace each -exec ... {} \; by the faster -exec ... {} + .
The + collects the arguments just like xargs.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script Help..Renaming Quoted files removing the timestamp

Hi all, i am new to this forum, unix and shell scripting. I would really appreciate if you all can help me here.. I have files coming in the below format 'filename20513'13May06:03:45 filename are characters.. like 'ABDDUT20513'13May06:03:45 i need it to be renamed as... (17 Replies)
Discussion started by: khman
17 Replies

2. Shell Programming and Scripting

Shell script to find files in dir and updation

Hi, I have the shell script requirement mentioned below : List all java and c files or all files in directory and sub directories' in folder structure in current dir. then search for pattren1 in all files globally and replace with other string . And also check the date... (3 Replies)
Discussion started by: ammulu
3 Replies

3. Shell Programming and Scripting

Help me to find files in a shell script with any matching pattern

Hi friends.. I have many dirs in my working directory. Every dir have thousands of files (.jsp, .java, .xml..., etc). So I am working with an script to find every file recursively within those directories and subdirectories ending with .jsp or .java which contains inside of it, the the pattern... (3 Replies)
Discussion started by: hnux
3 Replies

4. Shell Programming and Scripting

Removing matching text from multiple files with a shell script

Hello all, I am in need of assistance in creating a script that will remove a specified block of text from multiple .htaccess files. (roughly 1000 files) I am attempting to help with a project to clean up a linux server that has a series of unwanted url rewrites in place, as well as some... (4 Replies)
Discussion started by: boxx
4 Replies

5. Shell Programming and Scripting

Shell Script - find, recursively, all files that are duplicated

Hi. I have a problem that i can't seem to resolve. I need to create a script that list all the files, that are found recursively, with the same name. For example if a file exists in more than one directory with the same name it list all the files that he founds with all the info. Could someone... (5 Replies)
Discussion started by: KitFisto
5 Replies

6. Shell Programming and Scripting

Shell script to find files

Hi Experts, I am trying to write a shell script that should 1. Find files with name (ab030.txt,Ab030.TXT,AB030.TXT,ab030.TXT,AB030.txt) 2. If any of the above found, rename it to AB030.TXT Thanks. (4 Replies)
Discussion started by: welldone
4 Replies

7. Shell Programming and Scripting

Find and Replace in multiple files (Shell script)

hi guys, Suppose you have 100 files in a folder and you want to replace all occurances of a word say "ABCD" in those files with "DCBA", how would you do it ??? jatin (13 Replies)
Discussion started by: jatins_s
13 Replies

8. UNIX for Dummies Questions & Answers

shell script to find files by date and size

Hi, I have a directory PRIVATE in which I have several directories and each of these have several files. Therefore, I need to find those files by size and date to back up those files in another directory. I don't know how to implement this shell script using ''find''. appreciate any... (1 Reply)
Discussion started by: dadadc
1 Replies

9. Filesystems, Disks and Memory

shell script to find zero byte files

I have a directory MYDIR In which i have several directories 1,2,3,4.... Now, In each of these directories i have several files a.dat, b.dat, c.dat, d.dat..... MYDIR ----1 ---------a.dat ---------b.dat ---------c.dat ---------d.dat ----2 ---------a.dat ---------b.dat ---------c.dat... (2 Replies)
Discussion started by: ramky79
2 Replies

10. Shell Programming and Scripting

shell script to find files

I have a directory which contains files with different kinds of extensions .everyday a file with .log gets added to it .i want to extract the file with .log extension which is created with todays date . thanks in advance (2 Replies)
Discussion started by: naren_samba2005
2 Replies
Login or Register to Ask a Question