Unable to remove file using 'rm'
In one of our severs (IBM AIX), we have a file in path /data/1002/ which we were not able to remove or delete using the 'rm' command. The error message we got is "rm: S1208001.002: A file or directory in the path name does not exist."
With the "-f" option, no error message was displayed, but the file is still there.
This file has a '0' byte size and when i use the command "touch S120801.002", i see two files with the same file name in that directory.
The directory listing is as below:
$ ls -l total 56
-rwxrwxrwx 1 oracle dba 0 Feb 09 11:57 S1208001.002
drwxrwxrwx 4 nobody dba 24576 Feb 09 13:36 backup
How do I remove this bogus fie?
Thanks.
UPDATE 1
after using the touch command, the directory listing is as below:
$ ls -l total 56
-rwxrwxrwx 1 oracle dba 0 Feb 09 11:57 S1208001.002
-rwxrwxrwx 1 oracle dba 77790 Feb 09 14:30 S1208001.002
drwxrwxrwx 4 nobody dba 24576 Feb 09 13:36 backup
Solution 1:
It sounds like this filename may contain a non-printable character. That would explain "touch" making a different file.
Try something like
ls -b
in the directory to see if that's the case?
Then you should be able to do something like:
rm -i S*2
and it should prompt you for the file even with the hidden character.
Alternately, you may be able to use find to do this...
find . -name S\*2 -exec /bin/rm -i {} \;
should prompt you for the files... I don't know if AIX 'find' syntax is unusual so this might not work, but the 'rm -i' part should let you abort the command if it's wrong.
Solution 2:
You could try by inode. I am not sure what special char you've got going on there, but this might be worth a try:
$ touch badfile^M
$ ls -il bad*
99 -rw-r--r-- 1 username group 0 Feb 09 04:39 badfile
$ find . -inum 99 -exec /bin/rm {} \;
$ ls -li bad*
ls: 0653-341 The file bad* does not exist.
Solution 3:
There's a space at the end of the file (or some other unprintable character). Try selecting both lines in your $ ls -l
output to see it. To remove it, you could try the completely safe find method:
dir=/path/to/your/directory
absolute_dir_path_x="$(readlink -fn -- "$dir"; echo x)"
absolute_dir_path="${absolute_dir_path_x%x}"
while IFS= read -rd $'\0' path
do
file_path="$(readlink -fn -- "$path"; echo x)"
file_path="${file_path%x}"
echo "START${file_path}END"
done < <( find "$absolute_dir_path" -type f -name '*S1208001*' -print0 )
Then you can probably just add a rm -- "${file_path}"
at the end of the while loop.