Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to get directory names in a list and perform rm safely using shell

Posted on 2016-09-15
7
Medium Priority
?
67 Views
Last Modified: 2016-09-28
Hi,
would request your advise on this pls

I have a couple of directories with bunch of sub directories in date format

eg.
directory name :
/u01/app/date_list/2016-09-10
/u01/app/date_list/2016-09-11
/u01/app/date_list/2016-09-12
/u01/app/date_list/2016-09-13
/u01/app/date_list/2016-09-14
/u01/app/date_list/2016-09-15

would like to delete some directories by accepting date range in comma separated and delete only those directories.
e.g 2016-09-11,2016-09-13,2016-09-15

so outcome after delete would be
/u01/app/date_list/2016-09-10
/u01/app/date_list/2016-09-12
/u01/app/date_list/2016-09-14

I can hard code the base directory to be safe e.g

date_range=$1
base_dir=/u01/app/date_list
 for currentDt in $(echo ${date_range} | sed "s/,/ /g")
do
	if ([ "${currentDt}" != "" ] || [ "${currentDt}" != "*" ]); then
		# is this enough? or can we add some more validation ?
		# rm -Rf  ${base_dir}/${currentDt}
	fi
done

Open in new window



pls help is there any strong validation that I can perform. or a better way to handle this.

thanks in advance
0
Comment
Question by:enthuguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 16

Expert Comment

by:Joseph Gan
ID: 41800731
You can use "rsync" to do this if you like:

Similar to this https://www.experts-exchange.com/questions/28963539/Unix-How-to-Bulk-Remove-Multiple-Directories-from-FileSystem.html


mkdir empty_dir

for date in 2016-09-11 2016-09-13 2016-09-15 ...

do

rsync -a --delete empty_dir /u01/app/date_pist/$date

done
2
 
LVL 51

Expert Comment

by:Steve Bink
ID: 41800784
Three points for you:

1) Your logic is flawed - you need use '&&' instead of '||'
2) You can use the -d test to ensure you are targeting a directory.
3) Instead of piping your input through sed, use the variable substitution ${var//Pattern/Replacement}, such as ${currentDt//,/ }

Also, instead of sed/substitution, you could just create a loop to read $1, act on it, then shift to the next entry.  This would require you to use a space delimiter on the command line, but should otherwise work the same.
0
 
LVL 15

Assisted Solution

by:Insoftservice
Insoftservice earned 1000 total points
ID: 41803278
just provide o/p of this code . will provide u code to delete folders too
#!/bin/bash

$MYDIR="/u01/app/date_list"

DIRS=`ls -l $MYDIR | egrep '^d' | awk '{print $8}'`
# and now loop through the directories:
for DIR in $DIRS
do
echo  ${DIR}
done

Open in new window

1
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 51

Assisted Solution

by:Steve Bink
Steve Bink earned 1000 total points
ID: 41803342
On further reflection, I thought I'd offer one more piece of advice.  If you feel this script may be subject to abuse, you may want to include logic to check/remove any instances of '..' in the paths passed from the command line.  Even if you're hard-coding the base dir, some bad things can happen if a user can crawl through parent directories.
1
 

Accepted Solution

by:
enthuguy earned 0 total points
ID: 41813387
thanks, tried this way with all your input

#Local variables
base_dir=/oracle/data/build
current_dir=`pwd`
NONE='\033[00m'
RED='\033[01;31m'
GREEN='\033[01;32m'
YELLOW='\033[01;33m'
BOLD='\033[1m'
BLUE='\033[01;34m'

if [ "${R16_3}" == "true" ]; then
# Trim blank spaces
  date_range=`echo ${R16_3_DATES//[[:blank:]]/}`
  if ([ ! -z "${date_range}" ] || [ "${date_range}" != "*" ]) || [ "${date_range}" == "" ]
  then
    for currentDt in $(echo ${date_range} | sed "s/,/ /g")
    do
      if [ "`date '+%Y-%m-%d' -d ${currentDt} 2>/dev/null`" = "${currentDt}" ]
      then
        echo -e "${YELLOW}${BOLD}rm -Rf  ${base_dir}/R16_3/${currentDt}${NONE}"
        if [ -d "${base_dir}/R16_3/${currentDt}" ]; then
           rm -Rf  "${base_dir}/R16_3/${currentDt}"
        fi
      else
        echo -e "${RED}${BOLD}Invalid date : ${base_dir}/${currentDt}. Pls ensure you provide valid date.${NONE}"
      fi
    done
  else
    echo -e "${RED}${BOLD}Invalid date : ${base_dir}/${date_range}. Pls ensure you provide valid date.${NONE}"
  fi
fi

Open in new window

0
 

Author Comment

by:enthuguy
ID: 41813389
thanks al
0
 

Author Closing Comment

by:enthuguy
ID: 41819442
Worked well
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses

730 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