Shell script to delete specify directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to delete specify directory
# 1  
Old 09-19-2014
Shell script to delete specify directory

I have the following directories under "0.0.0.0"
Code:
/scratch/builds/snapshotBuilds/0.0.0.0
drwxr-xr-x+  tested-571
drwxr-xr-x+  tested-576
drwxr-xr-x+  tested-597
drwxr-xr-x+  600
drwxr-xr-x+  601
drwxr-xr-x+  602
drwxr-xr-x+  603
drwxr-xr-x+  tested-604
drwxr-xr-x+  tested-605
drwxr-xr-x+  tested-606
drwxr-xr-x+  tested-608
drwxr-xr-x+  613
drwxr-xr-x+  tested-614
drwxr-xr-x+  617
drwxr-xr-x+  618
drwxr-xr-x+  619
drwxr-xr-x+  620
drwxr-xr-x+  621
drwxr-xr-x+  622
drwxr-xr-x+  623
drwxr-xr-x+  624
drwxr-xr-x+  631
drwxr-xr-x+  632
drwxr-xr-x+  635

And Here is my requirement, please help me to write a shell script i.e.
from the above list, i want to rename directory 621 to tested-621 and the shell script should KEEP (dont not delete) builds 620, tested-621 and 622 and delete rest of the directories before 620 and after 621 (tested-621). Please note, I want to keep all the directories that are starting with tested-xxxx

Could you please help?

---------- Post updated at 12:47 PM ---------- Previous update was at 12:35 PM ----------

I found the following command in the forum:
Code:
find . -type d | awk '!/\.\/tested*/' | xargs -p rm -rf

But this will keep all the directories that are starting with tested* and delete rest all the directories. But, i want to keep all the tested* directories plus keep build+1 and build -1. For example, under "0.0.0.0" directory say build 50 is renamed to tested-50 then Your script should KEEP build 49, tested-50, 51.

Last edited by vbe; 09-19-2014 at 07:32 AM..
# 2  
Old 09-19-2014
Hello,

Welcome to forum, please use code tags for commands and codes as per forum rules. Following may help you.

Code:
find . -type d | awk '!/\.\/tested*/ !/ && !/\.\/build*/'

You can use above command to get list of directories which are NOT started with tested and build and if you are fine with results then you can use following(As I haven't tested the command).

Code:
find . -type d | awk '!/\.\/tested*/ !/ && !/\.\/build*/' | xargs -p rm -rf

Thanks,
R. Singh
# 3  
Old 09-19-2014
RE: Shell script to delete specify directory

Thank you Singh,

Slight modification in your command:

Code:
find . -type d | awk '!/\.\/tested*/  && !/\.\/111/' | xargs -p rm -rf:


This is working but a small issue is the above command is trying to delete '.' directory under the path.
For e.g. Plz refer the above command output as below and advice, how to exclude the '.' directory also


Code:
bash-4.1$ find . -type d | awk '!/\.\/tested*/  && !/\.\/111/' | xargs -p rm -rf
rm -rf . ./234 ./4445 ./222 ./321 ?...Y
rm: cannot remove directory: `.'

Thanks in advance,
Ibadh.
# 4  
Old 09-19-2014
Hello,

You can use the following command for same.

Code:
find . -type d | awk '!/\.\/tested*/  && !/\.\/111/ && !/^\.$/' | xargs -p rm -rf

Please take list of all directories first by find . -type d | awk '!/\.\/tested*/ && !/\.\/111/ && !/^\.$/' and then if you are getting fine result you can use the command to delete those.


Thanks,
R. Singh
# 5  
Old 09-19-2014
Hi Singh,

Thanks alot for the help.
what if the requirement is like below please?

Say from below list:
Code:
/scratch/builds/snapshotBuilds/0.0.0.0
drwxr-xr-x+  tested-571
drwxr-xr-x+  tested-576
drwxr-xr-x+  tested-597
drwxr-xr-x+  600
drwxr-xr-x+  601
drwxr-xr-x+  602
drwxr-xr-x+  603
drwxr-xr-x+  604
drwxr-xr-x+  605
drwxr-xr-x+  606
drwxr-xr-x+  608
drwxr-xr-x+  613
drwxr-xr-x+  614
drwxr-xr-x+  617
drwxr-xr-x+  618
drwxr-xr-x+  619
drwxr-xr-x+  620
drwxr-xr-x+  621
drwxr-xr-x+  622
drwxr-xr-x+  623
drwxr-xr-x+  624
drwxr-xr-x+  631
drwxr-xr-x+  632
drwxr-xr-x+  635

1. I want to rename 603, 614 and 622 to tested-603, tested-614 and tested-622
2. Don't want to delete +1 & -1 of the above build numbers (like 602 & 604, 613 & 615 and 621 & 623)
3. delete the remaining directories

I think we need a for loop which reads the build numbers at runtime/we pass thru variables
a. that has to be renamed those 3 build numbers and keep them safe
b. Keep +1 and -1 of the build numbers safe

Please let me know if the requirement is not clear?

I think, we need to write some logic with for loop, can you please advise as I am new to this scripting plz?

Thanks,
Ibadh.
# 6  
Old 09-19-2014
Using find the way you are here is very dangerous if there are any directories in any of the directories in the current directory. Furthermore, listing explicit files that are not to be processed (instead of files that are to be processed) increases the possibility that you will accidentally remove files unintentionally.

Although I could do this entirely in bash (which seems to the be shell you're using), I find the logic easier to think about with a mix of shell and awk since arrays in awk are more versatile than bash arrays.

The following shell script runs an awk script that creates shell commands to rename and remove the files (any file type including directories) and then feeds those shell commands into another shell. (I STRONGLY suggest that you initially remove the code shown in red so the script will just show you the shell commands it plans to execute. If the results look like what you want to do, put the code back in and rerun the script with the same operands to actually do the work.)
Code:
#!/bin/bash
keep_above=1
keep_below=1
# cleanup - rename selected files and remove others
# Usage: cleanup file_to_rename...
#
# Select a list of all files in the current directory whose names start and
# end with a decimal digit (files with a leading zero in their name will be
# ignored).  Rename each file named as an operand by prefixing it with
# "tested-".  Leave other files in the range:
#    (file_to_rename - $keep_below) <= name <= (file_to_rename + $keep_above)
# for any given operand unchanged.  Remove all other selected files.

IAm=${0##*/}
if [ $# -lt 1 ]
then    printf 'Usage: %s file_to_move...\n' "$IAm" >&2
        exit 1
fi
printf '%s\n' "$@" "EndOfRenameList" [1-9]*[0-9] |
awk -v above="$keep_above" -v below="$keep_below" '
/EndOfRenameList/ {
        eval=1
        next
}
!eval { for(i = $1 - below; i <= ($1 + above); i++)
                keep[i]
        rename[$1]
        next
}
{       if($1 in rename) {
                printf("mv \"%s\" \"tested-%s\"\n", $1, $1)
                delete rename[$1]
        } else if($1 in keep)
                printf("printf \"keeping %s\\n\"\n", $1)
        else    printf("rm -rf \"%s\"\n", $1)
}
END {   for(i in rename) {
                printf("printf \"File \\\"%s\\\" not found.\\n\" >&2\n", i)
                printf("printf \"ec=2\\n\"\n")
        }
        printf("exit \$ec\n")
}' | bash -x

You haven't said what OS you're using; if you want to try this on a Solaris/SunOS system, change awk in the script to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
# 7  
Old 09-22-2014
Don Cragun,

Thanks for the help, but your code is removing all directories Smilie
Please find the output and FYI, I am unsing Linux 6, please advise.
Code:
bash-4.1$ ls -lrt
total 64
-rw-r--r-- 1 ibshaik dba   51 Sep 22 02:11 devicesinfo
-rw-r--r-- 1 ibshaik dba   12 Sep 22 02:26 renamebuild
-rwxr-xr-x 1 ibshaik dba 1369 Sep 22 02:27 cleanup.sh
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 02:28 tested-111
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:24 555
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:24 444
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:24 411
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:24 333
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:24 311
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:24 222
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:24 211
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:24 121
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:24 111
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:25 0456
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:25 0234
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:25 0111
bash-4.1$
bash-4.1$ cat renamebuild
111 222 333
bash-4.1$ ./cleanup.sh renamebuild
awk: cmd. line:21: warning: escape sequence `\$' treated as plain `$'
+ rm -rf 111
+ rm -rf 121
+ rm -rf 211
+ rm -rf 222
+ rm -rf 311
+ rm -rf 333
+ rm -rf 411
+ rm -rf 444
+ rm -rf 555
+ printf 'File "renamebuild" not found.\n'
File "renamebuild" not found.
+ printf 'ec=2\n'
ec=2
+ exit
bash-4.1$ ls -lrt
total 28
-rw-r--r-- 1 ibshaik dba   51 Sep 22 02:11 devicesinfo
-rw-r--r-- 1 ibshaik dba   12 Sep 22 02:26 renamebuild
-rwxr-xr-x 1 ibshaik dba 1369 Sep 22 02:27 cleanup.sh
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 02:28 tested-111
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:25 0456
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:25 0234
drwxr-xr-x 2 ibshaik dba 4096 Sep 22 03:25 0111

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

3. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

4. Shell Programming and Scripting

delete only particular file in directory shell script

Hi, what function do we use to delete only particular file that we want in directory shell script for example I want only to delete test.txt in directory how to do it ? in sh Thank (1 Reply)
Discussion started by: guidely
1 Replies

5. Shell Programming and Scripting

script to delete contents in a directory by date

Hi , I am trying to make a cron job to delete the contents of a directory in a linux environment.The contents created before 2 days should get deleted by this job.Here is what i have written : /usr/bin/find / -name *.log -ctime +2 -exec /bin/rm -f {} \; Is it correct....If not so,please... (9 Replies)
Discussion started by: d8011
9 Replies

6. Shell Programming and Scripting

Script to delete all something.txt~ file from a directory

There are some files in a directory like a.tx~ , b.txt~,c.txt~. I want to delete all these files inside that directory and sub directory. How can i do this? #!/bin/bash cd thatdirectory ...... rm -rf *~ ...... (7 Replies)
Discussion started by: cola
7 Replies

7. Shell Programming and Scripting

Linux delete script in sub-directory

Under TEMP folder, it have many sub-folder, example"0015,0016,etc" I need to discovery those file which are 2 days ago in this sub-folder , and list out to investigate, at the end delete all file in those sub folder, only keep the emptu directory. Thanks (4 Replies)
Discussion started by: android
4 Replies

8. Shell Programming and Scripting

Sheel script to Delete a set of files from a given directory

I have a file <filestodelete> containing names of files to to be deleted from a directory <filesstore>. I want a script file which accptes the <filestodelete> and also the location of the directory(<filestore>) and deletes all files matching. Thanks in Advance.. (3 Replies)
Discussion started by: VardhiniVenkat
3 Replies

9. Shell Programming and Scripting

shell script which will delete every thing under a directory except a single sub dire

write a shell script which will delete every thing under a directory except a single sub directory (2 Replies)
Discussion started by: alokjyotibal
2 Replies
Login or Register to Ask a Question