Delete Directories and its files that begin with the same character string


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Delete Directories and its files that begin with the same character string
# 1  
Old 07-16-2019
Delete Directories and its files that begin with the same character string

I wrote a shell script program that supposed to delete any directories and its files that begin with the same character string . The program is designed to delete directories and its files that are one day old or less and only the directories that begin with the same character string. The problem is the program doesn't delete the directories. I don't know why the script isn't working. The OS is Linux redhats. Version 6 x.

Here is the shell script
Code:
#!/bin/bash -x

NUMBER_OF_DAYS=1
MOUNT_PATH=/saswork

echo "Script Started"
echo "Deleting the files greater than ${NUMBER_OF_DAYS}"
cd ${MOUNT_PATH}
chmod 755 SAS_work*
find ${MOUNT_PATH} -mtime +${NUMBER_OF_DAYS} -name "SAS_work*"
find ${MOUNT_PATH} -mtime +${NUMBER_OF_DAYS} -name "SAS_work*" | while read DIR_NAME
do
	echo "Removing the ${DIR_NAME}"
	rm -rf "${DIR_NAME}"
done
echo "Script Completed"

Below are sample of the directories that the program should delete if the directories are one day old or less.
Code:
drwx------  4 exxc_prxd    exxc_prxd            8192 Mar 15 14:49     SAS_work08480000328E_svxxsa60001mz34
drwx------  4 izjlin           D02012192                8192 Jul 14 08:57    SAS_work8BFF000059ED_svxxsi60001mz33
drwx------  4 jxwm67     D02012192                8192 Jul 16 19:32     SAS_work8CA50000264B_svxxsi60001mz33
drwx------  4 ixjiinz          D02012192                8192 Jul 15 11:17       SAS_work9417000072ED_svcxsi60001mz34
drwx------  3 tale4           D02017434               8192 Mar 13 14:30   SAS_workAA0C000044B7_svcxksi60001m34
drwx------  4 ixjwinz        D02012192                8192 Apr 12 09:23    SAS_workBA37000044E9_svxxxcksi60001mz34
drwx------  4 bdmknyex  D02017434             8192 Mar 15 09:41   SAS_workE77600003715_svcksi60001mz34


Last edited by Scrutinizer; 07-16-2019 at 01:11 PM.. Reason: quote tags -> code tags
# 2  
Old 07-16-2019
You cannot delete directories you do not have permission to delete (write permissions).

You are running your script as what user exactly?

Please post the exact command which includes the user prompt from your shell (in code tags).

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files to directories based on first 6 character

guys, i did create a script but its too long, though it function the same. # cat nightlyscan.sh #!/usr/ksh deyt=`date +"%Y-%m-%d"` for i in `ls -lrt|grep $deyt|awk '{print $9}'` do cp -f $i /S1/Sophos/logger/ done # but i did not paste it all. this is the desired. (9 Replies)
Discussion started by: kenshinhimura
9 Replies

2. Shell Programming and Scripting

How to delete directories and files with xargs?

Hello, i have an dynamical apache_cache that I need to clean all days (older tant one day) with an unix command : find /usr/IBM/HTTPServer/apache_cache/ -name '*' -mtime +1 -print0|xargs -0 rm -r -- but it didn't work. Could you explain me why. So I will put below all my script :... (13 Replies)
Discussion started by: steiner
13 Replies

3. Shell Programming and Scripting

Delete files in group of directories

OS: SUNOS 5.10 i386 Hello guys I wrote a shell script in bash shell to delete the files less than 30 days old. The following is the script. ======================================= #!/bin/bash for dirs in `/clu04/oracle/directory_list.lst` do find $dirs -type f -mtime -30 -exec rm {} \;... (3 Replies)
Discussion started by: zacsparrow
3 Replies

4. Shell Programming and Scripting

Script to go Into Directories and Find/Delete files

I have a task, I usually do manually, but with growing responsibilities I tend to forget to do this weekly, I want to write a script that automates this, but I cant seem to work it out in my head, I have the shell of it out, but need help, and you guys have helped me with EVERY problem I have... (5 Replies)
Discussion started by: gkelly1117
5 Replies

5. Shell Programming and Scripting

Delete first character from a string stored in a variable

Hallo! Example. #!/bin/bash BACKUP_DIR=/home/userx/backups/evolution echo $BACKUP_DIR # delete the first character from the string BACKUP_DIR=$(echo $BACKUP_DIR | cut -c 2-) echo $BACKUP_DIR It works. It does want I want, delete the first character from string in the... (11 Replies)
Discussion started by: linuxinho
11 Replies

6. Shell Programming and Scripting

delete last character in all occurences of string

Hello all, I have a file containing the following p1 q1 p2 q2 p1 p2 p3 pr1 pr2 pr1 pr2 pa1 pa2 I want to remove the last character from all strings that start with 'p' and end with '1'. In general, I do not know what is between the first part of the string and the last part of the string.... (4 Replies)
Discussion started by: bigfoot
4 Replies

7. Shell Programming and Scripting

Delete parts of a string of character in one given column of a tab delimited file

I would like to remove characters from column 7 so that from an input file looking like this: >HWI-EAS422_12:4:1:69:89 GGTTTAAATATTGCACAAAAGGTATAGAGCGT U0 1 0 0 ref_chr8.fa 6527777 F DD I get something like that in an output file: ... (13 Replies)
Discussion started by: matlavmac
13 Replies

8. Shell Programming and Scripting

Delete files from sub-directories over 7 days

Can any one please help me in deleting all the Files over 7 days from sub-directories A, B, C... Top-Directory Sub-Directory-A File-1 File-2 ..... File-n Sub-Directory-B File-1 File-2 ..... File-n Sub-Directory-C File-1 ... (1 Reply)
Discussion started by: sureshcisco
1 Replies

9. UNIX for Dummies Questions & Answers

delete pattern files in sub directories

Hello friends, I am compiling some set of SQL scripts in a set of sub directories demoed as below. After compiling log files are being created. Each and every time after compiling time I had to go subdir by subdir to delete the log files. I am sure there should be simple way to look for all log... (4 Replies)
Discussion started by: adurga
4 Replies

10. UNIX for Dummies Questions & Answers

Delete first 2 character from string

Hi ! All, I have to delete first 2 characters from string. How its possible? Like string value is "2001". I required output as "01" Plaese help me. Nitin (4 Replies)
Discussion started by: nitinshinde
4 Replies
Login or Register to Ask a Question