Ask Your Question
1

How to use Bash's For loop to skip files that have a particular pattern in their filename just before the file type?

asked 2022-10-30 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-06-06 22:00:00 +0000

scrum gravatar image

To use Bash's For loop to skip files that have a particular pattern in their filename just before the file type, you can use the following syntax:

for file in /path/to/directory/*; do
  if [[ $file == *pattern.* ]]; then
    continue
  fi
  # Your code here
done

In this syntax, the *pattern.* part is the pattern you want to skip. The asterisks represent any characters before and after the pattern, and the dot represents the file extension.

The continue statement inside the if block skips the current iteration of the loop if the pattern is found in the file name.

You can replace # Your code here with any code you want to execute on the files that do not match the pattern.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-10-30 11:00:00 +0000

Seen: 9 times

Last updated: Jun 06 '21