delete folders with no mpgs or nuv files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete folders with no mpgs or nuv files
# 1  
Old 11-14-2010
delete folders with no mpgs or nuv files

I need some help writing a simple script that will delete all subfolders that contain no mpg or nuv files, if they have anything else or are empty the folder should be deleted
I got this far but I'm pretty hopeless
Code:
foo=0

if [ -f *.nuv ]
then
 foo=1
fi

if [ -f *.mpg ]
then
 foo=1
fi

if [ $foo == 1 ]
then
 echo found
fi

# 2  
Old 11-14-2010
this code might help:

Code:
 
while read subdir; do
 
foo=0
 
if [ `ls -l $subdir|egrep -c "nuv|mpg"` -gt 0 ]
then
foo=1
fi

if [ $foo -eq 1 ]
then
echo found $subdir
fi

done < file_with_list_of_subdir

# 3  
Old 11-14-2010
I think you have the > or something else is wrong because it just hangs there when run after changing the < into a > and says ./test.sh: line 15: file_with_list_of_subdir: No such file or directory if I leave it as is
# 4  
Old 11-14-2010
You have to create the file named "file_with_list_of_subdir" containing all the names of sub directories. And < is redirection, serves as the input for each iteration of while loop.
# 5  
Old 11-14-2010
bash code:
  1. #!/bin/bash
  2. ls -d */ | while read Dir; do
  3.    if ls -l $Dir | egrep -q 'nuv|mpg'; then
  4.       echo "Files Found in $Dir"
  5.    else
  6.       echo "No files found in $Dir"
  7.    fi
  8. done
# 6  
Old 11-14-2010
use find command..
# 7  
Old 11-14-2010
Quote:
Originally Posted by mrplow
I need some help writing a simple script that will delete all subfolders that contain no mpg or nuv files
If you want to check only the subdirectory(one level) run the following in current directory.
Code:
ls -d */|while read dir;do ls -l $dir | grep -q ".[nuv|mpg]$" || echo rm -r $dir;done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to compare files in 2 folders and delete the large file

Hello, my first thread here. I've been searching and fiddling around for about a week and I cannot find a solution.:confused: I have been converting all of my home videos to HEVC and sometimes the files end up smaller and sometimes they don't. I am currently comparing all the video files... (5 Replies)
Discussion started by: Josh52180
5 Replies

2. Shell Programming and Scripting

How to delete all the files and folders inside all the directories except some specific directory?

hi, i have a requirement to delete all the files from all the directories except some specific directories like archive and log. for example: there are following directories such as A B C D Archive E Log F which contains some sub directories and files. The requirement is to delete all the... (7 Replies)
Discussion started by: Little
7 Replies

3. UNIX for Advanced & Expert Users

Help with creating script to delete log files/folders

Hi I am new to Linux / scripting language. I need to improve our Linux servers at work and looking to claim some space my deleting log files/ folders on a 5 day basis. Can someone help me with creating a script to do so. Any sample script will be helpful.:b: Regards (2 Replies)
Discussion started by: sachinksl
2 Replies

4. Shell Programming and Scripting

Script to delete folders and files from a prompt

Hi Everyone, I work for GE Money IVR as a DB analyst and the environment on which I work is Solaris 5.0 server and Oracle 11g. I got a project in which I have to clean up the folders and files which are not used in DB. I copied an existing script and edited it, dont know this is the... (5 Replies)
Discussion started by: habeeb506
5 Replies

5. UNIX for Dummies Questions & Answers

[SOLVED] Delete files and folders under given directory

I have a requirement to delete the files and folders under a given directory. my directory structure is like this.. Data | A(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files |--files | B(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files ... (1 Reply)
Discussion started by: ramse8pc
1 Replies

6. Shell Programming and Scripting

Loop folders, delete files, copy new ones

Folks, I am hopeful that you may be able to help me out with writing a script that can be run nightly (as cron?) to loop through all subfolders within the "/media" directory, delete all of the files in each of them, and then copy in all of the files from the "/home//sansa" directory to each of... (6 Replies)
Discussion started by: acraig
6 Replies

7. UNIX for Dummies Questions & Answers

Delete folders and files in it - UNIX

I need to delete a folder and files in it of yesterdays and simply put this in cron. Folder name - "2010-03-2010" File name - "eprod_06_23.dmp" and "eprod_06_23.exp" Actually this folder and file is been created by a script which takes a backup of DB everyday.Thats why it creates folder and file... (3 Replies)
Discussion started by: j_panky
3 Replies

8. Shell Programming and Scripting

Compare files in two folders and delete missing ones

I do not know much about shell scripting so I am at a loss here. If someone can help me, that would be great! I have two directories /dir1 /dir2 I need to delete all files from /dir1 and that does not have a correspondent file in /dir2. It should NOT check file suffixes in /dir2 . Why?... (20 Replies)
Discussion started by: kaah
20 Replies

9. Shell Programming and Scripting

delete files and folders older than 3 days

find /basedirectory -type f -mtime +3 >> /tmp/tempfile find /basedirectory -type d -mtime +3 >> /tmp/tempfile mailx -s "List of removed files and folders" myemail@domain.com < /tmp/te mpfile rm /tmp/tempfile find /basedirectory -type f -mtime +3 -exec rm {} \; find /basedirectory -type d... (7 Replies)
Discussion started by: melanie_pfefer
7 Replies

10. Shell Programming and Scripting

delete all folders/files and keep only the last 10 in a folder

Hi, I want to write a script that deletes all folders and keep the last 10 recent folders. I know the following: ls -ltr will sort the folders from old to recent. ls -ltr | awk '{print $9}' will list the folder names (with a blank line at the beginning) I want to get the 10th folder from... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question