Shell script to delete specify directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to delete specify directory
# 8  
Old 09-22-2014
The script I gave you takes a list of directories to rename as operands; not the name of a file containing a list of directories to rename.

Didn't the error message it gave you saying that renamebuild was not found give you a hint?

And, as the comments in the code I provided clearly states, it will not work on directories with leading 0's in their names. (Doing arithmetic on arbitrary strings of digits instead of decimal numbers leads to ambiguities that will cause my code to fail.) Furthermore, this code is not expecting (and will not correctly handle) attempts to rename 111 to tested-111 or other directories if the target directory name already exists.

Since your new requirements do not match your original requirements and the sample data you provided, feel free to use the code I provided as a template you can use as a start to get working code for your new requirements. As it stands, it clearly is unsuited to do what you want (even though it does everything you originally asked for).
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 09-24-2014
Question

Thanks a ton Don Cragun and the scripting is working fine.

---------- Post updated 09-24-14 at 02:07 PM ---------- Previous update was 09-23-14 at 07:23 PM ----------

Hi Don Cragun,

One more query on your script please i.e. from below output, when I input 103, the script renames 103 to tested-103 and keeps 102 & 104.
Now, I would like to append an '0' (zero) before the directories that I am keeping (in this e.g. 102 & 104) i.e. new names should be 0102 & 0104.

I tried to modify the script and it doesn't work for me and I don't want to mess-up the script Smilie. Please advise.

Code:
bash-4.1$ ./cleanup.sh 103
awk: cmd. line:22: warning: escape sequence `\$' treated as plain `$'
+ rm -rf 101
+ printf 'keeping 102\n'
keeping 102
+ mv 103 tested-103
+ printf 'keeping 104\n'
keeping 104
+ rm -rf 105
+ rm -rf 106
+ rm -rf 107
+ rm -rf 108
+ rm -rf 109
+ rm -rf 110
+ rm -rf 111
+ rm -rf 112
+ rm -rf 113
+ rm -rf 114

# 10  
Old 09-24-2014
Quote:
Originally Posted by ibad_urs
... ... ...

Hi Don Cragun,

One more query on your script please i.e. from below output, when I input 103, the script renames 103 to tested-103 and keeps 102 & 104.
Now, I would like to append an '0' (zero) before the directories that I am keeping (in this e.g. 102 & 104) i.e. new names should be 0102 & 0104.

I tried to modify the script and it doesn't work for me and I don't want to mess-up the script Smilie. Please advise.

Code:
bash-4.1$ ./cleanup.sh 103
awk: cmd. line:22: warning: escape sequence `\$' treated as plain `$'
+ rm -rf 101
+ printf 'keeping 102\n'
keeping 102
+ mv 103 tested-103
+ printf 'keeping 104\n'
keeping 104
+ rm -rf 105
+ rm -rf 106
+ rm -rf 107
+ rm -rf 108
+ rm -rf 109
+ rm -rf 110
+ rm -rf 111
+ rm -rf 112
+ rm -rf 113
+ rm -rf 114

Make a backup copy of the working script in case your changes don't work; but don't be afraid to experiment with your code. If you're worried about messing up your directory hierarchy, remove the | bash -x at the end of the script while you're testing so you can just see the commands that it plans to execute instead of actually running them. Please show us what you tried. (And I'll see if I can figure out why you're getting a warning on line 22 in the awk script that I'm not getting when I run it.)

I assume that you do realize that after you change a directory's name from 102 to 0102, the script as it is currently written cannot then later be used to move 0102 to tested-0102 and any directory with a name starting with "0" will never be considered for automatic removal during a later run.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 09-30-2014
Hi Don Cragun,

Please find the console output (syntax errors as I have not put the code in right way ), when I tried to rename the directories that I am keeping (keep_above1 and keep_below1).
Your assumation is correct i.e. after I change a directory's name from 102 to 0102, I am not going to rename these dir. to tested-0102 (it always be 0102) and won't be deleting.
(I tried to add a '0'(zero) before the directories that i am going to keep.
Please help me in appending a '0' before the directories that I am keeping (I am a new baby to shell scripting and learning the concepts now).
Code:
bash-4.1$ ./cleanup.sh 106
awk: cmd. line:17:         else    printf("rm -rf \"%s\"\n", $1)
awk: cmd. line:17:         ^ syntax error
awk: cmd. line:23: warning: escape sequence `\$' treated as plain `$'
bash-4.1$ vim cleanup.sh
bash-4.1$ vim cleanup.sh
bash-4.1$ ^C
bash-4.1$ vim cleanup.sh
bash-4.1$
bash-4.1$
bash-4.1$ ./cleanup.sh 106
awk: cmd. line:16:         else    printf("rm -rf \"%s\"\n", $1)
awk: cmd. line:16:         ^ syntax error
awk: cmd. line:22: warning: escape sequence `\$' treated as plain `$'
bash-4.1$ vim cleanup.sh
bash-4.1$
bash-4.1$
bash-4.1$ ./cleanup.sh 106
awk: cmd. line:14:                 printf("printf \"keeping mv %s 0%s, $1)
awk: cmd. line:14:                        ^ unterminated string
bash-4.1$ vim cleanup.sh
bash-4.1$
bash-4.1$
bash-4.1$ ./cleanup.sh 106
awk: cmd. line:14:                 printf("printf \"mv \"%s\" \" 0%s\", $1)
awk: cmd. line:14:                        ^ unterminated string
bash-4.1$ vim cleanup.sh
bash-4.1$
bash-4.1$
bash-4.1$
bash-4.1$ ./cleanup.sh 106
awk: cmd. line:14:                 printf("mv \"%s\" \" \"0%s\", $1)
awk: cmd. line:14:                        ^ unterminated string
bash-4.1$ vim cleanup.sh
bash-4.1$
bash-4.1$
bash-4.1$ ./cleanup.sh 106
awk: cmd. line:14:                 printf("mv \"%s\" \"0%s\", $1)
awk: cmd. line:14:                        ^ unterminated string
bash-4.1$ vim cleanup.sh
bash-4.1$
bash-4.1$
bash-4.1$
bash-4.1$ ./cleanup.sh 106
awk: cmd. line:14:                 printf("printf \"mv \"%s\" \"0%s\", $1)
awk: cmd. line:14:                        ^ unterminated string
bash-4.1$ vim cleanup.sh
bash-4.1$ ^C
bash-4.1$

# 12  
Old 09-30-2014
You have shown us the errors you got while running your updated script. Please show us the script itself.
# 13  
Old 09-30-2014
Here is the

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 \"mv \"%s\" \"0%s\", $1)
                printf("printf \"mv \"%s\" \"0%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")
}'

# 14  
Old 09-30-2014
Try:
Code:
#!/bin/bash
keep_above=1
keep_below=1
# cleanup - rename selected directories and remove oethers
# 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)
                printf("mv \"%s\" \"0%s\"\n", $1, $1)
        } else  printf("rm -rf \"%s\"\n", $1)
}
END {   for(i in rename) {
                printf("printf \"File \\\"%s\\\" not found.\\n\" >&2\n", i)
                printf("ec=2\n")
        }
        printf("exit $ec\n")
}' | bash

This User Gave Thanks to Don Cragun For This Post:
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