Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
added 47 characters in body
Source Link
pLumo
  • 23.2k
  • 2
  • 43
  • 70

You don't escape the spaces when you already quoted them, otherwise the backslashes will be taken literally:

base_dir="/backups/system\ db\ files/temp" 

should be

base_dir="/backups/system db files/temp"

Also why do you have _ in ${filename_base_} ?

Your last command should have probably worked if everything else was correct:

counter=$(ls "${sub_dir}/${filename_base}"202203*.tar.gz | wc -l)

Anyways, you should not parse ls.

Rather use find:

find "${sub_dir}" -name "${filename_base}202203*.tar.gz" -printf '.' | wc -c

or an array:

shopt -s nullglob
files=("${sub_dir}/${filename_base}"202203*.tar.gz)
echo "${#files[@]}"

You need the nullglob option, otherwise the result will be 1 if no files match your pattern as the string is then taken literally.

You don't escape the spaces when you already quoted them, otherwise the backslashes will be taken literally:

base_dir="/backups/system\ db\ files/temp" 

should be

base_dir="/backups/system db files/temp"

Also why do you have _ in ${filename_base_} ?

Your last command should have worked if everything else was correct:

counter=$(ls "${sub_dir}/${filename_base}"202203*.tar.gz | wc -l)

Anyways, you should not parse ls.

Rather use find:

find "${sub_dir}" -name "${filename_base}202203*.tar.gz" -printf '.' | wc -c

or an array:

shopt -s nullglob
files=("${sub_dir}/${filename_base}"202203*.tar.gz)
echo "${#files[@]}"

You don't escape the spaces when you already quoted them, otherwise the backslashes will be taken literally:

base_dir="/backups/system\ db\ files/temp" 

should be

base_dir="/backups/system db files/temp"

Also why do you have _ in ${filename_base_} ?

Your last command should have probably worked if everything else was correct:

counter=$(ls "${sub_dir}/${filename_base}"202203*.tar.gz | wc -l)

Anyways, you should not parse ls.

Rather use find:

find "${sub_dir}" -name "${filename_base}202203*.tar.gz" -printf '.' | wc -c

or an array:

shopt -s nullglob
files=("${sub_dir}/${filename_base}"202203*.tar.gz)
echo "${#files[@]}"

You need the nullglob option, otherwise the result will be 1 if no files match your pattern as the string is then taken literally.

added 47 characters in body
Source Link
pLumo
  • 23.2k
  • 2
  • 43
  • 70

You don't escape the spaces when you quote italready quoted them, otherwise the backslashes will be taken literally:

base_dir="/backups/system\ db\ files/temp" 

should be

base_dir="/backups/system db files/temp"

Also why do you have _ in ${filename_base_} ?

Your last command should have worked if everything else was correct:

counter=$(ls "${sub_dir}/${filename_base}"202203*.tar.gz | wc -l)

Anyways, you should not parse ls.

Rather use find:

find "${sub_dir}" -name "${filename_base}202203*.tar.gz" -printf '.' | wc -c

or an array:

shopt -s nullglob
files=("${sub_dir}/${filename_base}"202203*.tar.gz)
echo "${#files[@]}"

You don't escape the spaces when you quote it:

base_dir="/backups/system\ db\ files/temp" 

should be

base_dir="/backups/system db files/temp"

Also why do you have _ in ${filename_base_} ?


Anyways, you should not parse ls.

Rather use find:

find "${sub_dir}" -name "${filename_base}202203*.tar.gz" -printf '.' | wc -c

or an array:

shopt -s nullglob
files=("${sub_dir}/${filename_base}"202203*.tar.gz)
echo "${#files[@]}"

You don't escape the spaces when you already quoted them, otherwise the backslashes will be taken literally:

base_dir="/backups/system\ db\ files/temp" 

should be

base_dir="/backups/system db files/temp"

Also why do you have _ in ${filename_base_} ?

Your last command should have worked if everything else was correct:

counter=$(ls "${sub_dir}/${filename_base}"202203*.tar.gz | wc -l)

Anyways, you should not parse ls.

Rather use find:

find "${sub_dir}" -name "${filename_base}202203*.tar.gz" -printf '.' | wc -c

or an array:

shopt -s nullglob
files=("${sub_dir}/${filename_base}"202203*.tar.gz)
echo "${#files[@]}"
Source Link
pLumo
  • 23.2k
  • 2
  • 43
  • 70

You don't escape the spaces when you quote it:

base_dir="/backups/system\ db\ files/temp" 

should be

base_dir="/backups/system db files/temp"

Also why do you have _ in ${filename_base_} ?


Anyways, you should not parse ls.

Rather use find:

find "${sub_dir}" -name "${filename_base}202203*.tar.gz" -printf '.' | wc -c

or an array:

shopt -s nullglob
files=("${sub_dir}/${filename_base}"202203*.tar.gz)
echo "${#files[@]}"