1

I am trying to hide one new file from a script that contains 'sudo rm -rf *' - I am wondering if I can do this without redoing the script/ if this is possible or not.

I have tried the following:

$ sudo chattr +i file.txt

and

$ sudo chattr +a file.txt

The later is closer to the functionality I am looking for i.e. +a

These work, however, they are not skipped during my script, rather immediately it errors Operation not permitted... and doesn't remove any others.

..My desired result would be for it to be completely hidden or skipped from sudo rm -rf *

  • I can't help but feel you're trying to solve the wrong problem. If it's a script, copy it or edit it directly to remove the rm -rf * component. – Chris Davies Mar 30 '20 at 16:53

1 Answers1

2

By default, *’s expansion will ignore files starting with a ., so if you name your file in that manner, sudo rm -rf * won’t delete it:

touch .myfile
rm *
ls -a

See Why do shell globs omit dot files by default? for details.

Stephen Kitt
  • 434,908
  • Thanks, that's what I thought, but it still deleting the file... Perhaps because it's in a directory; so then I tried ./Dir/.file.txt – Lieutenant Dan Mar 30 '20 at 17:19
  • I think the issue is I am wanting to do this in a non hidden, regular directory, that has other non-hidden files perhaps. i.e. /directory/.Dir/.file.txt <---------------------- is that possible – Lieutenant Dan Mar 30 '20 at 17:38
  • Yes, if it’s in a sub-directory, * will include Dir and all its contents will be deleted, including the hidden file. .Dir would work. – Stephen Kitt Mar 30 '20 at 17:44