How to open file with special characters in the name

Asked
Active3 hr before
Viewed126 times

5 Answers

special
90%

In PHP, how can I open a file that has special characters in the name?, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Making statements based on opinion; back them up with references or personal experience.,The only thing I found was to rename the file then open. The problem is no PHP functions so I tried system commands. None worked. The ftp RNTO command will rename it. But it would be much better to filter out "special characters" when the file is saved.

Somebody on another forum had suggested using popen, so I tried this, and it works:

$logid = $_GET['logid'];
$logdate = str_replace("-", "/", $_GET['date'])."/";
$dst_file = escapeshellarg($uploads_dir.$logdate.$logid.'.log');
echo "<pre>\n";
$handle = popen("cat ".$dst_file, 'r');
while(!feof($handle)) {
    $line = fgets($handle);
    if (strlen($line) > 3) {
        echo $line;
    }
    ob_flush();
    flush();
}
pclose($handle);
echo "</pre>\n";
load more v
88%

Examples of file names that has special character and is not very common:,Example of Alpha-Numeric file names are:,Example of most common file names are:,We can eliminate this issue by passing the file name is single quote to solve the special characters present in the file.

Example of most common file names are:

abc.txt
avi.txt
debian.txt
   ...

Example of numeric file names are:

121. txt
3221. txt
674659. txt
   ...

Example of Alpha-Numeric file names are:

eg84235.txt
3 kf43nl2.txt
2323 ddw.txt
   ...

Examples of file names that has special character and is not very common:

#232.txt
# bkf.txt
#bjsd3469.txt
#121nkfd.txt
-2232.txt
-fbjdew.txt
-gi32kj.txt
--321.txt
--bk34.txt
...

Create a file that starts with a dash (-), say -abx.txt.

$ touch - abc.txt
Sample Output
touch: invalid option--'b'
Try 'touch --help'
for more information.

There are two ways to resolve this error as:

$ touch-- - abc.txt[Option #1]
$ touch ./-abc.txt		[Option # 2]

You may verify the file thus created by both the above ways by running commands ls or ls -l for long listing.

$ ls - l

total 0
   -
   rw - r--r--1 avi avi 0 Jun 8 11: 05 - abc.txt

To edit the above file you may do:

$ nano-- - abc.txt
or
$ nano. / -abc.txt

Note: You may replace nano with any other editor of your choice say vim as:

$ vim-- - abc.txt
or
$ vim. / -abc.txt

Similarly to move such file you have to do:

$ mv-- - abc.txt - a.txt
or
$ mv-- - a.txt - abc.txt

and to Delete this file, you have to do:

$ rm-- - abc.txt
or
$ rm. / -abc.txt

If you have lots of files in a folder the name of which contains dash, and you want to delete all of them at once, do as:

$ rm. / - *

2. The same rule as discussed above follows for the name of the folder having any number of hypen and their occurrence, except the fact that for deleting the folder you have to use ‘rm -rf‘ as:

$ rm - rf-- - abc
or
$ rm - rf. / -abc

create a file #abc.txt.

$ touch #abc.txt
Sample Output
touch: missing file operand
Try 'touch --help'
for more information.

To resolve such error, you may ask BASH not to interpret # as comment.

$ touch. / #abc.txt
or
$ touch '#abc.txt'

and verify the file just created as:

$ ls - l

total 0
   -
   rw - r--r--1 avi avi 0 Jun 8 12: 14 #abc.txt

Now create a file the name of which contains # anywhere except at the begging.

$ touch. / a #bc.txt
$ touch. / abc #.txt

or
$ touch 'a#bc.txt'
$ touch 'abc#.txt'

Run ‘ls -l‘ to verify it:

$ ls - l

total 0
   -
   rw - r--r--1 avi avi 0 Jun 8 12: 16 a #bc.txt -
   rw - r--r--1 avi avi 0 Jun 8 12: 16 abc #.txt

What happens when you create two files (say a and #bc) at once:

$ touch a.txt #bc.txt

Verify the file just created:

$ ls - l

total 0
   -
   rw - r--r--1 avi avi 0 Jun 8 12: 18 a.txt

Obvious from the above example it only created file ‘a‘ and file ‘#bc‘ has been ignored. To execute the above situation successfully we can do,

$ touch a.txt. / #bc.txt
or
$ touch a.txt '#bc.txt'

and verify it as:

$ ls - l

total 0
   -
   rw - r--r--1 avi avi 0 Jun 8 12: 20 a.txt -
   rw - r--r--1 avi avi 0 Jun 8 12: 20 #bc.txt

You can move the file as:

$ mv. / #bc.txt. / #cd.txt
or
$ mv '#bc.txt'
'#cd.txt'

Copy it as:

$ cp. / #cd.txt. / #de.txt
or
$ cp '#cd.txt'
'#de.txt'

You may edit it as using your choice of editor as:

$ vi. / #cd.txt
or
$ vi '#cd.txt'
$ nano. / #cd.txt
or
$ nano '#cd.txt'

And Delete it as:

$ rm. / #bc.txt
or
$ rm '#bc.txt'

To delete all the files that has hash (#) in the file name, you may use:

 # rm. / # *

Create a file having semi-colon in it.

$ touch;
abc.txt
Sample Output
touch: missing file operand
Try 'touch --help'
for more information.
bash: abc.txt: command not found

To resolve such error, tell BASH not to interpret semicolon as command separator, as:

$ touch. / ';abc.txt'
or
$ touch ';abc.txt'

Don’t requires anything extra, just do it normal way, as simple file name as shown below.

$ touch + 12. txt

You have to enclose file name in single quote, as we did in the case of semicolon. Rest of the things are straight forward..

$ touch '$12.txt'

You don’t need to do anything differently, treat it as normal file.

$ touch % 12. txt

Having Asterisk in file name don’t change anything and you can continue using it as normal file.

$ touch * 12. txt

Note: When you have to delete a file that starts with *, Never use following commands to delete such files.

$ rm *
   or
$ rm - rf *

Instead use,

$ rm.
/*.txt

Just Enclose the file name in single quote and rest of the things are same.

$ touch '!12.txt'

Nothing extra, treat a filename having At Sign as nonrmal file.

$ touch '@12.txt'

No extra attention required. Use a file having ^ in filename as normal file.

$ touch ^ 12. txt

Filename should be enclosed in single quotes and you are ready to go.

$ touch '&12.txt'

If the file name has Parenthesis, you need to enclose filename with single quotes.

$ touch '(12.txt)'

No Extra Care needed. Just treat it as just another file.

$ touch {
   12. txt
}

A file name having Chevrons must be enclosed in single quotes.

$ touch '<12.txt>'

Treat file name having Square Brackets as normal files and you need not take extra care of it.

$ touch[12. txt]

They are very common and don’t require anything extra. Just do what you would have done with a normal file.

$ touch _12.txt

Having an Equal-to sign do not change anything, you can use it as normal file.

$ touch = 12. txt

Backslash tells shell to ignore the next character. You have to enclose file name in single quote, as we did in the case of semicolon. Rest of the things are straight forward.

$ touch '.txt'

Again, an example where you don’t need to put any special attempt. A file name having Question mark can be treated in the most general way.

$ touch ? 12. txt

Creating, editing, renaming and deleting of such files are straight forward.

$ touch .12.txt

Note: In Linux you may have as many dots (.) as you need in a file name. Unlike other system dots in file name don’t means to separate name and extension. You can create a file having multiple dots as:

$ touch 1.2 .3 .4 .5 .6 .7 .8 .9 .10.txt

and check it as:

$ ls - l

total 0
   -
   rw - r--r--1 avi avi 0 Jun 8 14: 32 1.2 .3 .4 .5 .6 .7 .8 .9 .10.txt

You can have comma in a file name, as many as you want and you Don’t requires anything extra. Just do it normal way, as simple file name.

$ touch, 12. txt
or
$ touch, 12, .txt

You can have colon in a file name, as many as you want and you Don’t requires anything extra. Just do it normal way, as simple file name.

$ touch: 12. txt
or
$ touch: 12: .txt

To have quotes in file name, we have to use the rule of exchange. I.e, if you need to have single quote in file name, enclose the file name with double quotes and if you need to have double quote in file name, enclose it with single quote.

$ touch "15'.txt"

and

$ touch '15”.txt'

Some Editors in Linux like emacs create a backup file of the file being edited. The backup file has the name of the original file plus a tilde at the end of the file name. You can have a file that name of which includes tilde, at any location simply as:

$ touch~1 a.txt
or
$touch 2 b~.txt

It is not a good idea to have file name with spaces and if you have to distinct readable name, you should use, underscore or dash. However if you have to create such a file, you have to use backward slash which ignores the next character to it. To create above file we have to do it this way..

$ touch hi\ my\ name\ is\ avishek.txt

hi my name is avishek.txt
load more v
72%

The file name contains a "/".,There are five problems that occur when dealing with strange filenames: ,The characters in the name are specially handled by the shell. ,The file name contains non-printing characters.

  1. The characters in the name are specially handled by the shell.
  2. The file name contains an exclamation point or a single quote.
  3. The file starts with a "-".
  4. The file name contains non-printing characters.
  5. The file name contains a "/".

Note: In the following examples, solutions are only given for removing the files. The situations and solutions can also be applied to the mv (move) and cp (copy) commands. Use the same syntax for manipulating the characters, but apply them using the traditional conventions for the respective commands.

mv
  1. The characters in the name are specially handled by the shell.
  2. The file name contains an exclamation point or a single quote.
  3. The file starts with a "-".
  4. The file name contains non-printing characters.
  5. The file name contains a "/".

Note: In the following examples, solutions are only given for removing the files. The situations and solutions can also be applied to the mv (move) and cp (copy) commands. Use the same syntax for manipulating the characters, but apply them using the traditional conventions for the respective commands.

cp
load more v
65%

That command is ambiguous because spaces are normally used to separate arguments. cd does not know what you want to do but you have two possibilities to solve it:,Either you "mask" the spaces (and all other special characters) so that the terminal knows you mean the space as a character and not as a separator:,Spaces and several other special characters like \, *, ), ( and ? cause problems when I try to use them in the command line or scripts, e.g.:, Please stop posting half answers and dumb advice as comments

Either you "mask" the spaces (and all other special characters) so that the terminal knows you mean the space as a character and not as a separator:

cd Milano\, \Torino\\(Jan\) - Compressed

Or you put your folder name or path into quotes:

cd "Milano, Torino (Jan)-Compressed"
load more v
75%

\ - Reserved for use as a path separator and escape character,/ - Reserved for use as a path separator,* and ? - Reserved for use as wildcard characters,<, > and | - Reserved for use as command redirection operators

The following reserved characters:

< (less than) >
(greater than): (colon)
" (double quote) /
(forward slash)\(backslash) |
(vertical bar or pipe) ?
(question mark) *
(asterisk)
load more v

Other "special-undefined" queries related to "How to open file with special characters in the name"