Skip to main content
safety measures added in the answer
Source Link
Rakib Fiha
  • 660
  • 4
  • 10
  • 24

This is what you are looking for.

What is need to be done basically are as follows:

1, check if the digit is single or double-digit. If it is single digit and the value less than or equal to 9 then, rename the file. Which should be as simple as doing:

(( someDigit <= 9 )) && [[ ${#someDigit'slength} -lt 2 ]] 

2, since there would be some files with letters so, we can store them as:

digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); 

3, then filter the digit so that we can compare it for first option. Add more filters which you may feel would be necessary in tr -d option as follows:

digitOnly=$( echo $digit | tr -d [a-Z] ); 

4, then run a loop in your directory where you intend to rename the files. Implementing all of this would look as follows:

for file in *file.txt; do digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); digitOnly=$( echo $digit | tr -d [a-Z] ); if (( digitOnly <= 9 )) && [[ ${#digitOnly} -lt 2 ]]; then # mv "${file}" "ch 0${digit} - file.txt" echo "'${file}' is renamed to 'ch 0${digit} - file.txt'"; fi done 

WithRemove the # from mv, after you are satisfied before real execution. With your example:

touch 'ch 10 - file.txt' 'ch 2 - file.txt' 'ch 3 - file.txt' 'ch 4a - file.txt' 'ch 5 - file.txt' 

Then, executing the script or copying pasting the above script in bash will give you the following output:

'ch 2 - file.txt' is renamed to 'ch 02 - file.txt' 'ch 3 - file.txt' is renamed to 'ch 03 - file.txt' 'ch 4a - file.txt' is renamed to 'ch 04a - file.txt' 'ch 5 - file.txt' is renamed to 'ch 05 - file.txt' 

Obviously, you can even make it more generic, by including it in a function then take input such as file pattern search and so on, but your question does not demand that. So, I believe, the current answer would handle the situation stated in the question quite well.

This is what you are looking for.

What is need to be done basically are as follows:

1, check if the digit is single or double-digit. If it is single digit and the value less than or equal to 9 then, rename the file. Which should be as simple as doing:

(( someDigit <= 9 )) && [[ ${#someDigit'slength} -lt 2 ]] 

2, since there would be some files with letters so, we can store them as:

digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); 

3, then filter the digit so that we can compare it for first option. Add more filters which you may feel would be necessary in tr -d option as follows:

digitOnly=$( echo $digit | tr -d [a-Z] ); 

4, then run a loop in your directory where you intend to rename the files. Implementing all of this would look as follows:

for file in *file.txt; do digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); digitOnly=$( echo $digit | tr -d [a-Z] ); if (( digitOnly <= 9 )) && [[ ${#digitOnly} -lt 2 ]]; then mv "${file}" "ch 0${digit} - file.txt" echo "'${file}' is renamed to 'ch 0${digit} - file.txt'"; fi done 

With your example:

touch 'ch 10 - file.txt' 'ch 2 - file.txt' 'ch 3 - file.txt' 'ch 4a - file.txt' 'ch 5 - file.txt' 

Then, executing the script or copying pasting the above script in bash will give you the following output:

'ch 2 - file.txt' is renamed to 'ch 02 - file.txt' 'ch 3 - file.txt' is renamed to 'ch 03 - file.txt' 'ch 4a - file.txt' is renamed to 'ch 04a - file.txt' 'ch 5 - file.txt' is renamed to 'ch 05 - file.txt' 

Obviously, you can even make it more generic, by including it in a function then take input such as file pattern search and so on, but your question does not demand that. So, I believe, the current answer would handle the situation stated in the question quite well.

This is what you are looking for.

What is need to be done basically are as follows:

1, check if the digit is single or double-digit. If it is single digit and the value less than or equal to 9 then, rename the file. Which should be as simple as doing:

(( someDigit <= 9 )) && [[ ${#someDigit'slength} -lt 2 ]] 

2, since there would be some files with letters so, we can store them as:

digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); 

3, then filter the digit so that we can compare it for first option. Add more filters which you may feel would be necessary in tr -d option as follows:

digitOnly=$( echo $digit | tr -d [a-Z] ); 

4, then run a loop in your directory where you intend to rename the files. Implementing all of this would look as follows:

for file in *file.txt; do digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); digitOnly=$( echo $digit | tr -d [a-Z] ); if (( digitOnly <= 9 )) && [[ ${#digitOnly} -lt 2 ]]; then # mv "${file}" "ch 0${digit} - file.txt" echo "'${file}' is renamed to 'ch 0${digit} - file.txt'"; fi done 

Remove the # from mv, after you are satisfied before real execution. With your example:

touch 'ch 10 - file.txt' 'ch 2 - file.txt' 'ch 3 - file.txt' 'ch 4a - file.txt' 'ch 5 - file.txt' 

Then, executing the script or copying pasting the above script in bash will give you the following output:

'ch 2 - file.txt' is renamed to 'ch 02 - file.txt' 'ch 3 - file.txt' is renamed to 'ch 03 - file.txt' 'ch 4a - file.txt' is renamed to 'ch 04a - file.txt' 'ch 5 - file.txt' is renamed to 'ch 05 - file.txt' 

Obviously, you can even make it more generic, by including it in a function then take input such as file pattern search and so on, but your question does not demand that. So, I believe, the current answer would handle the situation stated in the question quite well.

added more description about the answer
Source Link
Rakib Fiha
  • 660
  • 4
  • 10
  • 24

This is what you are looking for.

What is need to be done basically are as follows:

1, check if the digit is single or double-digit. If it is single digit and the value less than or equal to 9 then, rename the file. Which should be as simple as doing:

(( someDigit <= 9 )) && [[ ${#someDigit'slength} -lt 2 ]] 

2, since there would be some files with letters so, we can store them as:

digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); 

3, then filter the digit so that we can compare it for first option. Add more filters which you may feel would be necessary in tr -d option as follows:

digitOnly=$( echo $digit | tr -d [a-Z] ); 

4, then run a loop in your directory where you intend to rename the files. Implementing all of this would look as follows:

for file in *file.txt; do digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); digitOnly=$( echo $digit | tr -d [a-Z] ); if (( digitOnly <= 9 )) && [[ ${#digitOnly} -lt 2 ]]; then mv "${file}" "ch 0${digit} - file.txt" echo "'${file}' is renamed to 'ch 0${digit} - file.txt'"; fi done 

With your example:

touch 'ch 10 - file.txt' 'ch 2 - file.txt' 'ch 3 - file.txt' 'ch 4a - file.txt' 'ch 5 - file.txt' 

Then, executing the script or copying pasting the above script in bash will give you the following output:

'ch 2 - file.txt' is renamed to 'ch 02 - file.txt' 'ch 3 - file.txt' is renamed to 'ch 03 - file.txt' 'ch 4a - file.txt' is renamed to 'ch 04a - file.txt' 'ch 5 - file.txt' is renamed to 'ch 05 - file.txt' 

Obviously, you can even make it more generic, by including it in a function then take input such as file pattern search and so on, but your question does not demand that. So, I believe, the current answer would handle the situation stated in the question quite well.

This is what you are looking for.

What is need to be done basically are as follows:

1, check if the digit is single or double-digit. If it is single digit and the value less than or equal to 9 then, rename the file. Which should be as simple as doing:

(( someDigit <= 9 )) && [[ ${#someDigit'slength} -lt 2 ]] 

2, since there would be some files with letters so, we can store them as:

digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); 

3, then filter the digit so that we can compare it for first option. Add more filters which you may feel would be necessary in tr -d option as follows:

digitOnly=$( echo $digit | tr -d [a-Z] ); 

4, then run a loop in your directory where you intend to rename the files. Implementing all of this would look as follows:

for file in *file.txt; do digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); digitOnly=$( echo $digit | tr -d [a-Z] ); if (( digitOnly <= 9 )) && [[ ${#digitOnly} -lt 2 ]]; then mv "${file}" "ch 0${digit} - file.txt" echo "'${file}' is renamed to 'ch 0${digit} - file.txt'"; fi done 

With your example:

touch 'ch 10 - file.txt' 'ch 2 - file.txt' 'ch 3 - file.txt' 'ch 4a - file.txt' 'ch 5 - file.txt' 

Then executing the script will give the following output:

'ch 2 - file.txt' is renamed to 'ch 02 - file.txt' 'ch 3 - file.txt' is renamed to 'ch 03 - file.txt' 'ch 4a - file.txt' is renamed to 'ch 04a - file.txt' 'ch 5 - file.txt' is renamed to 'ch 05 - file.txt' 

Obviously, you can even make it more generic, by including it in a function then take input such as file pattern search and so on, but your question does not demand that. So, I believe, the current answer would handle the situation stated in the question quite well.

This is what you are looking for.

What is need to be done basically are as follows:

1, check if the digit is single or double-digit. If it is single digit and the value less than or equal to 9 then, rename the file. Which should be as simple as doing:

(( someDigit <= 9 )) && [[ ${#someDigit'slength} -lt 2 ]] 

2, since there would be some files with letters so, we can store them as:

digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); 

3, then filter the digit so that we can compare it for first option. Add more filters which you may feel would be necessary in tr -d option as follows:

digitOnly=$( echo $digit | tr -d [a-Z] ); 

4, then run a loop in your directory where you intend to rename the files. Implementing all of this would look as follows:

for file in *file.txt; do digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); digitOnly=$( echo $digit | tr -d [a-Z] ); if (( digitOnly <= 9 )) && [[ ${#digitOnly} -lt 2 ]]; then mv "${file}" "ch 0${digit} - file.txt" echo "'${file}' is renamed to 'ch 0${digit} - file.txt'"; fi done 

With your example:

touch 'ch 10 - file.txt' 'ch 2 - file.txt' 'ch 3 - file.txt' 'ch 4a - file.txt' 'ch 5 - file.txt' 

Then, executing the script or copying pasting the above script in bash will give you the following output:

'ch 2 - file.txt' is renamed to 'ch 02 - file.txt' 'ch 3 - file.txt' is renamed to 'ch 03 - file.txt' 'ch 4a - file.txt' is renamed to 'ch 04a - file.txt' 'ch 5 - file.txt' is renamed to 'ch 05 - file.txt' 

Obviously, you can even make it more generic, by including it in a function then take input such as file pattern search and so on, but your question does not demand that. So, I believe, the current answer would handle the situation stated in the question quite well.

added more description about the answer
Source Link
Rakib Fiha
  • 660
  • 4
  • 10
  • 24

This is what you are looking for.

What is need to be done basically are as follows:

1, check if the digit is single or double-digit. If it is single digit and the value less than or equal to 9 then, rename the file. Which should be as simple as doing:

#!/usr/bin/env(( bashsomeDigit <= 9 )) && [[ ${#someDigit'slength} -lt 2 ]] 

2, since there would be some files with letters so, we can store them as:

digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); 

3, then filter the digit so that we can compare it for first option. Add more filters which you may feel would be necessary in tr -d option as follows:

digitOnly=$( echo $digit | tr -d [a-Z] );  

4, then run a loop in your directory where you intend to rename the files. Implementing all of this would look as follows:

for file in *file.txt; do digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); digitOnly=$( echo $digit | tr -d [a-Z] ); if (( digitOnly <= 9 )) && [[ ${#digitOnly} -lt 2 ]]; then mv "${file}" "ch 0${digit} - file.txt" echo "'${file}' is renamed to 'ch 0${digit} - file.txt'"; fi done 

With your example:

touch 'ch 10 - file.txt' 'ch 2 - file.txt' 'ch 3 - file.txt' 'ch 4a - file.txt' 'ch 5 - file.txt' 

Then executing the script will give the following output:

'ch 2 - file.txt' is renamed to 'ch 02 - file.txt' 'ch 3 - file.txt' is renamed to 'ch 03 - file.txt' 'ch 4a - file.txt' is renamed to 'ch 04a - file.txt' 'ch 5 - file.txt' is renamed to 'ch 05 - file.txt' 

Obviously, you can even make it more generic, by including it in a function then take input such as file pattern search and so on, but your question does not demand that. So, I believe, the current answer would handle the situation stated in the question quite well.

This is what you are looking for.

#!/usr/bin/env bash for file in *file.txt; do digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); digitOnly=$( echo $digit | tr -d [a-Z] ); if (( digitOnly <= 9 )) && [[ ${#digitOnly} -lt 2 ]]; then mv "${file}" "ch 0${digit} - file.txt" echo "'${file}' is renamed to 'ch 0${digit} - file.txt'"; fi done 

With your example:

touch 'ch 10 - file.txt' 'ch 2 - file.txt' 'ch 3 - file.txt' 'ch 4a - file.txt' 'ch 5 - file.txt' 

Then executing the script will give the following output:

'ch 2 - file.txt' is renamed to 'ch 02 - file.txt' 'ch 3 - file.txt' is renamed to 'ch 03 - file.txt' 'ch 4a - file.txt' is renamed to 'ch 04a - file.txt' 'ch 5 - file.txt' is renamed to 'ch 05 - file.txt' 

Obviously, you can even make it more generic, by including it in a function then take input such as file pattern search and so on, but your question does not demand that. So, I believe, the current answer would handle the situation stated in the question quite well.

This is what you are looking for.

What is need to be done basically are as follows:

1, check if the digit is single or double-digit. If it is single digit and the value less than or equal to 9 then, rename the file. Which should be as simple as doing:

(( someDigit <= 9 )) && [[ ${#someDigit'slength} -lt 2 ]] 

2, since there would be some files with letters so, we can store them as:

digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); 

3, then filter the digit so that we can compare it for first option. Add more filters which you may feel would be necessary in tr -d option as follows:

digitOnly=$( echo $digit | tr -d [a-Z] );  

4, then run a loop in your directory where you intend to rename the files. Implementing all of this would look as follows:

for file in *file.txt; do digit=$( echo $file | cut -d '-' -f1 | grep ch | awk '{print $2}' ); digitOnly=$( echo $digit | tr -d [a-Z] ); if (( digitOnly <= 9 )) && [[ ${#digitOnly} -lt 2 ]]; then mv "${file}" "ch 0${digit} - file.txt" echo "'${file}' is renamed to 'ch 0${digit} - file.txt'"; fi done 

With your example:

touch 'ch 10 - file.txt' 'ch 2 - file.txt' 'ch 3 - file.txt' 'ch 4a - file.txt' 'ch 5 - file.txt' 

Then executing the script will give the following output:

'ch 2 - file.txt' is renamed to 'ch 02 - file.txt' 'ch 3 - file.txt' is renamed to 'ch 03 - file.txt' 'ch 4a - file.txt' is renamed to 'ch 04a - file.txt' 'ch 5 - file.txt' is renamed to 'ch 05 - file.txt' 

Obviously, you can even make it more generic, by including it in a function then take input such as file pattern search and so on, but your question does not demand that. So, I believe, the current answer would handle the situation stated in the question quite well.

added 10 characters in body
Source Link
Rakib Fiha
  • 660
  • 4
  • 10
  • 24
Loading
added 10 characters in body
Source Link
Rakib Fiha
  • 660
  • 4
  • 10
  • 24
Loading
Source Link
Rakib Fiha
  • 660
  • 4
  • 10
  • 24
Loading