Removing Files
Sometime you may encounter problems in removing (rm) files in a UNIX environment. It could be due to the file name consists of non-printable characters (such as space, Ctrl-C, ...) or the file name starts with a minus ("-")
Lets tackle the easy one, that is file name with a "-". If you "rm" directory on the file, the "rm" command will complaint illegal option. What you can do is either provide a relative path "./" or full path to avoid having the first character to be the minus sign.
$ touch ./-abc $ rm -abc rm: illegal option -- a rm: illegal option -- b rm: illegal option -- c usage: rm [-fiRr] file ... $ rm ./-abcSo far so good. How about files with non-printable characters. Let's create some of these files. To introduce control character, type in Ctrl-V followed by whatever control character.
$ touch "abc def" $ touch "hij^Clkm" $ touch "rst^Mxyz" $ ls xyz def hijlkm rstIf you really want to see the actual character set of these file names, you can do an "octal dump" on the output of "ls -1" (ls minus one). i.e., one file name per line.
$ ls -1 | od -c 0000000 a b c d e f \n h i j 003 l k m \n 0000020 r s t \r x y z \n 0000030If you have problem deleting any of the file, what you can do is to find out the i-node number of that file and delete it using find
$ ls -li total 0 297552 -rw------- 1 chihung gdz 0 Oct 15 20:18 abc def 297551 -rw------- 1 chihung gdz 0 Oct 15 20:18 hijlkm xyz 297550 -rw------- 1 chihung gdz 0 Oct 15 20:18 rst $ find . -inum 297550 -exec rm -i {} \; xyz (yes/no)? yes $ ls -li total 0 297552 -rw------- 1 chihung gdz 0 Oct 15 20:18 abc def 297551 -rw------- 1 chihung gdz 0 Oct 15 20:18 hijlkm
Remember to use "rm -i" in "find", just in case.
Labels: shell script
0 Comments:
Post a Comment
<< Home