Solved

Grep for specific files and then delete them

Posted on 2014-02-05
14
575 Views
Last Modified: 2014-02-08
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.
0
Comment
Question by:compcreate
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 250 total points
ID: 39837859
ls *.jpg | grep -v _ >/tmp/filelist
cat /tmp/filelist | xargs rm -f
0
 

Author Comment

by:compcreate
ID: 39837877
The first line worked to create the proper list, however the second line did not delete them.
0
 
LVL 6

Expert Comment

by:Shiju Jacob
ID: 39837888
cat /tmp/filelist | xargs -0 rm
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 48

Expert Comment

by:Tintin
ID: 39837889
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 "{}"
0
 
LVL 13

Expert Comment

by:Sandy
ID: 39838018
Perfect Tintin

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

Just to add "r" in-case some folders

TY/SA
0
 

Author Comment

by:compcreate
ID: 39838804
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!
0
 
LVL 13

Expert Comment

by:Sandy
ID: 39838813
#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
0
 
LVL 84

Expert Comment

by:ozo
ID: 39838831
ls -F
would display * after executable files,
but .jpg files are usually not executable
0
 
LVL 19

Expert Comment

by:xterm
ID: 39839181
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.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 39840686
You have ls aliased, so change

cat /tmp/filelist | xargs rm -f

to

cat /tmp/filelist | xargs /bin/rm -f
0
 

Author Comment

by:compcreate
ID: 39843557
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 39843756
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
0
 
LVL 28

Accepted Solution

by:
serialband earned 250 total points
ID: 39843801
If your ls command is aliased, then precede the command with a backslash to temporarily unalias the -F option it so that you don't display the * after the file name.

This is the same as Tintin's and Sandy's example but with a \ preceding the ls command.

\ls *.jpg | grep -v _ >/tmp/filelist
cat /tmp/filelist  | xargs -I{} rm -rf "{}"
0
 

Author Comment

by:compcreate
ID: 39844217
THAT DID IT!  Thanks!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to learn Linux? 10 59
windows 7 starter missing password 21 74
winscp 000webhost.com 6 43
Using Linux to replace Windows Server 2008 R2 for network drives 5 63
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now