1

For some reason, when I do a bash operation on a list of files (i.e. ls | cat -n | while read n f; do mv "$f" `printf "name-%02d.png" $n`; done, the "Hide extension" setting becomes enabled on all of them, and I have to manually uncheck that via "Get info". How can I avoid this, or fix it programmatically?

EDIT: The files I was operating on were native screenshots and thus already had their extensions hidden. My bad!

  • I'd use mv -n in a loop like this to keep it from overwriting any files that're already named in this format. Usually I'd also suggest mv -i (inquire what to do in case of a conflict), but that'd read from the file list instead of the terminal, and cause much confusion. – Gordon Davisson Oct 31 '17 at 22:25
3

Hmmm, that sounds really odd. I just ran your sample code on some test files and it didn't alter the Hide extension flag at all. Are you sure the files didn't already have that option checked before you ran your command? Have you tried creating a few fresh files, verifying manually that none have that option selected, and then running your command on them?

I can't help in terms of the "how to avoid this" part of your question, as I can't manage to recreate your problem on my end. But you can disable that setting programmatically. If you have Xcode installed, you can do the following:

# Disables "Hide extension" for all visible files in current directory
for file in *; do
    SetFile -a e "$file"
done

Note that if you ever want to reverse the process (i.e. turn on the setting), you can do so by changing the lowercase e to an uppercase E.

# Enables "Hide extension" for all visible files in current directory
for file in *; do
    SetFile -a E "$file"
done

If you don't have Xcode installed, see this answer on superuser for instructions on how to do the same thing using AppleScript.

By the way, you should try to avoid parsing the output of ls like you have done in your example. It can cause problems if any of the files you are processing contain a newline character in their filename. This is particularly important if you ever write a script that will be processing files which you have not created yourself (as you won't know how they are named). See Why you shouldn't parse the output of ls(1) for details.

  • I just realized I'm an idiot and because the input files were native screenshots, the extension was already hidden. Thank you for the thorough answer—I do have SetFile so I'll use this. Perfect! – T3db0t Nov 3 '17 at 16:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .