Safely Remove Files with Special Chars


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Safely Remove Files with Special Chars
# 1  
Old 08-12-2018
Safely Remove Files with Special Chars

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:


Code:
-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.
# 2  
Old 08-12-2018
Safely means? And, special chars targets to non-[[:alnum:]]?
# 3  
Old 08-12-2018
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)

Code:
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.

Code:
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
# 4  
Old 08-12-2018
Safely means no wildcards which might delete other files in the directory.

Special char set in example in the post.
# 5  
Old 08-12-2018
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.
Code:
ls *[!-_.a-zA-Z0-9]*

matches file names that have a character that is not in the list.
An attempt with an RE follows:
Code:
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.
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 08-12-2018
Code:
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:/#

# 7  
Old 08-12-2018
Code:
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:/#

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to split data with a delimiter having chars and special chars

Hi Team, I have a file a1.txt with data as follows. dfjakjf...asdfkasj</EnableQuotedIDs><SQL><SelectStatement modified='1' type='string'><! The delimiter string: <SelectStatement modified='1' type='string'><! dlm="<SelectStatement modified='1' type='string'><! The above command is... (7 Replies)
Discussion started by: kmanivan82
7 Replies

2. Shell Programming and Scripting

If condition matching with special chars

Hi, I have file #cat drivers.txt fcs0 fcs1 vscsi1 vscsi2 In this i need to check the availabality of "fcs" or "vscsi" alone not vscsi0,fcs1 I tried with "if condition" but it is not working. cat drivers.txt| while read ADAP do echo "Checking for $ADAP" if ;then echo "FC... (9 Replies)
Discussion started by: ksgnathan
9 Replies

3. Shell Programming and Scripting

All strings within two special chars

I have a file with multiple lines. From each line I want to get all strings that starts with '+' and ends with '/'. Then I want the strings to be separated by ' + ' Example input: +$A$/NOUN+At/NSUFF_FEM_PL+K/CASE_INDEF_ACC Sample output: $A$ + At + K (20 Replies)
Discussion started by: Viernes
20 Replies

4. UNIX for Dummies Questions & Answers

Remove Unicode/special chars from XML

Hi, We are receiving an XML file in Unix which has some special characters between tags like '^' etc <Tag> 1e^O7f%<2304e.$d8f57e8^Bf-&e.^Zh7/327e^O7 </Tag> We need to remove all special characters like ^ ones and also any '&' or '<' or '>' being sent within the start and close tags i.e.... (6 Replies)
Discussion started by: dsrookie7
6 Replies

5. Solaris

How to remove soft link safely

Greetings, I need some help performing a system admin function that I have been tasked with. The request seems simple enough, but my feeling is that it might be more complicated than it seems. Here is what i've been tasked with: SunOS 5.10 Generic_142900-15 sun4u sparc SUNW,SPARC-Enterprise... (3 Replies)
Discussion started by: Harleyrci
3 Replies

6. UNIX for Dummies Questions & Answers

Files with special characters - how to remove

Hi, I have a directory that has a file which contained special characters in the filename. Can someone please advise how to remove the file, preferably with a rm -i ? Thanks in advance. Listing is as below: {oracle}> ls -1b bplog.bkup.001 bplog.bkup.002 bplog.bkup.003 bplog.bkup.004... (1 Reply)
Discussion started by: newbie_01
1 Replies

7. UNIX for Dummies Questions & Answers

Strings with Special chars in IF condition

I was trying to run a code to check if a fax number is empty or not. for that, I've written the following code which is throwing an error. #!/bin/ksh fax= "999-999-9999" if ; then fax_no="000-000-0000" else fax_no=$fax fi echo $fax_no And I get the... (7 Replies)
Discussion started by: hooaamai
7 Replies

8. Shell Programming and Scripting

special chars arrangement in code

here is my simple script to show process and owners except me: ps `-ef |grep xterm |grep -v aucar` | while read a1 a2 a3 a4 a5 a6 a7 a8 do echo KILL..\($a1\).. $a2 |more done how can I pass values from command "ps -ef |grep xterm|grep -v aucar" to ? because above command... (2 Replies)
Discussion started by: xramm
2 Replies

9. Shell Programming and Scripting

treating special chars

Hi, I need some advise on treating non printable chars over ascii value 126 Case 1 : On some fields in the text , I need to retiain then 'as-is' and load to a database.I understand it also depends on database codepage. but i just wanna know how do i ensure it do not change while loading... (1 Reply)
Discussion started by: braindrain
1 Replies

10. UNIX for Advanced & Expert Users

Supress special chars in vi

Hi, One of our application is producing log files. But if we open the log file in vi or less or view mode, it shows all the special characters in it. The 'cat' shows correctly but it shows only last page. If I do 'cat' <file_name> | more, then again it shows special characters. ... (1 Reply)
Discussion started by: divakarp
1 Replies
Login or Register to Ask a Question