I have a big number of files around 185000, 99% of then start with a 6 digit number follow by underscore and other random symbols and random extensions
312095_ck_image-24-10-20-11-29-1.jpeg
312095_ck_image-24-10-20-11-29-2.jpeg
312095_ck_image-24-10-20-11-29.jpeg
Basically this six digit number is a userid(user id given by some backend oracle db),
each of the user ids can appear multiple times in a directory. I also have a text file of 6 digit numbers one per each line( around 18000 numbers) . Is it possible to match files directory with the content of a text file.
So if a file starts with a number that is in the text file I want it moved to another directory(regardless off the rest of the name or extension) so I can delete that folder later. I just want all the matching files in one folder so I can delete entire folder and not each individual files. Is this even possible in Linux (in shell or by installing/building from source some other Linux program). OS version is RHEL Linux 6.
If it makes this any easier I can load a list of files in a directory to a db table and match it against the list of numbers I have in a text file, so I can know exactly what is the of the file to be re/moved. I just don't know how to then feed that list to mv command so it can move/delete the files. what's the easiest way to achieve it?
So if my actual folder Is /new_upload/entrants/
and I have a empty folder called junk on the same level as entrants /new upload/junk
and if 312095 apears inside a list of ids I want to execute
mv 312095_* /new_upload/junk
[idis] entrants# sh -h
sh-4.1#
[idis] entrants# $SHELL --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
grep
manpage, particularly the-f
flag which allows you to pass a filename full of patterns (your file of user IDs). Also, see this answer which talks generally about how to approach problems with a simple text-based approach to where you're actually creating a script, instead of executing one. IMO, it is much more conducive to learning, experimentation and testing, while minimizing the risk of damage or malfunction. – Jim L. Dec 12 '23 at 19:25echo "${file}"
instead ofrm "${file}"
and get a list of files that would have been deleted. – Marcus Müller Dec 12 '23 at 20:14