Deleting all but JPG files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Deleting all but JPG files
# 1  
Old 07-02-2010
Deleting all but JPG files

I would like to delete all files but keep all the .JPG files.
How can this be accomplished?
Thanks in advance
# 2  
Old 07-02-2010
Code:
find . -type f ! -name "*.JPG" | xargs rm

(this changes if your files may be called .jpg, .Jpg, etc.)

. is the current directory. Be mindful of who you're logged in as, and where you are when you run it!

(and if you're using Linux, some dumb admin probably switched on the option to ask you "Delete file...", in which case use xargs rm -f Smilie)
# 3  
Old 07-02-2010
Scott

Thanks! It works great!
One more question how can I modify the code to dellete all files but keeping the ones that DO NOT have any file extension?
Thanks one more time.
# 4  
Old 07-02-2010
Code:
find . -type f -name "*.*" | xargs rm

This might work Smilie

---------- Post updated at 04:39 PM ---------- Previous update was at 04:34 PM ----------

Quote:
Originally Posted by scottn
You mean?

Code:
find . -type f ! -name "*.*" | xargs rm

Nop, I mean what I wrote Smilie. OP wants to delete all files that have extension.
This User Gave Thanks to bartus11 For This Post:
# 5  
Old 07-02-2010
Yes... one too many negatives for my brain at this late hour... you're right Smilie
# 6  
Old 07-02-2010
bartus11

Thank you very much! However, the code does not work if the file name contains spaces like this
Quote:
1 - Copy.jpg
1 Copy.jpg
And this is the message I get
Code:
rm: cannot remove `-': No such file or directory
rm: cannot remove `Copy.fas': No such file or directory
rm: cannot remove `./1': No such file or directory
rm: cannot remove `Copy.fas': No such file or directory

It will also delete files that are named as number with no file extension.
Thanks for your help!
# 7  
Old 07-02-2010
Hi.

Change the xargs, slightly:

Code:
xargs -I{} rm {}

This User Gave Thanks to Scott For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

2. Shell Programming and Scripting

copying random Jpg files to different folder

Hi, I have 200 pictures in a folder and I would like move 10 random pictures every week to given folder automatically. I have this server on 1and1.com. So I tried the following using Bash script for manual copy and paste for testing #!/bin/bash mapfile -t -n 3 files < <(find... (13 Replies)
Discussion started by: raamkum
13 Replies

3. Shell Programming and Scripting

Deleting files

Hi all, I have developed a shell script to copy the files from source to destination and simultaneously to delete the copied files in source. I can copy the files but the files cannot be deleted in source side. (3 Replies)
Discussion started by: Venkatesan
3 Replies

4. Shell Programming and Scripting

AIX system.... deleting files in remote directory after retrieving files

Hi Friends, I am new to this , I am working on AIX system and my scenario is to retrive the files from remote system and remove the files from the remote system after retreving files. I can able to retrieve the files but Can't remove files in remote system. Please check my code and help me out... (3 Replies)
Discussion started by: vinayparakala
3 Replies

5. UNIX for Dummies Questions & Answers

deleting files

:confused: hi all, I need to delete all the files from a archieve directory whose filename starts with 2008, 2009. The folder consists of 2008, 2009, 2010 and 2011. the filename example is as below: 20081111_12_asc_ac_st.zip similarly there are files for 2009. There are around... (2 Replies)
Discussion started by: abhi_123
2 Replies

6. Shell Programming and Scripting

Need help comparing two files and deleting some things in those files!

So I have two files: File1 pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2 pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2 ref4948 1.1 treehouse.txt 1.6 ref8573 1.5 ref3284 1.4 ref5838... (24 Replies)
Discussion started by: linuxkid
24 Replies

7. Shell Programming and Scripting

Rename all ".JPG" files to ".jpg" under all subfolders...

Hi, Dear all: One question ! ^_^ I'm using bash under Ubuntu 9.10. My question is not to rename all ".JPG" files to ".jpg" in a single folder, but to rename all ".JPG" files to ".jpg" in all subfolders. To rename all ".JPG" to ".jpg" in a single folder, for x in *.JPG; do mv "$x"... (7 Replies)
Discussion started by: jiapei100
7 Replies

8. UNIX for Dummies Questions & Answers

Delete files Recursively *thumbs*.jpg

Greetings, I need to delete all files that contain the word thumbs. Those files are spread all throughout sub-directories in a file directory tree. Is there a script or single line command that will find all files with the word thumbs, and simply delete the file? For example: Delete... (4 Replies)
Discussion started by: ..Chris..
4 Replies

9. UNIX for Dummies Questions & Answers

df -k and deleting files

hi everybody, urgently need solutioin aftet i execute the command df -k, i get to see al the memory status blah blah if some file system has 95% full then what should i do and any help on how and what to do ? help really appriciated. cheers (4 Replies)
Discussion started by: ajayr111
4 Replies

10. Shell Programming and Scripting

Deleting old files

Hi, I have a directory which contains files.This Directory keeps getting in new files from time to time.I want to maintain only 15 files in that directory at any time and the old files should be deleted. Eg: Directory 'c' @'a/b/c contains: 1_a 2_a 3_a... I want to delete all the old... (2 Replies)
Discussion started by: shiroh_1982
2 Replies
Login or Register to Ask a Question