-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new feature to base.plugin.bash #2257
base: master
Are you sure you want to change the base?
Conversation
2 New aliases: d: go to Downloads directory from anywhere rmrf: force delete any file or directory recursively
Update general.aliases.bash, 2 new aliases
New function labeled `rex()` servers a single purpose. Replace the file extensions of multiple file at once. File extensions usually does not quite matter in bash, but there is a few cases when one might needs to and this function simplifies the process from multiple commands into one.
@@ -55,6 +56,7 @@ alias cd..='cd ..' # Common misspelling for going up one directory | |||
alias ...='cd ../..' # Go up two directories | |||
alias ....='cd ../../..' # Go up three directories | |||
alias -- -='cd -' # Go back | |||
alias d='cd /home/$USER/Downloads' # Go to the Downloads directory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit too short and already taken by a function for the fasd plugin. please rename to something that won't clobber or move to your private alias file.
@@ -183,3 +183,16 @@ if ! _command_exists del; then | |||
mkdir -p /tmp/.trash && mv "$@" /tmp/.trash | |||
} | |||
fi | |||
|
|||
# replace multiple file extensions at once | |||
function rex() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a package in Ubuntu and other distros called rex and I would rather not have them clash. Could you consider naming it renex or rext?
# replace multiple file extensions at once | ||
function rex() { | ||
about 'mass replace of the extension of multiple files' | ||
param '1: extension to replace' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation is mixed spaces and tabs, please align all to just tabs. Thanks!
group 'base' | ||
local ext2replace="${1:-}" | ||
local newext="${2:-}" | ||
local files=(`ls *.$ext2replace`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local files=(`ls *.$ext2replace`) | |
local files=(*."$ext2replace") |
`ls *.$foo`
is subject to unwanted word splitting and pathname expansions. One should simply use *."$foo"
. In this case, one should check the generated filenames exist because the literal '*.txt'
remains if the matching filename does not exist and shopt -s nullglob
is not set.
local ext2replace="${1:-}" | ||
local newext="${2:-}" | ||
local files=(`ls *.$ext2replace`) | ||
for file in "${files[@]}"; do mv "$file" "${file/%.$ext2replace/.$newext}"; done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$ext2replace
should be quoted. Otherwise,$ext2replace
is treated as a glob pattern.- In Bash >= 5.1, when
pathsub_replacement
is turned on (which is the default),&
inside$newext
would be expanded to the original matching. It is unusual to include&
in the filename extension, but it would still cause unexpected results when the user specifies such an extension.- One naive workaround would be to quote
$newext
as"$newext"
, but it causes another issue with Bash <= 4.2 or whenshopt -s compat42
is turned on: For example,a=x.dat; echo "${x/".dat"/".txt"}"
would result ina".txt"
. - To properly handle this, one needs to save, unset, and restore the shell setting
patsub_replacement
. - Or another way to work it around is to assign the replacement result to a shell variable with no quoteing of the entire RHS:
local dst=${file/%."$ext2replace"/."$newext"}
. This works under an arbitrary combination ofshopt -q patsub_replacement
andshopt -q compat42
.
- One naive workaround would be to quote
@TheMadTomato hey paul, would you like to try merging with the master and fix the suggested problems please? |
Hello, Sorry i missed your comments. |
Description
New function labeled
rex()
servers a single purpose. Replace the file extensions of multiple file at once. File extensions usually does not quite matter in bash, but there is a few cases when one might needs to and this function simplifies the process from multiple commands into one.The Function
How Has This Been Tested?
Created multiple test files with different extensions, than proceeded to use
rex
to only change the specified extension which worked flawlesslyTesting Environment
Screenshots (if appropriate):
Types of changes
Checklist:
clean_files.txt
and formatted it usinglint_clean_files.sh
.