LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

Rep: Reputation: Disabled
Command line help


need help with specifically the Delete command.

Sinero

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
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
do you know how does the for loop work?
 
Old 09-10-2015, 08:33 AM   #3
marius162
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

Code:
#!/bin/bash

PATH_DESKTOPFOLDER=...whichever path
#possibly $1
PATH_TO_FILES2BESORTED=...

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

cd $PATH_TO_FILES2BESORTED

#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
do
  #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
  SZEXT=${FILE##*.}

  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
done

#now move everything that is left to the desktop folder
mv * $PATH_DESKTOPFOLDER

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

Rep: Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348
Perhaps this command will generate a list of the image files that you could process.
Code:
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
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,476

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
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:
Code:
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:
Code:
cat copyem
#!/bin/sh
#	make a directory to copy these file to
DIR=some_name
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
do
	cp ${FILE} ${DIR}
done
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:
Code:
... -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
Code:
chmod 755 copyem     (you only do this once)
the run it
Code:
./copyem
Check that you got what you expected to (and fix whatever you may have missed in the script and re run it).
Code:
ls some_name
Finally blow away the tree
Code:
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.
 
  


Reply


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


All times are GMT -5. The time now is 03:20 AM.

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