Hello,
i am on 2.6.13-1.1526_FC4smp
i have directory named /home , and in there i have directory 2009_10_10
and two files : 2009_10_10.log and 2009_10_11.log
How to write shell script which will delete only file/s that don't have his/their 'parent' directory, so in this case , only 2009_10_11.log would be deleted ?
:o
Try this:
find /home -type f | awk -F[./] '$(NF-2)!=$(NF-1){system("ls " $(NF-1)"."$NF)}'
Replace the ls command with rm-f if the output is correct:
find /home -type f | awk -F[./] '$(NF-2)!=$(NF-1){system("rm -f " $(NF-1)"."$NF)}'
thanks , but it's not working : (
look , this is a copy-paste of what i've did ,
note that directories that are in the /u02/oradata/arch/flash/RING10/backupset , are not empty, so i guess some output of the ls command is
from the content of those directories:
[oracle@dell backupset]$ pwd
/u02/oradata/arch/flash/RING10/backupset
[oracle@dell backupset]$ ls -l
total 36
-rw-r--r-- 1 oracle oinstall 6407 Jan 29 07:15 2010_01_29.log
-rw-r--r-- 1 oracle oinstall 4102 Feb 2 07:15 2010_02_02.log
drwxr-x--- 2 oracle oinstall 4096 Feb 4 14:02 2010_02_03
-rw-r--r-- 1 oracle oinstall 3923 Feb 3 07:15 2010_02_03.log
drwxr-x--- 2 oracle oinstall 4096 Feb 4 14:01 2010_02_04
-rw-r--r-- 1 oracle oinstall 4667 Feb 4 14:02 2010_02_04.log
[oracle@dell backupset]$
[oracle@dell backupset]$ find /u02/oradata/arch/flash/RING10/backupset -type f | awk -F[./] '$(NF-2)!=$(NF-1){system("ls " $(NF-1)"."$NF)}'
2010_02_02.log
2010_01_29.log
ls: o1_mf_annnn_TAG20100204T071355_5pnsf51k_.bkp: No such file or directory
ls: o1_mf_annnn_TAG20100204T140154_5pokb3k8_.bkp: No such file or directory
ls: o1_mf_nnndf_TAG20100204T070029_5pnrmxpp_.bkp: No such file or directory
ls: o1_mf_annnn_TAG20100204T070012_5pnrmfvd_.bkp: No such file or directory
ls: o1_mf_annnn_TAG20100204T134643_5pojfo2k_.bkp: No such file or directory
ls: o1_mf_nnndf_TAG20100204T134648_5pojfrlt_.bkp: No such file or directory
2010_02_04.log
2010_02_03.log
ls: o1_mf_annnn_TAG20100203T071451_5pl52w9m_.bkp: No such file or directory
ls: o1_mf_nnndf_TAG20100203T070044_5pl48fq1_.bkp: No such file or directory
[oracle@dell backupset]$
In this case, i would need to have deleted 2010_02_02.log and 2010_01_29.log , as they don't have their directory.
I've missed something...try this one (not tested):
ls -l | sort -r |
awk -F[./] 'NR==1{next} /^d/{a[$NF];next} !($(NF-1) in a){system("ls " $(NF-1)"."$NF)}'
Replace the ls command with rm-f if the output is correct.
i am getting some error ...
[oracle@dell backupset]$ ls -l | sort -r | awk -F[./] 'NR==1{next} /^d/{a[$NF];next} !($(NF-1) in a){system("ls " $(NF-1)"."$NF)'
awk: cmd. line:1: NR==1{next} /^d/{a[$NF];next} !($(NF-1) in a){system("ls " $(NF-1)"."$NF)
awk: cmd. line:1: ^ unexpected newline or end of string
Sorry, I forgot a closing brace. I've edited the code.
This will work also (obviously not as short written as Franklin52's solution):
#!/bin/bash
#TARGET dir should end with /, as in /home/user/
TARGET="/your/dir/"
TEMP="/path/to/a/temporary/file"
ls -l $TARGET | grep ^d | awk '{print $8}' > $TEMP
ls $TARGET | grep -v -f $TEMP | awk -v TARGET="$TARGET" '{print TARGET$0}' | xargs ls -lrta
thanks , but
i am receving again something i think i am not suppose to.
[oracle@dell backupset]$ ls -l | sort -r | awk -F[./] 'NR==1{next} /^d/{a[$NF];next} !($(NF-1) in a){system("ls " $(NF-1)"."$NF)}'
ls: invalid line width: -r--r--
ls: invalid line width: -r--r--
ls: invalid line width: -r--r--
ls: invalid line width: -r--r--
ls: invalid line width: -r--r--
[oracle@dell backupset]$
---------- Post updated at 12:51 PM ---------- Previous update was at 12:49 PM ----------
/code]
---------- Post updated at 12:49 PM ---------- Previous update was at 12:44 PM ----------