-   Linux - Newbie (
-   -   Anything with a '!' as part of the name, "breaks" the terminal (how do I linux?) (

Master_CAPS 03-13-2012 05:29 PM

Anything with a '!' as part of the name, "breaks" the terminal (how do I linux?)
In windows my webserver had a lot of files like (!photo, !uploads etc...) the ! infront of of the file names kept it at the top (my files in windows are listed in alphabetical order and ! is over numbers and letters)

So I cant do a "cd !photos" or "chmod !photos" or anything because I aassume the "!" in linux terminal acts as an operator or argument of some sort... So is there some escape character to use with my commands without renamning everything?

here is what I get (some things ommited):

Satellite /var/www $ dir
bg.png                      index.html        photos.php    style.ie7.css
Files.html              index.html~        script.js      test.php~
images                      Iphonepage.html        style.css      uploader.php
index-for-iphone.html !photos                style.ie6.css  uploads
Satellite /var/www $ cd !photos
bash: !photos: event not found
Satellite /var/www $

Tinkster 03-13-2012 05:36 PM

Try cd \!Photos

! to bash is a special character, and need to be escaped.

Master_CAPS 03-13-2012 08:13 PM


Originally Posted by Tinkster (Post 4626079)
Try cd \!Photos

! to bash is a special character, and need to be escaped.

Works great, but this also works too.

cd '!photos'

David the H. 03-14-2012 11:01 AM

In interactive terminals in bash "!" is used for history substitution. When the line is executed it is replaced by the contents of the previous command.

To work around it, you need to hard-escape it with either a backslash or single quotes. Double quotes still allow it to expand, however.

You can also disable history expansion with "set +H". This might be the most convenient option if you don't ever use it. Since the modern shell now provides other ways to access history (starting with the up arrow), most modern users don't need it or even bother to learn how it works (I certainly never did). Add the line to your bashrc or other start-up file to make it permanent.

Check out the HISTORY and QUOTING sections of the bash man page for more details. There are also a handful of shell options/environment variables that affect how history expansion behaves.

chrism01 03-15-2012 01:09 AM

Also, at the higher level, try using '_' instead of '!'.

DavidMcCann 03-15-2012 11:40 AM

More generally, the rules for Unix filenames are
1. Start with a letter, number, or under-score "_"
2. Only include letters, numbers, under-scores, hyphens, or full stops. (No spaces!)

The GUI will cope with most things, but CLI tools get confused by exotic names.

David the H. 03-15-2012 01:34 PM

To be fair, there aren't really any hard-and-fast "rules" for file naming; just recommended common practices.

Posix-based filesystems support everything except the null byte (ascii 000) and the forward slash (ascii 057); the latter being the directory separator. So there's no technical reason for avoiding any character.

But as this topic demonstrates, most shells treat various non-alphanumeric characters as special, so it's usually a good idea to limit your filenames to basic ascii, and generally avoid all whitespace and punctuation marks outside of the period, underscore, and dash. To keep you from having to worry about escaping everything all the time.

Another reason to avoid unusual names is that it can sometimes be difficult to input non-keyboard-supported characters. Have you ever tried manipulating filenames in Japanese, in a non-X console, meaning there's no IME access, for example? It's certainly not fun, I can tell you. That's why I always make sure my filenames stick to the above pattern. (Or partially, at least. As long as the filename contains at least one unique ascii substring, you can generally still use shell globbing to match it.)

All times are GMT -5. The time now is 11:30 PM.