Remove file called "-X"??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove file called "-X"??
# 1  
Old 03-14-2011
Question Remove file called "-X"??

Hi all,

So I have found a file literally called "-X" in a folder, that is preventing me from using any meaningful commands in that folder because, as I understand it, at command runtime it will convert any wildcard characters into matching filenames, thus a command such as:
Code:
ls *

Ends up actually doing:
Code:
ls -X

Because the -X file is the first one in the folder...which is not a valid argument for ls. And thats what SunOS tells me. This happens with every command I've tried so far. The nature of this problem has also made it difficult for me to google for answers, so I'm hoping someone here can help me with an explanation or a way to rename this file! Here's some examples of my troubles:

Code:
root@qmgt2 # ls -al
total 3695918
-rw-r-----   1 root     other    1771857920 Nov 14  2006 -X
dr-xr-xr-x  22 bin      bin          512 Mar 29  2010 .
drwxr-xr-x  48 root     root        2048 Feb 24 12:41 ..
drwxr-x---   2 root     other        512 Mar  9  2005 APP_BACKUPS
drwxr-xr-x   6 root     other        512 Sep 22  2004 ORCLfmap
dr-xr-xr-x  37 bin      bin         1024 Mar 29  2010 OV
drwxr-xr-x   3 root     bin          512 Sep 20  2004 SUNWits
drwxr-xr-x   5 root     sys          512 Sep 20  2004 SUNWrtvc
drwxr-xr-x   3 root     other        512 Sep 24  2004 VRTSvcs
drwxr-xr-x   3 netcool  ncoadmin     512 May 26  2009 alu
drwxr-x---   3 root     other        512 Apr  4  2005 backups
drwxrwxrwx   2 root     other        512 Sep 22  2004 bin
drwxr-x---   3 root     other        512 Mar 29  2010 cfgdwn
drwxr-xr-x   5 root     sys          512 Jun 12  2008 dcelocal
-rw-r-----   1 root     other         25 Nov 15  2006 exclude
-rw-r-----   1 root     other       1243 Nov 14  2006 exclude.txt.new
-rw-r-----   1 root     other       2809 Nov 15  2006 exclude.txt.old
drwx------   2 root     root        8192 Sep 20  2004 lost+found
drwxr-x---   3 root     other        512 Feb 23  2005 mnams
-rw-r-----   1 root     other    39992320 Sep 17  2007 mnams.tar
drwxr-x---   3 root     other        512 Mar  2  2005 msg
drwxr-xr-x   4 root     other        512 Nov 10  2009 netcool
-rw-r-----   1 root     other    79429147 May 28  2007 netcool.tar.gz
drwxr-xr-x  12 netcool  ncoadmin     512 May 26  2009 netcoolnsa
drwxr-x---   2 root     other        512 Sep  7  2005 oracle
dr-xr-xr-x  12 bin      bin          512 Jun 12  2008 perf
drwxr-xr-x   3 root     bin          512 May 27  2005 sfw
drwxr-x---   2 root     other      28672 Apr  4  2010 tmp
root@qmgt2 # ls *
ls: illegal option -- X
usage: ls -1RaAdCxmnlhogrtucpFbqisfL@ [files]
root@qmgt2 # du -sk * | sort -n
du: illegal option -- X
usage: du [-a] [-d] [-h|-k] [-r] [-o|-s] [-L] [file ...]
root@qmgt2 # tail -X
usage: tail [+/-[n][lbc][f]] [file]
       tail [+/-[n][l][r|f]] [file]
root@qmgt2 # tail "-X"
usage: tail [+/-[n][lbc][f]] [file]
       tail [+/-[n][l][r|f]] [file]
root@qmgt2 # mv -X new.dat
mv: illegal option -- X
mv: Insufficient arguments (1)
Usage: mv [-f] [-i] f1 f2
       mv [-f] [-i] f1 ... fn d1
       mv [-f] [-i] d1 d2
root@qmgt2 # mv "-X" new.dat
mv: illegal option -- X
mv: Insufficient arguments (1)
Usage: mv [-f] [-i] f1 f2
       mv [-f] [-i] f1 ... fn d1
       mv [-f] [-i] d1 d2
root@qmgt2 # mv '-X' new.dat
mv: illegal option -- X
mv: Insufficient arguments (1)
Usage: mv [-f] [-i] f1 f2
       mv [-f] [-i] f1 ... fn d1
       mv [-f] [-i] d1 d2
root@qmgt2 # mv *X new.dat
mv: illegal option -- X
mv: Insufficient arguments (1)
Usage: mv [-f] [-i] f1 f2
       mv [-f] [-i] f1 ... fn d1
       mv [-f] [-i] d1 d2

I've never encountered this before. Something tells me that there is something special about this -X "file"... besides it being a royal PITA. Smilie
# 2  
Old 03-14-2011
It has to do with how your shell expands the command.

Try this, assuming you're in the same directory as the file:
Code:
# mv ./-X ./dash-X

Then you can examine or remove the 'dash-X' file.
This User Gave Thanks to LivinFree For This Post:
# 3  
Old 03-14-2011
sweet, that did the trick. Although I still had issues viewing the file because of the - inside the name, so I just renamed it again to new.dat using your syntax above, and now tail doesn't complain.

Thanks for this!Smilie
# 4  
Old 03-14-2011
In general, when dealing with arguments which look like options, you can use a double dash to signal the end of options:

Code:
command -o1 -o2 -- -X

In that case, -o1 and -o2 are two option-arguments to command. -- signals the end of options. Since -X appears after --, it is not considered an option-argument even though it begins with a dash.

One way to create that file and then remove it:
Code:
touch -- -X
rm -- -X

Regards,
Alister
# 5  
Old 03-15-2011
alister makes a good point - many utilities do have such an option. Not all do, though, which is why I suggested the course I did - it's fairly portable.

This subject is a common one, though. I've even heard of people doing this on purpose to act as a safety mechanism, although I wouldn't ever do so myself:
Code:
$ mkdir garbage && cd garbage
$ touch file{1..5}
$ touch ./-i
$ rm *
rm: remove regular empty file `file1'? n
rm: remove regular empty file `file2'? n
rm: remove regular empty file `file3'? n
rm: remove regular empty file `file4'? n
rm: remove regular empty file `file5'? n
$

The "*" is expanded by my shell as "-i file1 file2 file3 file4 file5", so rm takes the -i as an option to prompt interactively just in case you do something silly like "rm *". It's bad practice, in my mind anyway, to rely on such, but to each their own.

Check your manpages to see if your utilities accept "--" to stop processing arguments. If so, it's a good habit to get in using, especially when scripting.
# 6  
Old 03-15-2011
TRY

Before Remove/Renaming you have go to that path where the file "-X" is located.
otherwise replace full path instead of "./"

For Remove:
------------
Code:
rm ./-X

For Rename:
------------
Code:
mv ./-X newfile

# 7  
Old 03-15-2011
the "--" thing works most of the time for me too.
alternatively, for really weird filenames, action through "inode" does the trick.

Code:
$ touch -- --d
$ ls -ilrt | grep -- --d
 90779 -rw-r--r--   1 user1   grp1         0 Mar 15 14:10 --d
$ find . -inum "90779"
./--d
$ find . -inum "90779" -exec mv {} recovered \;
$ ls -li recovered
 90779 -rw-r--r--   1 user1   grp1         0 Mar 15 14:10 recovered
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Linux

Read only access for Group called "Developers" to all folders on the Centos 6.6

Linux Gurus, I need to provide Read only access for particular group of users, they should have Read only access to entire server except their Home directory. I tried using setfacl that's not helping. Can you please suggest is there any other alternate way to address this request. Your help is... (5 Replies)
Discussion started by: shekar777
5 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Advanced & Expert Users

Rename file called "-X"??

Hi all, So I have found a file literally called "-X" in a folder, that is preventing me from using any meaningful commands in that folder because, as I understand it, at command runtime it will convert any wildcard characters into matching filenames, thus a command such as: ls *Ends up... (1 Reply)
Discussion started by: dan-e
1 Replies

7. UNIX for Advanced & Expert Users

How to remove a directory called "--"

I need to remove a directory called "--"". But it seems "--" linked to /export/home/usr1, which I do not want to delete. How to rm "--" safely? The OS is Solaris 9. Thanks. (6 Replies)
Discussion started by: fld2007
6 Replies

8. UNIX for Advanced & Expert Users

All alias in .profile lost when "script" command is called

Hi, I was trying to call "script <an ip add>" command from .profile file to log everything whenever anyone logs in to this user. I did the following at the end of .profile. 1) Extracted the IP address who logged in 2) Called script < ip add> . The problem I am facing is all, aliases etc. written... (3 Replies)
Discussion started by: amicon007
3 Replies

9. Windows & DOS: Issues & Discussions

Samba (SMB) client fails: "Called name not present"

Hi, I issue smbclient on a Linux REd hat server : smbclient -L ***.16.0.42 -U domaine/Administrator Password: Domain= OS= Server= Domain= OS= Server= Sharename Type Comment --------- ---- ------- IPC$ IPC IPC distant ... (0 Replies)
Discussion started by: big123456
0 Replies

10. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies
Login or Register to Ask a Question