Solved

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

Posted on 2016-09-15
7
54 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 50

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 250 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 50

Assisted Solution

by:Steve Bink
Steve Bink earned 250 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSH in linux 9 94
Certificate Request CentOS/Apache 1 59
Linux recover lost file deleted by mv 1 32
Example code 13 45
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

710 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