Help need with remove command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help need with remove command
# 1  
Old 03-16-2013
Help need with remove command

Hi,
Help need with remove commnad

I am using shell script where in I delete old files using the following commands:
Code:
 
rm ${DIR}/${pattern}*

We source the value of ${DIR} and ${pattern} from a config file. In some instances when the config file was missing the above parameters or say sourcing did not happen.
Then ${DIR} and ${pattern} becomes blank and the remove statement was executed as
Code:
 
rm /*

But luckily no damage was done.
I read the documentation and not really sure which one is best for me.

Quote:
${parameter:-word}
If parameter is set and is non-null then substitute its value;
otherwise substitute word.
${parameter:=word}
If parameter is not set or is null then set it to word; the
value of the parameter is then substituted. Positional parame-
ters may not be assigned to in this way.
${parameter:?word}
If parameter is set and is non-null then substitute its value;
otherwise, print word and exit from the shell (if not interac-
tive). If word is omitted then a standard message is printed.
Help is appreciated. I want to use the safest approach as I am afraid to use rm commands.
# 2  
Old 03-16-2013
The safest would be something like:
Code:
rm ${DIR:?'$DIR is not correctly set'}/${pattern:?'$pattern is not correctly set'}*

assuming that something else hasn't been done in your script that needs to be undone if the script dies at this point.
# 3  
Old 03-16-2013
Quote:
Originally Posted by Don Cragun
The safest would be something like:
Code:
rm ${DIR:?'$DIR is not correctly set'}/${pattern:?'$pattern is not correctly set'}*

assuming that something else hasn't been done in your script that needs to be undone if the script dies at this point.

Thank you sir!!

---------- Post updated at 12:58 PM ---------- Previous update was at 12:27 PM ----------

Quote:
Originally Posted by Don Cragun
The safest would be something like:
Code:
rm ${DIR:?'$DIR is not correctly set'}/${pattern:?'$pattern is not correctly set'}*

assuming that something else hasn't been done in your script that needs to be undone if the script dies at this point.
I am planning to use the below code for removing files. I want to echo after all files are deleted. The order in which I delete files or all at onces does not matter.

Please let me know if there is a better way to do this.

Also I would like a safe approach in case the parameter values are blank then it should not wipe out the root directory.


Code:
 
    if `rm ${INPUTDIR:?'$INPUTDIR is not correctly set'}/${PATTERNA:?'$PATTERNA is not correctly set'}*`; then
        if `rm ${INPUTDIR:?'$INPUTDIR is not correctly set'}/${PATTERNB:?'$PATTERNB is not correctly set'}*`; then
            if `rm ${TEMPDIR:?'$TEMPDIR is not correctly set'}/${PATTERNB:?'$PATTERNB is not correctly set'}*`; then
                if `rm ${CONTROLDIR:?'$CONTROLDIR is not correctly set'}/${PATTERNB:?'$PATTERNB is not correctly set'}*.cmd`; then
                    echoen "All Files Deleted Successfully!!"
                fi
            fi
        fi
    fi

# 4  
Old 03-16-2013
Quote:
Originally Posted by pinnacle
Thank you sir!!

---------- Post updated at 12:58 PM ---------- Previous update was at 12:27 PM ----------



I am planning to use the below code for removing files. I want to echo after all files are deleted. The order in which I delete files or all at onces does not matter.

Please let me know if there is a better way to do this.

Also I would like a safe approach in case the parameter values are blank then it should not wipe out the root directory.


Code:
 
    if `rm ${INPUTDIR:?'$INPUTDIR is not correctly set'}/${PATTERNA:?'$PATTERNA is not correctly set'}*`; then
        if `rm ${INPUTDIR:?'$INPUTDIR is not correctly set'}/${PATTERNB:?'$PATTERNB is not correctly set'}*`; then
            if `rm ${TEMPDIR:?'$TEMPDIR is not correctly set'}/${PATTERNB:?'$PATTERNB is not correctly set'}*`; then
                if `rm ${CONTROLDIR:?'$CONTROLDIR is not correctly set'}/${PATTERNB:?'$PATTERNB is not correctly set'}*.cmd`; then
                    echoen "All Files Deleted Successfully!!"
                fi
            fi
        fi
    fi

This is overkill. You only need to use ${x:?'error msg'} once for each different $x (the first time it is referenced). And, you won't be able to list the filenames after they have been removed. And, I've never seen an echoen command.

You could try something like the following to list the files to be removed and then remove them. Don't try this if any of your filenames could contain whitespace characters! If there is a chance that any filename you're trying to remove will contain one or more whitespace characters, you need to use more complicated code.
Code:
    echo 'Preparing to remove the following files:'
    ec=1
    printf "\t%s\n" ${INPUTDIR:?'$INPUTDIR is not correctly set'}/${PATTERNA:?'$PATTERNA is not correctly set'}*
    if $(rm $INPUTDIR/$PATTERNA*); then
        printf "\t%s\n" $INPUTDIR/${PATTERNB:?'$PATTERNB is not correctly set'}*
        if $(rm $INPUTDIR/$PATTERNB*); then
            printf "\t%s\n" ${TEMPDIR:?'$TEMPDIR is not correctly set'}/$PATTERNB*
            if $(rm $TEMPDIR/$PATTERNB*); then
                printf "\t%s\n" ${CONTROLDIR:?'$CONTROLDIR is not correctly set'}/$PATTERNB*.cmd
                if $(rm $CONTROLDIR/$PATTERNB*.cmd); then
                    echo 'All Files Deleted Successfully!!'
                    ec=0
                fi
            fi
        fi
    fi
    if [ $ec -ne 0 ]; then
        echo 'Some Files Were Not Deleted!!'
    fi

Note, this code has not been tested. I strongly suggest changing the rms in this script to echos until you verify that it will do what you're trying to do.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 03-16-2013
Thanks I was not aware that
Code:
printf pattern*

and
Code:
echo pattern*

will list files.

I thought
Code:
ls pattern*

was only command to list files. Thanks for the information.

When I just do

Code:
 
printf pattern*

It lists only one file and then the command prompt also comes on same line.

Whereas when I do

Code:
 
echo pattern*

It list all files and command prompt comes on next line.

Could you please let me know the following:
1. When should we use
Code:
printf or echo

command instead of ls command.
2. Why the printf command I showed above list only one file. Where as
Code:
 
printf "\t%s\n" pattern*

list all files.
3. I am not expecting any blank space in the file name that needs to be removed but incase blank space is part of the file name. Will it just not delete that file or will it mess up the server files in root directory or any other directories.

I appreciate all your help and explaination.
# 6  
Old 03-16-2013
Quote:
Originally Posted by pinnacle
Thanks I was not aware that
Code:
printf pattern*

and
Code:
echo pattern*

will list files.

I thought
Code:
ls pattern*

was only command to list files. Thanks for the information.

When I just do

Code:
 
printf pattern*

It lists only one file and then the command prompt also comes on same line.

Whereas when I do

Code:
 
echo pattern*

It list all files and command prompt comes on next line.

Could you please let me know the following:
1. When should we use
Code:
printf or echo

command instead of ls command.
Note that in all of these cases, the shell (not echo, ls, or printf) is expanding the pattern to produce a list of files. Which command you should use to process that list of files depends on what you want to do with the files in that list.

Note that there are at least three different versions of echo: AT&T's UNIX System V echo, University of California's Berkeley Software Distributions's (BSD's) echo, and the Linux GNU echo each of which have different options and each of which handle backslash characters in operands differently. If you want to write portable code, never use echo if the first character of the first operand might be a minus sign character or if any character in any operand might be a backslash character. (And you can't use -- as a first operand to skip over options and treat them as operands because some versions of echo will print the -- instead of ignoring it.) Use ls if some of the files given to it might be directories and you want ls to also list the files in the directories named by that operand, but note that the format of the output of ls (when no options are specified and the 1st operand does not begin with a minus-sign character) may differ if its standard output is directed to a terminal than if its standard output is directed to a pipe or a regular file. Use printf (with a first operand that specifies the format you want used to display any remaining operands) if you want to specify the output format and want the output to be portable to any operating system.
Quote:
Originally Posted by pinnacle
2. Why the printf command I showed above list only one file. Where as
Code:
 
printf "\t%s\n" pattern*

list all files.
The first operand to printf is always interpreted as a format string to control the display of any remaining operands. If the first operand does not contain any percent-sign characters, remaining operands will be ignored.

When the first operand is "\t%s\n" or '\t%s\n', each remaining operand will be printed as a string (%s) following a tab character (\t) and followed by a newline character (\n). If the first operand was '\t%s\t%s\t%s\t%s\n', up to four following operands will be printed each with a leading tab character and with the last one followed by a newline character. (If the number of remaining operands is not an even multiple of 4, the final unmatched %s format specifiers will act as if empty strings were supplied to be printed.)

When you didn't supply a format string as a first operand to printf, the first file operand you you gave it was interpreted as a format string. If it didn't contain any percent-sign characters followed by other characters that printf could interpret as a valid format specifier, remaining operands would be ignored.
Quote:
Originally Posted by pinnacle
3. I am not expecting any blank space in the file name that needs to be removed but incase blank space is part of the file name. Will it just not delete that file or will it mess up the server files in root directory or any other directories.
If any of your filenames contain space, tab, or newline characters; files you want to remove will not be removed, files you didn't want to remove may be removed instead, and other commands could be executed.
Quote:
Originally Posted by pinnacle
I appreciate all your help and explaination.
I hope this helps,
Don
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 03-18-2013
Code:
    echo 'Preparing to remove the following files:'
    ec=1
    printf "\t%s\n" ${INPUTDIR:?'$INPUTDIR is not correctly set'}/${PATTERNA:?'$PATTERNA is not correctly set'}*
    if $(rm $INPUTDIR/$PATTERNA*); then
        printf "\t%s\n" $INPUTDIR/${PATTERNB:?'$PATTERNB is not correctly set'}*
        if $(rm $INPUTDIR/$PATTERNB*); then
            printf "\t%s\n" ${TEMPDIR:?'$TEMPDIR is not correctly set'}/$PATTERNB*
            if $(rm $TEMPDIR/$PATTERNB*); then
                printf "\t%s\n" ${CONTROLDIR:?'$CONTROLDIR is not correctly set'}/$PATTERNB*.cmd
                if $(rm $CONTROLDIR/$PATTERNB*.cmd); then
                    echo 'All Files Deleted Successfully!!'
                    ec=0
                fi
            fi
        fi
    fi
    if [ $ec -ne 0 ]; then
        echo 'Some Files Were Not Deleted!!'
    fi

The above code is failing when it finds no files with the specified pattern.
I used
Code:
rm -f

in that case the variable check we are doing to check if rm was successfull will not be any longer useful since
Code:
 rm -f

will always return 0.

Is there any other way to do this ?
And also if time permits could you help me with code to delete files that could have space in their name.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can not remove file using rm command

I have two questions: the first is I have a line of code: printf "What is the id of the patient getting GJB2 analysis : "; read id that stores a user input in a variable $id in the python directory c:/Users/cmccabe/Desktop/Python27/$id.txt Using rm I get the error cannot remove ... (21 Replies)
Discussion started by: cmccabe
21 Replies

2. UNIX for Dummies Questions & Answers

cut or remove command

hi, I want to remove first word in all files where first and second word is seperated by comma. example: a file contains Apple,mango. i need only mango shoule be there in the file??? Apple, should be removed!!!!! can anyone help me out??? thanks in advance, Arun Manas:b: (2 Replies)
Discussion started by: arunmanas
2 Replies

3. Shell Programming and Scripting

simple remove command -ksh

KSH A simple remove command is not workin for me. The user has all the access and the return code after remove command is 0. But I am still able to find the file. I have the code as below. tmplog=/tmp/$fl_nm.$$ main () { --set of statments rm -fr $tmplog RC=$? } (2 Replies)
Discussion started by: tostay2003
2 Replies

4. AIX

Unable to remove files from rm command

I need to delete some files which are not getting removed not even from root user -rw-r----- 1 ptml tellin 0 Jul 26 19:23 temp.out1 -rw-r----- 1 ptml tellin 0 Jul 26 19:23 temp.out -rw-r----- 1 ptml tellin 2 Jul 26 19:23 prev -rw-r----- 1... (4 Replies)
Discussion started by: m_usmanayub
4 Replies

5. UNIX for Dummies Questions & Answers

Command to remove First and Last line from a File

I have a file from which the Header and the Trailer lines need to be removed. They are confirmed to be the first and the last lines in the file. I have tried a few commands, but not successful yet. It needs to be implemented urgently, hence any help is greatly appreciated. Raghu ----------... (1 Reply)
Discussion started by: ragz_82
1 Replies

6. UNIX for Dummies Questions & Answers

inverse remove command

Hi All, Is there an option for 'rm' to "remove everything but these files"? For instance if I have: a.txt <-- I just want to remove this one a_1.txt a_2.txt Is there an option ? for this: rm -? *_*.txt Thanks, ScKaSx (4 Replies)
Discussion started by: ScKaSx
4 Replies

7. UNIX for Dummies Questions & Answers

Find and remove command - can you tell me exactly what its doing?

find /app01/tomcat_local -name *jsp* -type f -exec rm -r {} \; I would assume the above is just deleting any *jsp* below the /app01/tomcat_local directory - is this correct as its seems to delete more than I expect.... (1 Reply)
Discussion started by: frustrated1
1 Replies

8. Shell Programming and Scripting

rm command not able to remove file

I have directory IXNPG7 under which i have seen file ads.c , ads.gif , ads.js and lots more with extension .html I tried to remove the Entire Directory with rm -Rf IXNPG7 but it is saying -- Directory Not empty can't remove Secondly i tried removing all the files first using rm *.*... (7 Replies)
Discussion started by: jambesh
7 Replies

9. UNIX for Dummies Questions & Answers

command to remove last record on file

Hi, First time on the forum. I have converted some files using the Unix to DOS command but need to strip off the last record that is generated from this conversion that contains just a ^Z. Is there any command that would accomplish this without having to do stream editing? (4 Replies)
Discussion started by: mheinen
4 Replies

10. UNIX for Dummies Questions & Answers

search and remove command

I want to run the 'locate' command and then remove what the command finds. My guess was, locate blahblah |rm -f but this does not work. Thanks! (3 Replies)
Discussion started by: jasonr
3 Replies
Login or Register to Ask a Question