regex - Match backed up files (`*~`) -


I'm trying to match backup files in a script and then delete them. Backup files are in those that have a tilde ( ~ ):

  $ ls -al drwxr-xr-x 3 jwalton jwalton 4096 Feb 18 09: 00 Drwxr- Xr-x5 jwalton jwalton 4096 Feb 18 08:54 .. drwxr-xr-x5 flamingo flatten 40 9 Feb. 18 08:54 Test-Prose-RWXR-XR-X1 Jawlton Jubilee 664 February 18 09:00 Clean. My clean script has the following test, but it looks like I am doing something wrong. :  
  What is the correct test match for backed up files?   

This is your quote which is scouting it is looking for a file that is literally * ~ < / Code>. This works, but only when * ~ extends a file If:

  if [-e * ~]; then rm * ~ fi  

Why? Because shell wildcard first Expands so that if it is nothing, its equivalent is:

  if [-e]; then // false positive!  

If this corresponds to a file in the x.txt ~ , then it spreads:

  if [-e x.txt] ~]; then // OK  

If it matches more than one file, then it matches x.txt ~ y.txt ~ , then it spreads:

  if [-e x.txt ~ y.txt ~]; So // "[: x.txt ~: binary operator is expected"]  

It is worth knowing that in Unix filenames except / and ASCII 0 Can be any letter. You can:

$ touch "* ~" $ ls "* ~" * ~ $ rm "* ~"

and you end With files with weird names such as -r

- $ touch - -or # or imagine, your own c, java program $ ls -r $ rm - -r

However, If is not really necessary to prevent such a file or directory errors, and you can press those people: / P>

  rm -f * ~  

Comments