Ask Your Question
0

Bash: How can I move all files containing a certain pattern to a different directory?

asked 2023-04-11 22:02:31 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-11 22:06:20 +0000

qstack gravatar image

updated 2023-04-11 22:06:52 +0000

If you want to move files that contain a certain pattern within the file contents, you can use grep to search for the pattern and then move the matching files. Here's an example:

Suppose you want to move all files containing the pattern example from the current directory to the target_directory:

for file in *; do
  if grep -q "example" "$file"; then
    mv "$file" target_directory/
  fi
done
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

1 follower

Stats

Asked: 2023-04-11 22:02:31 +0000

Seen: 21 times

Last updated: Apr 11 '23