Skip to main content
added 6 characters in body
Source Link
guest_7
  • 5.8k
  • 1
  • 8
  • 13

If we do not have access to GNU version of xargs/sed , then we need to take the responsibility to quote filenames safe for xargs.

Usage:

 ./myscript   your list of files goes here
#!/bin/bash

# user defined function: qXargs
# backslashes all chars special to xargs:
# SPC/TAB/NL/double quotes, single quotes, and backslash.

qXargs() {
  printf '%s\n' "$1" |
  sed \
    -e "s:[\\'${IFS%?}\"]:\\\\&:g" \
    -e '$!s:$:\\:'  \
  ;
}

# loop over command-line arguments
# quote them to make xargs safe and
# break apart arg into head portion and
#'extension and slip in today's date

today=$(date +'%Y-%d-%m')

for arg
do
   src=$(qXargs "$arg")
  head=$(qXargs "${arg%.*}")
  tail=$(qXargs "${arg##*.}")
  printf '%s\n%s_%s.%s\n'  \
    "$src" \
    "$head" "$today" "$tail" ;
done | xargs -n 2 -t mv -f --

Assuming GNU versions of utilities.

#!/bin/bash
### this is the ./myscript file
d=$(date +'%Y-%d-%m')
printf '%s\0' "$@" |
sed -Ez "p;s/(.*)(\..*)/\1$d\2/" |
xargs -r0 -n2 -t mv -f --

Notes:

  • Your confusion regarding not being able to get the extension from the xargs replacement string {} is bcoz {} is just a placeholder to remind xargs to replace it with the argument. So the shell while parsing the xargs command cannot see it.

If we do not have access to GNU version of xargs/sed , then we need to take the responsibility to quote filenames safe for xargs.

Usage:

 ./myscript   your list of files goes here
#!/bin/bash

# user defined function: qXargs
# backslashes all chars special to xargs:
# SPC/TAB/NL/double quotes, single quotes, and backslash.

qXargs() {
  printf '%s\n' "$1" |
  sed \
    -e "s:[\\'${IFS%?}\"]:\\\\&:g" \
    -e '$!s:$:\\:'  \
  ;
}

# loop over command-line arguments
# quote them to make xargs safe and
# break apart arg into head portion and
#'extension and slip in today's date

today=$(date +'%Y-%d-%m')

for arg
do
   src=$(qXargs "$arg")
  head=$(qXargs "${arg%.*}")
  tail=$(qXargs "${arg##*.}")
  printf '%s\n%s_%s.%s\n'  \
    "$src" \
    "$head" "$today" "$tail" ;
done | xargs -n 2 -t mv

Assuming GNU versions of utilities.

#!/bin/bash
### this is the ./myscript file
d=$(date +'%Y-%d-%m')
printf '%s\0' "$@" |
sed -Ez "p;s/(.*)(\..*)/\1$d\2/" |
xargs -r0 -n2 -t mv

Notes:

  • Your confusion regarding not being able to get the extension from the xargs replacement string {} is bcoz {} is just a placeholder to remind xargs to replace it with the argument. So the shell while parsing the xargs command cannot see it.

If we do not have access to GNU version of xargs/sed , then we need to take the responsibility to quote filenames safe for xargs.

Usage:

 ./myscript   your list of files goes here
#!/bin/bash

# user defined function: qXargs
# backslashes all chars special to xargs:
# SPC/TAB/NL/double quotes, single quotes, and backslash.

qXargs() {
  printf '%s\n' "$1" |
  sed \
    -e "s:[\\'${IFS%?}\"]:\\\\&:g" \
    -e '$!s:$:\\:'  \
  ;
}

# loop over command-line arguments
# quote them to make xargs safe and
# break apart arg into head portion and
#'extension and slip in today's date

today=$(date +'%Y-%d-%m')

for arg
do
   src=$(qXargs "$arg")
  head=$(qXargs "${arg%.*}")
  tail=$(qXargs "${arg##*.}")
  printf '%s\n%s_%s.%s\n'  \
    "$src" \
    "$head" "$today" "$tail" ;
done | xargs -n 2 -t mv -f --

Assuming GNU versions of utilities.

#!/bin/bash
### this is the ./myscript file
d=$(date +'%Y-%d-%m')
printf '%s\0' "$@" |
sed -Ez "p;s/(.*)(\..*)/\1$d\2/" |
xargs -r0 -n2 -t mv -f --

Notes:

  • Your confusion regarding not being able to get the extension from the xargs replacement string {} is bcoz {} is just a placeholder to remind xargs to replace it with the argument. So the shell while parsing the xargs command cannot see it.
added 44 characters in body
Source Link
guest_7
  • 5.8k
  • 1
  • 8
  • 13

If youwe do not have access to GNU version of xargs/sed , then we need to take the responsibility to quote filenames safe for xargs.

Usage:

 ./myscript   your list of files goes here
#!/bin/bash

# user defined function: qXargs
# backslashes all chars special to xargs:
# SPC/TAB/NL/double quotes, and single quotes, and backslash.

qXargs() {
  printf '%s\n' "$1" |
  sed \
    -e "s:[\\'${IFS%?}\"]:\\\\&:g" \
    -e '$!s:$:\\:'  \
  ;
}

# loop over command-line arguments
# quote them to make xargs safe and
# break apart arg into head portion and
#'extension and slip in today's date

today=$(date +'%Y-%d-%m')

for arg
do
   src=$(qXargs "$arg")
  head=$(qXargs "${arg%.*}")
  tail=$(qXargs "${arg##*.}")
  printf '%s\n%s_%s.%s\n'  \
    "$src" \
    "$head" "$today" "$tail" ;
done | xargs -n 2 -t mv

Assuming GNU versions of utilities.

#!/bin/bash
### this is the ./myscript file
d=$(date +'%Y-%d-%m')
printf '%s\0' "$@" |
sed -Ez "p;s/(.*)(\..*)/\1$d\2/" |
xargs -r0 -n2 -t mv

Notes:

  • Your confusion regarding not being able to get the extension from the xargs replacement string {} is bcoz {} is just a placeholder to remind xargs to replace it with the argument. So the shell while parsing the xargs command cannot see it.

If you do not have access to GNU version of xargs/sed , then we take the responsibility to quote filenames safe for xargs.

Usage:

 ./myscript   your list of files goes here
#!/bin/bash

# user defined function
# backslashes all chars special to xargs:
# SPC/TAB/NL/double quotes, and single quotes

qXargs() {
  printf '%s\n' "$1" |
  sed \
    -e "s:[\\'${IFS%?}\"]:\\\\&:g" \
    -e '$!s:$:\\:'  \
  ;
}

# loop over command-line arguments
# quote them to make xargs safe and
# break apart arg into head portion and
#'extension and slip in today's date
for arg
do
   src=$(qXargs "$arg")
  head=$(qXargs "${arg%.*}")
  tail=$(qXargs "${arg##*.}")
  printf '%s\n%s_%s.%s\n'  \
    "$src" \
    "$head" "$today" "$tail" ;
done | xargs -n 2 -t mv

Assuming GNU versions of utilities.

#!/bin/bash
### this is the ./myscript file
d=$(date +'%Y-%d-%m')
printf '%s\0' "$@" |
sed -Ez "p;s/(.*)(\..*)/\1$d\2/" |
xargs -r0 -n2 -t mv

Notes:

  • Your confusion regarding not being able to get the extension from the xargs replacement string {} is bcoz {} is just a placeholder to remind xargs to replace it with the argument. So the shell while parsing the xargs command cannot see it.

If we do not have access to GNU version of xargs/sed , then we need to take the responsibility to quote filenames safe for xargs.

Usage:

 ./myscript   your list of files goes here
#!/bin/bash

# user defined function: qXargs
# backslashes all chars special to xargs:
# SPC/TAB/NL/double quotes, single quotes, and backslash.

qXargs() {
  printf '%s\n' "$1" |
  sed \
    -e "s:[\\'${IFS%?}\"]:\\\\&:g" \
    -e '$!s:$:\\:'  \
  ;
}

# loop over command-line arguments
# quote them to make xargs safe and
# break apart arg into head portion and
#'extension and slip in today's date

today=$(date +'%Y-%d-%m')

for arg
do
   src=$(qXargs "$arg")
  head=$(qXargs "${arg%.*}")
  tail=$(qXargs "${arg##*.}")
  printf '%s\n%s_%s.%s\n'  \
    "$src" \
    "$head" "$today" "$tail" ;
done | xargs -n 2 -t mv

Assuming GNU versions of utilities.

#!/bin/bash
### this is the ./myscript file
d=$(date +'%Y-%d-%m')
printf '%s\0' "$@" |
sed -Ez "p;s/(.*)(\..*)/\1$d\2/" |
xargs -r0 -n2 -t mv

Notes:

  • Your confusion regarding not being able to get the extension from the xargs replacement string {} is bcoz {} is just a placeholder to remind xargs to replace it with the argument. So the shell while parsing the xargs command cannot see it.
added 754 characters in body
Source Link
guest_7
  • 5.8k
  • 1
  • 8
  • 13

If you do not have access to GNU version of xargs/sed , then we take the responsibility to quote filenames safe for xargs.

Usage:

 ./myscript   your list of files goes here
#!/bin/bash

# user defined function
# backslashes all chars special to xargs:
# SPC/TAB/NL/double quotes, and single quotes

qXargs() {
  printf '%s\n' "$1" |
  sed \
    -e "s:[\\'${IFS%?}\"]:\\\\&:g" \
    -e '$!s:$:\\:'  \
  ;
}

# loop over command-line arguments
# quote them to make xargs safe and
# break apart arg into head portion and
#'extension and slip in today's date
for arg
do
   src=$(qXargs "$arg")
  head=$(qXargs "${arg%.*}")
  tail=$(qXargs "${arg##*.}")
  printf '%s\n%s_%s.%s\n'  \
    "$src" \
    "$head" "$today" "$tail" ;
done | xargs -n 2 -t mv

Assuming GNU versions of utilities.

#!/bin/bash
### this is the ./myscript file
d=$(date +'%Y-%d-%m')
printf '%s\0' "$@" |
sed -Ez "p;s/(.*)(\..*)/\1$d\2/" |
xargs -r0 -n2 -t mv
 

Notes:

  • Your confusion regarding not being able to get the extension from the xargs replacement string {} is bcoz {} is just a placeholder to remind xargs to replace it with the argument. So the shell while parsing the xargs command cannot see it.

Assuming GNU versions of utilities.

#!/bin/bash
### this is the ./myscript file
d=$(date +'%Y-%d-%m')
printf '%s\0' "$@" |
sed -Ez "p;s/(.*)(\..*)/\1$d\2/" |
xargs -r0 -n2 -t mv

Notes:

  • Your confusion regarding not being able to get the extension from the xargs replacement string {} is bcoz {} is just a placeholder to remind xargs to replace it with the argument. So the shell while parsing the xargs command cannot see it.

If you do not have access to GNU version of xargs/sed , then we take the responsibility to quote filenames safe for xargs.

Usage:

 ./myscript   your list of files goes here
#!/bin/bash

# user defined function
# backslashes all chars special to xargs:
# SPC/TAB/NL/double quotes, and single quotes

qXargs() {
  printf '%s\n' "$1" |
  sed \
    -e "s:[\\'${IFS%?}\"]:\\\\&:g" \
    -e '$!s:$:\\:'  \
  ;
}

# loop over command-line arguments
# quote them to make xargs safe and
# break apart arg into head portion and
#'extension and slip in today's date
for arg
do
   src=$(qXargs "$arg")
  head=$(qXargs "${arg%.*}")
  tail=$(qXargs "${arg##*.}")
  printf '%s\n%s_%s.%s\n'  \
    "$src" \
    "$head" "$today" "$tail" ;
done | xargs -n 2 -t mv

Assuming GNU versions of utilities.

#!/bin/bash
### this is the ./myscript file
d=$(date +'%Y-%d-%m')
printf '%s\0' "$@" |
sed -Ez "p;s/(.*)(\..*)/\1$d\2/" |
xargs -r0 -n2 -t mv
 

Notes:

  • Your confusion regarding not being able to get the extension from the xargs replacement string {} is bcoz {} is just a placeholder to remind xargs to replace it with the argument. So the shell while parsing the xargs command cannot see it.
added 4 characters in body
Source Link
guest_7
  • 5.8k
  • 1
  • 8
  • 13
Loading
added 4 characters in body
Source Link
guest_7
  • 5.8k
  • 1
  • 8
  • 13
Loading
deleted 156 characters in body
Source Link
guest_7
  • 5.8k
  • 1
  • 8
  • 13
Loading
added 142 characters in body
Source Link
guest_7
  • 5.8k
  • 1
  • 8
  • 13
Loading
Source Link
guest_7
  • 5.8k
  • 1
  • 8
  • 13
Loading