Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
4 added 65 characters in body
source | link

With bash, you could do:

all=(*)
except=(file1 file2 notme.txt)
only=()
IFS=/
for ifile in "${all[@]}"; do
  case "/${except[*]}/" in
    (*"/$i$file/"*) ;;     # do nothing (exclude)
    (*) only+=("$i""$file")  # add to the array
  esac
done
ls -ld -- "${only[@]}"

(that works here for the files in the current directory, but not reliably for globs like all=(*/*) except=(foo/bar) as we use / to join the elements of the array for the look-up).

It's based on the fact that "${array[*]}" joins the elements of the array with the first character of $IFS (here chosen to be / as it can't otherwise occur in a file name; NUL is a character that can't occur in a file path, but unfortunately bash (contrary to zsh) can't have such a character in its variables). So for each file in $all (here with $file being foo as an example), we do a case "/file1/file2/notme.txt/" in (*"/$filefoo/"*) to check if $file is to be excluded.

With bash, you could do:

all=(*)
except=(file1 file2 notme.txt)
only=()
IFS=/
for i in "${all[@]}"; do
  case "/${except[*]}/" in (*"/$i/"*) ;;
    (*) only+=("$i")
  esac
done
ls -ld -- "${only[@]}"

(that works here for the files in the current directory, but not reliably for globs like all=(*/*) except=(foo/bar) as we use / to join the elements of the array for the look-up).

It's based on the fact that "${array[*]}" joins the elements of the array with the first character of $IFS (here chosen to be / as it can't otherwise occur in a file name; NUL is a character that can't occur in a file path, but unfortunately bash (contrary to zsh) can't have such a character in its variables). So for each file in $all, we do a case "/file1/file2/notme.txt/" in (*"/$file/"*) to check if $file is to be excluded.

With bash, you could do:

all=(*)
except=(file1 file2 notme.txt)
only=()
IFS=/
for file in "${all[@]}"; do
  case "/${except[*]}/" in
    (*"/$file/"*) ;;     # do nothing (exclude)
    (*) only+=("$file")  # add to the array
  esac
done
ls -ld -- "${only[@]}"

(that works here for the files in the current directory, but not reliably for globs like all=(*/*) except=(foo/bar) as we use / to join the elements of the array for the look-up).

It's based on the fact that "${array[*]}" joins the elements of the array with the first character of $IFS (here chosen to be / as it can't otherwise occur in a file name; NUL is a character that can't occur in a file path, but unfortunately bash (contrary to zsh) can't have such a character in its variables). So for each file in $all (here with $file being foo as an example), we do a case "/file1/file2/notme.txt/" in (*"/foo/"*) to check if $file is to be excluded.

3 added 214 characters in body
source | link

With bash, you could do:

all=(*)
except=(file1 file2 notme.txt)
only=()
IFS=/
for i in "${all[@]}"; do
  case "/${except[*]}/" in (*"/$i/"*) ;;
    (*) only+=("$i")
  esac
done
ls -ld -- "${only[@]}"

(that works here for the files in the current directory, but not reliably for globs like all=(*/*) except=(foo/bar) as we use / to join the elements of the array for the look-up).

It's based on the fact that "${array[*]}" joins the elements of the array with the first character of $IFS (here chosen to be / as it can't otherwise occur in a file name; NUL is a character that can't occur in a file path, but unfortunately bash (contrary to zsh) can't have such a character in its variables). So for each file in $all, we do a case "/file1/file2/notme.txt/" in (*"/$file/"*) to check if $file is to be excluded.

With bash, you could do:

all=(*)
except=(file1 file2 notme.txt)
only=()
IFS=/
for i in "${all[@]}"; do
  case "/${except[*]}/" in (*"/$i/"*) ;;
    (*) only+=("$i")
  esac
done
ls -ld -- "${only[@]}"

(that works here for the files in the current directory, but not reliably for globs like all=(*/*) except=(foo/bar) as we use / to join the elements of the array for the look-up).

It's based on the fact that "${array[*]}" joins the elements of the array with the first character of $IFS. So for each file in $all, we do a case "/file1/file2/notme.txt/" in (*"/$file/"*) to check if $file is to be excluded.

With bash, you could do:

all=(*)
except=(file1 file2 notme.txt)
only=()
IFS=/
for i in "${all[@]}"; do
  case "/${except[*]}/" in (*"/$i/"*) ;;
    (*) only+=("$i")
  esac
done
ls -ld -- "${only[@]}"

(that works here for the files in the current directory, but not reliably for globs like all=(*/*) except=(foo/bar) as we use / to join the elements of the array for the look-up).

It's based on the fact that "${array[*]}" joins the elements of the array with the first character of $IFS (here chosen to be / as it can't otherwise occur in a file name; NUL is a character that can't occur in a file path, but unfortunately bash (contrary to zsh) can't have such a character in its variables). So for each file in $all, we do a case "/file1/file2/notme.txt/" in (*"/$file/"*) to check if $file is to be excluded.

2 added 239 characters in body
source | link

With bash, you could do:

all=(*)
except=(file1 file2 notme.txt)
only=()
IFS=/
for i in "${all[@]}"; do
  case "/${except[*]}/" in (*"/$i/"*) ;;
    (*) only+=("$i")
  esac
done
ls -ld -- "${only[@]}"

(that works here for the files in the current directory, but not reliably for globs like all=(*/*) except=(foo/bar) as we use / to join the elements of the array for the look-up).

It's based on the fact that "${array[*]}" joins the elements of the array with the first character of $IFS. So for each file in $all, we do a case "/file1/file2/notme.txt/" in (*"/$file/"*) to check if $file is to be excluded.

With bash, you could do:

all=(*)
except=(file1 file2 notme.txt)
only=()
IFS=/
for i in "${all[@]}"; do
  case "/${except[*]}/" in (*"/$i/"*) ;;
    (*) only+=("$i")
  esac
done
ls -ld -- "${only[@]}"

(that works here for the files in the current directory, but not reliably for globs like all=(*/*) except=(foo/bar) as we use / to join the elements of the array for the look-up).

With bash, you could do:

all=(*)
except=(file1 file2 notme.txt)
only=()
IFS=/
for i in "${all[@]}"; do
  case "/${except[*]}/" in (*"/$i/"*) ;;
    (*) only+=("$i")
  esac
done
ls -ld -- "${only[@]}"

(that works here for the files in the current directory, but not reliably for globs like all=(*/*) except=(foo/bar) as we use / to join the elements of the array for the look-up).

It's based on the fact that "${array[*]}" joins the elements of the array with the first character of $IFS. So for each file in $all, we do a case "/file1/file2/notme.txt/" in (*"/$file/"*) to check if $file is to be excluded.

1
source | link