Link to home
Start Free TrialLog in
Avatar of compcreate
compcreate

asked on

Grep for specific files and then delete them

Hello,
   I have a directory full of images with two naming conventions:

1234.jpg

- and -

1234_1.jpg

I want to search the directory for all files that DO NOT have the underscore and list them in a text file for me to view and confirm.  Once confirmed I want to delete all the file names on the list generated.

I assume I can grep the directory to get my output and then redirect the output to a delete command some how.

Grateful for any guidance.
SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of compcreate
compcreate

ASKER

The first line worked to create the proper list, however the second line did not delete them.
cat /tmp/filelist | xargs -0 rm
Should have deleted the files if they had no spaces in their name.

If they have any spaces, use

cat /tmp/filelist  | xargs -I{} rm -f "{}"
Perfect Tintin

#cat /tmp/filelist  | xargs -I{} rm -rf "{}"

Just to add "r" in-case some folders

TY/SA
This still did not work.  The file names do not contain spaces.  They are exactly as I first described HOWEVER I did notice something else...

when I ls the directory the files look like this:

1234.jpg*
13456.jpg*
123_1.jpg*

etc....

So I do not know what the * is after the extension but could that be affecting this?

thanks!
#ls *.jpg? | grep -v _ >/tmp/filelist

#cat /tmp/filelist  | xargs -I{} rm -rf "{}"

to see with ls as what is the use of these "*" directories/files use #ls -laF

TY/SA
ls -F
would display * after executable files,
but .jpg files are usually not executable
The * is just indicating that the files have executable permissions - this is fine, and won't affect the command.

Are you running the second command from the same folder where you ran the first command?  If you cd'd to /tmp to look at the filelist and then ran the 2nd command from there, it would not work.  You need to cd back to where the directory where the files are.

You can better see what is going wrong if you do this:

cat /tmp/filelist  | xargs rm -i

This will prompt you for deletion on every file in the list, and then you can see what it's trying to do, and where it's failing.
You have ls aliased, so change

cat /tmp/filelist | xargs rm -f

to

cat /tmp/filelist | xargs /bin/rm -f
when I run:

cat /tmp/filelist  | xargs rm -i

I get this:

rm: cannot lstat `17428.jpg*': No such file or directory

The first command works perfectly and gives me the list I need.  The second goes through and produces and error for 17428 different pictures that it can't find to delete.

Thanks
If the list is not too long, a quick and dirty work around might be
rm -i `cat /tmp/filelist`
but that could also delete 17428.jpg.zzz

Another workaround might be
echo *.jpg | grep -o '\<[^_ ]*.jpg\>' >  /tmp/filelist
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THAT DID IT!  Thanks!