Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-10-2015, 08:06 AM   #1
LQ Newbie
Registered: Sep 2015
Posts: 1

Rep: Reputation: Disabled
Command line help

need help with specifically the Delete command.


I have a bunch of folders named recup_dir.183 all the way through recup_dir.517 I need to delete all the files in all of the folders starting with recup_dir. except for video and picture format such as mp4,vob,ogv,drc.gif, .jpeg, .PNG, BMP, RIF, JFIF, .TIFF,Exif, etc. etc. and if possible put all of the picture and video files that were not deleted into a single folder on my desktop.

Any help would be super cool. Doing it folder by folder will take me forever.
Old 09-10-2015, 08:18 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,792

Rep: Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888
do you know how does the for loop work?
Old 09-10-2015, 08:33 AM   #3
LQ Newbie
Registered: Apr 2015
Posts: 11

Rep: Reputation: Disabled
You may want to write a shell script to do that, which would look like


PATH_DESKTOPFOLDER=...whichever path
#possibly $1

#check parameters for validity here and bail out if they are not; we don't want to
#accidentaly execute this script in /usr :-)


#exercise (I'm to lazy to work this out now): find all the recup_dir.* folders
#and cd to them before executing the inner loop which actually sorts the files.

find * | while READ FILE
#delivers one filename after another to $FILE
  #insulate the file extension
  #mind that the file system is probably case sensitive, so either map the filename
  #to lower case before testing the extensions (FILENAME_LOWERCASE=$(echo $FILE | tr A-Z a-z))
  #or write tests for all cases

  if test "$SZEXT" = "mp4" ; then rm $FILE ; fi #could be a good idea to implement a simulation mode
  if test "$SZEXT" = "vob" ; then rm $FILE ; fi #here which only dumps the filenames and what the
                                                #script would do with them. Just to be on the safe side.
  #... and so on

#now move everything that is left to the desktop folder

Last edited by marius162; 09-10-2015 at 08:34 AM.
Old 09-10-2015, 08:38 AM   #4
Senior Member
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,664

Rep: Reputation: 1542Reputation: 1542Reputation: 1542Reputation: 1542Reputation: 1542Reputation: 1542Reputation: 1542Reputation: 1542Reputation: 1542Reputation: 1542Reputation: 1542
Perhaps this command will generate a list of the image files that you could process.
find recup_dir.* -type f -exec file --mime-type '{}'  \; | grep image | cut -d ":" -f1

Last edited by allend; 09-10-2015 at 09:13 AM.
Old 09-10-2015, 09:14 AM   #5
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
This would be something you would do with the find utility.

First off, rather than deleting all but the files you want to keep, simply use find to copy what you want from recup_dir to a new (or different) directory then simply remove the recup_dir with rm -r recup_dir (that's a recursive remove all files and subdirectories)..

That assumes that recup_dir is the parent of all the recup_dir.[number] directories.

How you find what you want to keep is done something like this:
find recup_dir -type f -name *.mp4 -o -name *.vob -o -name *.ogv \
     -o -name *.drc [...]
The -o means or.

You should, with an editor, create a file containing the commands and arguments, let's call it copyem:
cat copyem
#	make a directory to copy these file to
mkdir -p ${DIR}
for FILE in find [path_to]/recup_dir -name *.mp4 -name *.vob -name *.ogv -name *.drc \
	-name *.gif -name *.jpeg -name *.PNG -name *.BMP -name *.RIF \
	-name *.JFIF -name *.TIFF -name *.Exif
	cp ${FILE} ${DIR}
Now, this is not real sophisticated -- you must match file name extensions exactly. So, if you have file extensions with upper case letters (or upper and lower case mixed), you'll need to specify them like this:
... -name *.[BbMmPp] -o -name *.[PpNng]
and so on.

So, you have all the file extensions the way you want them in this file, save it and
chmod 755 copyem     (you only do this once)
the run it
Check that you got what you expected to (and fix whatever you may have missed in the script and re run it).
ls some_name
Finally blow away the tree
rm -r recup_dir
By the way, you do not want to copy all that stuff to your desktop -- copy them to a directory.

Hope this helps some.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cursor does not move to the bottom line and overwrite to same line on command line mesuutt Linux - Newbie 3 02-25-2012 09:04 AM
Print all PID folders from /proc line-by-line with this format (( PID: command-line )) courteous Linux - Newbie 7 12-12-2010 05:47 PM
awk command line: blank line record sep, new line field sep robertmarkbram Programming 4 02-21-2010 06:25 AM
xine command line? or some other command line only engine? lumix Linux - Software 2 08-10-2007 11:46 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:52 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration