Neo
August 12, 2018, 3:54am
1
Hey Guys,
I'm swamped writing code for the forums:
Could someone write a script or command line to safely delete files with special chars in filenames from a directory:
Example:
-rw-r--r-- 1 root root 148 Apr 30 23:00 ?xA??
-rw-r--r-- 1 root root 148 May 6 23:00 ??xA??
-rw-r--r-- 1 root root 148 Apr 30 23:00 ??yA??
-rw-r--r-- 1 root root 148 Jan 6 2018 ??>Ym?
-rw-r--r-- 1 root root 148 Mar 11 06:27 ??~Ym?
-rw-r--r-- 1 root root 148 Apr 1 06:44 ?z?,[?
-rw-r--r-- 1 root root 148 Apr 22 06:41 ?zPA??
-rw-r--r-- 1 root root 148 Jan 6 2018 \Ym?
-rw-r--r-- 1 root root 148 Jan 6 2018 cYm?
-rw-r--r-- 1 root root 148 Jan 6 2018 z0??
Thanks.
RudiC
August 12, 2018, 5:08am
2
Safely means? And, special chars targets to non-[[:alnum:]]?
Hello Neo,
Considering that ?
character is present in your all junk files then following may help you.(Using inode
for deleting it safely and removing the probability to deleting anything else since special characters have their own meaning too)
ls -lhtri | awk -v s1="\\" '$NF~/\?/{print "Filename which will be deleting is "$NF;system("echo find -type f -inum " $1 " -exec rm -f {} " s1 ";")}'
Above is for safety purposes, once you see output is coming correct files, you could delete them then with following.
ls -lhtri | awk -v s1="\\" '$NF~/\?/{print "Filename which will be deleting is "$NF;system("find -type f -inum " $1 " -exec rm -f {} " s1 ";")}'
Thanks,
R. Singh
Neo
August 12, 2018, 5:14am
4
Safely means no wildcards which might delete other files in the directory.
Special char set in example in the post.
A shell glob has [ ] that is a character set similar to the one in a RE.
A first character ! negates the following characters and ranges.
ls *[!-_.a-zA-Z0-9]*
matches file names that have a character that is not in the list.
An attempt with an RE follows:
ls | grep '[^[:alnum:][:punct:]]'
matches file names that have a character that is not in the given character classes.
Also [:print:]
might be worth to try.
1 Like
Neo
August 12, 2018, 12:08pm
6
root@www:/# ls *[!-_.a-zA-Z0-9]*
??a??? 2{0?? ?f??? 0-jA?? @?vA?? p?;Ym? ???@?? ??yA?? ??}0?? ?$HYm? ?U#`V? ??>Ym? ??mA?? ?zPA?? ??mA?? ??????
?#???? ?xA?? ?U0?? @8nYm? pGWA?? t???`??]? ?.SYm? ??~Ym? ??fYm? ?%NYm? ?z?,[? ??xA?? ?BJYm? ??z0?? ??cYm? ??RA??
??fYm? ?u??? ?>,[? @?d0?? pZr??? ?PRYm? ?.TA?? ?^TA?? ??D,[? ?2?Ym? ?~SYm? ??vYm? ?l???? ??\Ym? ?gTYm? ??QYm?
?L??? ?pYm? 0?4A?? @????? p?R0?? ??vYm? ?o5Ym? ?|E`V? ?"60?? ?PNYm? ??U??? ?????? ?afYm? ??cYm? ?&;`V? ??????
lost+found:
root@www:/#
Neo
August 12, 2018, 12:09pm
7
root@www:/# ls | grep '[^[:alnum:][:punct:]]'
a??
#???
?fYm
L??
2{0?
?xA
?u??
?pYm
?f??
?U0?
?>,[
4A
0-jA
@8nYm
@?d0?
@????
@?vA
pGWA
pZr??
p?R0?
p?;Ym
t???`
?]
?PRYm
??vYm
?? @
?.SYm
?.TA
?o5Ym
??yA
?~Ym
?^TA
?|E`V
??}0?
??fYm
?D,[
?"60?
?$HYm
?%NYm
?2Ym
?PNYm
?U#`V
?z,[
?~SYm
??U??
??>Ym
??xA
??vYm
????
? mA
?BJYm
?l???
?afYm
?zPA
??z0?
??\Ym
??cYm
?mA
?cYm
?gTYm
?&;`V
?????
??RA
??QYm
?????
root@www:/#
Neo
August 12, 2018, 12:11pm
8
root@www:/# ls *[!-_.a-zA-Z0-9]* |wc -l
67
root@www:/# ls | grep '[^[:alnum:][:punct:]]' | wc -l
65
Neo
August 12, 2018, 12:51pm
9
Added the +
after doing a test ls
run and changed this to:
rm *[!-_.+a-zA-Z0-9]*
Worked great!
Thanks!!!