Bash script - find command with delete and exec


 
Thread Tools Search this Thread
Top Forums Programming Bash script - find command with delete and exec
# 1  
Old 04-06-2017
Bash script - find command with delete and exec

hi all,

i have devised a script that starts in /restored/ and in there, there are a lot of sub folders called peoples names and in the sub folders are files/folders and it deletes the data in the sub folders BUT not the sub folder itself and it should then touch a file in all the sub folders warning people files/folders get deleted in x days time

it all works apart from the touch command, if i change the mindepth to 1 it works but when i change it to 2 it doesnt

below is my script -

Code:
cd /restored/
find -mindepth 2 -exec rm -rf {} \;
find -mindepth 2 -type d -exec touch WARNING_everything_in_here_will_get_removed_in_14_days_time.txt {} \;

can anyone please help me out, i would be very gratefull

many thanks,

rob
# 2  
Old 04-06-2017
I'm pretty sure some structuring of your text with e.g. punctuation would certainly enhance readability and understandability. And, posting WHAT "doesn't" could point helpers in the right direction. What be the difference when running it with mindepth 2 as compared to mindepth 1?
Would it help to put the {} in front of the file name to be touched?

Code:
find  -mindepth 2 -type d -exec echo touch WARNING_everything_in_here_will_get_removed_in_14_days_time.txt {} \;
touch WARNING_everything_in_here_will_get_removed_in_14_days_time.txt ./test/test1
touch WARNING_everything_in_here_will_get_removed_in_14_days_time.txt ./test/test1/test3
touch WARNING_everything_in_here_will_get_removed_in_14_days_time.txt ./test/test2

find  -mindepth 1 -type d -exec echo touch {}/WARNING_everything_in_here_will_get_removed_in_14_days_time.txt \;
touch ./test/WARNING_everything_in_here_will_get_removed_in_14_days_time.txt
touch ./test/test1/WARNING_everything_in_here_will_get_removed_in_14_days_time.txt
touch ./test/test1/test3/WARNING_everything_in_here_will_get_removed_in_14_days_time.txt
touch ./test/test2/WARNING_everything_in_here_will_get_removed_in_14_days_time.txt

You might want to make sure everybody knows when the 14 days will be over ...
# 3  
Old 04-06-2017
got the stupid syntax wrong using the find touch command

Code:
cd /restored/
find -mindepth 2 -mtime +13 -exec rm -rf {} \;
find -type d -exec touch {}/WARNING_everything_in_here_will_get_removed_in_14_days_time.txt \;

# 4  
Old 04-22-2017
As a safety measure ensure that the cd was successful.
Code:
if cd /restored/
then
  find -mindepth 2 -mtime +13 -exec rm -rf {} \;
  find -type d -exec touch {}/WARNING_everything_in_here_will_get_removed_in_14_days_time.txt \;
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Programming

Script to monitor progress of find/exec command

hi all, i want to monitor the progress of a find and exec command, this is the code i use - find . -type f -exec md5sum {} \; >> /md5sums/file.txt this command works and produces a text file with all the md5sums but while running it doesnt show the progress is there anyway i can do this... (4 Replies)
Discussion started by: robertkwild
4 Replies

3. Shell Programming and Scripting

Find and move command with exec

Hi all, I am trying to find files newer than a given file and them mv them to a new location. So I far I have: find . ! -newer <file_name> -exec ls -l {} \; and find . ! -newer <file_name> -exec mv /TEMP_LOCATION {} \; find is not liking this. Anyone know how to modify the last... (2 Replies)
Discussion started by: jonnyd
2 Replies

4. UNIX for Dummies Questions & Answers

What does the '\' in find -exec command

Hi, I have two scripts that remove files. One works fine and is coded find -name "syst*" -mtime +1 -exec rm {} \; The other is almost the same - only thing missing is the '\'. On that script though I keep getting: rm syst1202.file ? etc Does the \ make that difference or is it a... (3 Replies)
Discussion started by: Grueben
3 Replies

5. Shell Programming and Scripting

find command with -exec

Hi all, Please could someone help with the following command requirement. I basically need to find files NEWER than a given file and order the result on time. My attempt so far is as follows: find . -newer <file_name> -exec ls -lrt {} ;\ But I dont seem to get the right result... (12 Replies)
Discussion started by: jonnyd
12 Replies

6. UNIX for Dummies Questions & Answers

Bash script to delete file input on command line

1) I wrote a script and gave the desired permissions using "chmod 755 scriptname". Now if i edit the script file, why do i need to set the permission again? Didn't i set the permission attribute.. or if i edit the file, does the inode number of file changes? 2) I am running my unix on a server... (1 Reply)
Discussion started by: animesharma
1 Replies

7. Shell Programming and Scripting

find command with -exec

Hi People, I have a directory full of compressed files (.Z extention) In many of these files there is a string pattern (3800078163033) I want to find all file names which contain this string in their text. Regards, Abhishek (2 Replies)
Discussion started by: max29583
2 Replies

8. UNIX for Advanced & Expert Users

Find command with prune and exec

Hi, I'm using the following command to get a list of files on the system. find /releases -type f -exec ls -l > /home/sebarry/list.txt '{}' \; however, its searching a directory I don't want it to search so I know I have to use prune but I don't seem to be able to get prune and exec to work... (1 Reply)
Discussion started by: Sebarry
1 Replies

9. UNIX for Dummies Questions & Answers

find command exec error

Hi All, i am writing a shell script in korn shell which deletes all the files in a directory once in every 10DAYS. the directory has different format files. the script has something like this; cd /home/data/pavi echo "Please Enter the Number of Days to search for" read DAYS... (2 Replies)
Discussion started by: pavan_test
2 Replies
Login or Register to Ask a Question