Reputation: 5
I have 1000 files with following names:
something-345-something.txt
something-5468-something.txt
something-100-something.txt
something-6200-something.txt
and a lot more...
And I have one txt file, with only numbers in it. f.e:
1000
500
5468
6200
699
usw...
Now I would like to move all files, which have a number in their filenames which is in my txt file.
So in my example above the following files should be moved only: something-5468-something.txt something-6200-something.txt
Is there an easy way to achieve this?
Upvotes: 0
Views: 70
Reputation: 247012
Here's a crazy bit of bash hackery
shopt -s extglob nullglob
mv -t /target/dir *-@($(paste -sd "|" numbers.txt))-*
That uses paste
to join all the lines in your numbers file with pipe characters, then uses bash extended pattern matching to find the files matching any one of the numbers.
I assume mv
from GNU coreutils for the -t
option.
Upvotes: 0
Reputation: 26955
What about on the fly moving files by doing this:
for i in `cat you-file.txt`; do
find . -iname "*-$i-*" -exec mv '{}' /target/dir \;
; done
For every line in your text file, the find
command will try to find only does matching the pattern *-$i-*
(something-6200-something.txt) and move it to your target dir.
Upvotes: 1
Reputation: 4101
Script file named move
(executable):
#!/bin/bash
TARGETDIR="$1"
FILES=`find . -type f` # build list of files
while read n # read numbers from standard input
do # n contains a number => filter list of files by that number:
echo "$FILES" | grep "\-$n-" | while read f
do # move file that passed the filter because its name matches n:
mv "$f" "$TARGETDIR"
done
done
Use it like this:
cd directory-with-files
./move target-directory < number-list.txt
Upvotes: 0
Reputation: 3739
Naive implementation: for file in $(ls); do grep $(echo -n $file | sed -nr 's/[^-]*-([0-9]+).*/\1/p') my-one-txt.txt && mv $file /tmp/somewhere; done
In English: For every file in output of ls: parse number part of filename with sed
and grep for it in your text file. grep
returns a non-zero exit code if nothing is found, so mv
is in evaluated in that case.
Upvotes: 0