Sunday, January 31, 2010

Unix - delete file with hyphen at beginning


Accidentally one of my script created a file named '-1264924755.done' (i.e. filename starting with a hyphen).

When I tried to remove this file:

$ rm -1264924755.done

rm: invalid option -- 1
Try `rm ./-1264924755.done' to remove the file `-1264924755.done'.
Try `rm --help' for more information.

This is because the command line parser treats the filename as a command line switch (because of the hyphen). So commands like mv, rm etc treats the file name itself as a parameter(option) to the command.

The ways to delete such files:

1) Referencing the filename to the command via its path location (so that the special character is not the first character after the white space)


$ rm ./-1264924755.done

or

$ rm ~/work/temptest/-1264924755.done

2) A special way:

$ rm -- -1264924755.done

Related posts:

- Find last modified directory in UNIX
- Grep and print control character characters in UNIX
- Find process running time in UNIX
- Bash - save command in history without executing it
- Display file permissions in octal in UNIX

1 comment:

dammuozz said...

Thank you! the cp ../folder/-file.ext worked for me!! :)

© Jadu Saikia www.UNIXCL.com