0

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?

14
  • 1
    mv 'abc"def"ghi"jkl"mno' 'abc:def:ghi:jkl:mno'? Commented Dec 5, 2017 at 20:17
  • 2
    The difference in completion may have to do with the fact that ls abb<tab> is completing an argument for the ls command, while abc<tab> is completing a command using the default completion (not associated with any specific command). Commented Dec 5, 2017 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. Commented Dec 5, 2017 at 21:09
  • 1
    printf '%q\n' * in the directory where the file lives will show each file name in an unambiguous form. Commented Dec 5, 2017 at 21:32
  • 1
    If this is a question about tab-completion (a strictly interactive facility), not about software development, there are better places for it. Commented Dec 5, 2017 at 21:39

1 Answer 1

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'

Sign up to request clarification or add additional context in comments.

2 Comments

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 - that solution (autocomplete) is feasible for 1 or 2 files interactively, it's not really an answer to your original question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.