Skip to main content
Typo
Source Link
Chris Davies
  • 128.3k
  • 16
  • 179
  • 324

Your initial process renamed files by appending .json. For example file1 (matching the pattern file?) was renamed to file1.json. The reverse is more complicated and requires a little shell script processing

find . -type f -name 'file?.json' -exec sh -c 'mv -- "$1" "${1%.json}"' _ {} \; 

This will invoke sh for each file found by find. Typically a more efficient process can be created for very little increase in complexity:

find . -type f -name 'file?.json' -exec sh -c 'for f in "$@"; do mv -- "$f" "${f%.json}"; done' _ {} + 

Both solutions make use the POSIX shell substitution ${var%suffix}, which would returnreturns a value corresponding to $var with the literal text suffix removed (if possible) from the end of its value.

var=hello.txt echo "${var%.txt}" # "hello" because ".txt" is removed echo "${var%.zzz}" # "hello.txt" because there is no "zzz"".zzz" suffix echo "$var" # "hello.txt" because the variable itself is unchanged 

Your initial process renamed files by appending .json. For example file1 (matching the pattern file?) was renamed to file1.json. The reverse is more complicated and requires a little shell script processing

find . -type f -name 'file?.json' -exec sh -c 'mv -- "$1" "${1%.json}"' _ {} \; 

This will invoke sh for each file found by find. Typically a more efficient process can be created for very little increase in complexity:

find . -type f -name 'file?.json' -exec sh -c 'for f in "$@"; do mv -- "$f" "${f%.json}"; done' _ {} + 

Both solutions use the POSIX shell substitution ${var%suffix}, which would return a value corresponding to $var with the literal text suffix removed from the end of its value.

var=hello.txt echo "${var%.txt}" # "hello" because ".txt" is removed echo "${var%.zzz}" # "hello.txt" because there is no "zzz" suffix 

Your initial process renamed files by appending .json. For example file1 (matching the pattern file?) was renamed to file1.json. The reverse is more complicated and requires a little shell script processing

find . -type f -name 'file?.json' -exec sh -c 'mv -- "$1" "${1%.json}"' _ {} \; 

This will invoke sh for each file found by find. Typically a more efficient process can be created for very little increase in complexity:

find . -type f -name 'file?.json' -exec sh -c 'for f in "$@"; do mv -- "$f" "${f%.json}"; done' _ {} + 

Both solutions make use the POSIX shell substitution ${var%suffix}, which returns a value corresponding to $var with the literal text suffix removed (if possible) from the end of its value.

var=hello.txt echo "${var%.txt}" # "hello" because ".txt" is removed echo "${var%.zzz}" # "hello.txt" because there is no ".zzz" suffix echo "$var" # "hello.txt" because the variable itself is unchanged 
Source Link
Chris Davies
  • 128.3k
  • 16
  • 179
  • 324

Your initial process renamed files by appending .json. For example file1 (matching the pattern file?) was renamed to file1.json. The reverse is more complicated and requires a little shell script processing

find . -type f -name 'file?.json' -exec sh -c 'mv -- "$1" "${1%.json}"' _ {} \; 

This will invoke sh for each file found by find. Typically a more efficient process can be created for very little increase in complexity:

find . -type f -name 'file?.json' -exec sh -c 'for f in "$@"; do mv -- "$f" "${f%.json}"; done' _ {} + 

Both solutions use the POSIX shell substitution ${var%suffix}, which would return a value corresponding to $var with the literal text suffix removed from the end of its value.

var=hello.txt echo "${var%.txt}" # "hello" because ".txt" is removed echo "${var%.zzz}" # "hello.txt" because there is no "zzz" suffix