I'm working on a bash script for my server backup. Based on my web root(/home), I wanna filter my web directories excludes something generals. I found --ignore option for it. Here's my code for returng what I want.
DIR_LIST=`ls -al $WWW_ROOT --ignore={.,..,ubuntu,test} | grep "^d" | awk '{ print $9 }'`
echo $DIR_LIST;
But when I tried with array, it's not worked as well.
EXCLUDED=(. .. test ubuntu)
STR=$(IFS=,; echo "${EXCLUDED[*]}")
DIR_LIST=`ls -al $WWW_ROOT --ignore={$STR} | grep "^d" | awk '{ print $9 }'`
echo $DIR_LIST;
echo $STR works well but echo $DIR_LIST is not. I think brace is not worked properly.
How can I do this as I expected?
Here is one way of doing it without using ls
and to make matters worst you're using the -al
flag.
#!/usr/bin/env bash
shopt -s nullglob extglob
files=(/path/to/www/directory/!(ubuntu|test)/)
declare -p files
That will show you the files in the array assignment.
If you want to loop through the files and remove the pathname from the file name without using any external commands from the shell.
for f in "${files[@]}"; do echo "${f##*/}"; done
Which has the same result when using basename
for f in "${files[@]}"; do var=$(basename "$f"); echo "$var"; done
Or just do it in the array
printf '%s\n' "${files[@]##*/}"
The "${files##*/}"
is a form of P.E. parameter expansion.
There is an online bash manual where you can look up P.E. see Parameter Expansion
Or the man page. see PAGER='less +/^[[:space:]]*parameter\ expansion' man bash
Look up nullglob
and extglob
see shell globbing
The array named files
now has the data/files that you're interested in.
By default the dotfiles is not listed so you don't have to worry about it, unless dotglob
is enabled which is off by default.