-1

I am trying to remove and replace quotation marks that are present in a file name. For example, I would like to change:

$ ls
abc"def"ghi"jkl"mno

to this

$ ls
abc:def:ghi:jkl:mno

In trying to solve this, I came across How to rename a bunch of files to eliminate quote marks, which is exactly what I want to do. However, it didn't work for my case. To figure out why, I tried creating a test file like this:

$ touch abba\"abba\"cde\"cde\"efef

With this file, the solutions I came across (such as mentioned above) worked. But why didn't it work for the first file?

One thing I discovered was that bash command completion sees them differently. If I type in

$ ls abb<tab>

bash will complete the filename like so:

$ abba\"abba\"cde\"cde\"efef

just as I created it. But for the original file, bash completion went like this:

$ ls abc<tab>

results in

$ abc"def"ghi"jkl"mno

So in the test case file, there is an escape of the quotation marks, and in the other case (the file I really want to rename), there is no escaping of the the quotation marks. I don't know how the original files were named.

Can anyone explain why bash sees these names differently, and how I would go about renaming my file?

ChrisF
  • 47
  • 3
  • 11
  • try , mv abba\"abba\"cde\"cde\"efef abc_def_ghi_jkl_mno , again using :(colon) in file name might not be a good idea – Jithin Scaria Dec 05 '17 at 20:14
  • 1
    `mv 'abc"def"ghi"jkl"mno' 'abc:def:ghi:jkl:mno'`? – Cyrus Dec 05 '17 at 20:17
  • 2
    The difference in completion may have to do with the fact that `ls abb` is completing an argument for the `ls` command, while `abc` is completing a command using the default completion (not associated with any specific command). – chepner Dec 05 '17 at 20:26
  • 1
    Are you sure those are regular ASCII quotes, and not fancy unicode quotes? They look very similar, but there's a big difference in how the shell handles them. – Gordon Davisson Dec 05 '17 at 21:09
  • What @GordonDavisson said ... I'll hazard the guess that it's indeed unicode (or some other non-ascii-characters) you're looking at ... – tink Dec 05 '17 at 21:14
  • @chepner You've identified a typo in my question, which I will fix. There should be an "ls" in the second case. Thanks! – ChrisF Dec 05 '17 at 21:26
  • ChrisF, what do you see if you pipe the output of ls through **od -xa**? – tink Dec 05 '17 at 21:26
  • 1
    `printf '%q\n' *` in the directory where the file lives will show each file name in an unambiguous form. – chepner Dec 05 '17 at 21:32
  • 1
    If this is a question about tab-completion (a strictly interactive facility), not about software development, there are [better places](https://unix.stackexchange.com) [for it](https://superuser.com/). – Charles Duffy Dec 05 '17 at 21:39
  • 1
    Gordon and @tink I did the octal dump. I think you may on to something. It looks like the quotation marks are associated with 80ef39 or 80ef30,...it varies. In any case, the quotation mark is taking more than 1 byte, whereas the other characters are only taking 1. In contrast, the "abba..." file is more what we would expect (ASCII)--1 byte per quote. Are you aware of a way to deal with unicode characters in filenames? – ChrisF Dec 05 '17 at 21:46
  • 1
    https://stackoverflow.com/questions/40712263/how-to-remove-special-characters-in-file-names - this deals with removing any characters other than a very limited set such as `[a-zA-Z0-9_-]`. – Benjamin W. Dec 05 '17 at 21:59
  • @ChrisF If there are only a few files, you can do them "by hand" using tab-completion to fill in the names. If there are a lot... It can be done, but will be more complex, and we need to know exactly what the relevant characters (and their encoding) are. The hex codes you list don't match what I'd expect; left double-quote in UTF-8 is `e2 80 9c`, and right double-quote is `e2 80 9d`. `39` and `30` can't occur in UTF-8 multibyte characters, since they're the single-byte encodings of "9" and "0" respectively. Note that `od -xa` swaps byte order, which can be very confusing. Try `od -a -t x1`. – Gordon Davisson Dec 06 '17 at 04:53
  • [How to escape single quotes within single quoted strings?](https://stackoverflow.com/q/1250079/608639), [When to wrap quotes around a shell variable?](https://stackoverflow.com/q/10067266/608639), [When is double-quoting necessary?](https://unix.stackexchange.com/q/68694/56041), [How to handle spaces in filenames using double quotes in a Bash script](https://stackoverflow.com/q/32767951/608639), [Listing files in date order with spaces in filenames](https://stackoverflow.com/q/4583801/608639), etc. – jww Aug 29 '18 at 12:47
  • 1
    @jww those are useful (and might be reason for a -1, if the answer was found there), but don't really address the main issue I had, which ended up being unicode characters being in the file name, as mentioned in one of the comments. The answer I selected prematurely was an issue I had, but not "the issue". – ChrisF Sep 05 '18 at 17:04

1 Answers1

0

Here is two ways to rename a file with "(quotation) mark,

option 1: With escape character \

mv abc\"cdf\"efg\"hij newFileName

option 2: By using '(single quote)

mv 'abc"cdf"efg"hij' newFileName

Note: using special charaters like :(colon) in file name might not be a good idea,


and regarding the auto completion, it usually fill the name with escape character, example

ls abc<tab> will complete the name to ls abc\"cdf\"efg\"hij

unless you start the name with a quote, example

ls 'abc<tab> will complete the name to ls 'abc"cdf"efg"hij'

Jithin Scaria
  • 1,271
  • 1
  • 15
  • 26
  • Thanks Jithin. I started to comment that it didn't work, but I must have had a typo. Option 2 worked when I did the auto-completion. Much appreciated. – ChrisF Dec 05 '17 at 21:21
  • @ChrisF - that solution (autocomplete) is feasible for 1 or 2 files interactively, it's not really an answer to your original question. – tink Dec 05 '17 at 21:25