Skip to content

Instantly share code, notes, and snippets.

@senventise
Created February 1, 2023 07:52
Show Gist options
  • Select an option

  • Save senventise/0ea417b29368988562ecd33467ec4511 to your computer and use it in GitHub Desktop.

Select an option

Save senventise/0ea417b29368988562ecd33467ec4511 to your computer and use it in GitHub Desktop.
Fix wrong extension with `file`
#!/usr/bin/env fish
argparse --name=test "d/dryrun" "p/path=" -- $argv
if test $_flag_d
echo "Dry running..."
end
if not test -d $_flag_p
echo "Path not exists: $_flag_p"
return 1
end
for filename in "$_flag_p/"*
# check file exists
if not test -f $filename
return
end
set exts (file --brief --extension $filename)
set ext_list (string split "/" $exts)
# validate extension
if test $exts != "???" ; and not contains (string sub -s 2 (path extension $filename)) $ext_list
set new_name (path change-extension $ext_list[1] $filename)
echo $filename "=>" $new_name
if not test $_flag_d
mv "$filename" "$new_name"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment