2

I am writing a script to remove files from directories (if given). When it is run without command line arguments it works in the current directory. When it is run with at least one command line argument it assumes that the 1st argument points (by relative or absolute pathname) to a directory to work in.

I am wanting to design it where the user would type in the script like this:

./script.sh /home 

where this would change to the home directory.

Here's what I have:

#!/bin/bash

cd $1

I am new to shell scripting so any help is appreciated!

6
  • 1
    Have you tried running this? Aside from recommending that you quote the parameter expansion (cd "$1"), there's not much else to say yet. Commented Apr 26, 2015 at 22:53
  • 1
    Thanks for responding chepner. I have tried running it. When I check the error code it is 0 so it is not failing to change directories. I think it is changing directories in the subshell not the main shell. Commented Apr 26, 2015 at 22:58
  • 1
    That's correct. Each shell has its own working directory. The cd command applies to the shell in which it is executed, in this case a non-interactive shell that executes the script. When the script completes and that shell exists, you are back in the shell from which you executed the script, and that working directory has not changed. Commented Apr 26, 2015 at 23:05
  • 1
    I would be extremely cautious about using a script such as this. It is too dangerous for my taste. There are too many ways to use it with unintended consequences. I'd not use it. I'd rather change to the directory, inspect its contents, and remove it — or simply use rm -fr /carefully/vetted/pathname. Commented Apr 26, 2015 at 23:08
  • 1
    You guys are really helping me out here and I appreciate it! Actually, I know it was unclear, I want to end up making this script interactive where the files and directories inside the chosen directory are listed 1, 2, 3, 4... and the user will select a number to delete the file or directory. Commented Apr 26, 2015 at 23:15

2 Answers 2

2

I think the following should do what you want, by checking whether you have at least one argument first. If there is at least one argument, then the script will change to the directory given in the argument before removing files.

Please be careful, because you will actually be deleting files. It is very hard to get them back.

For the safety of the future folks who see this question, I am adding -i to make the command confirm before deleting. But if you are sure you want to do this without confirmation, you can remove the -i.

#!/bin/bash

if [[ $# -ge 1 ]]; then
    cd "$1"
fi
rm -i *
Sign up to request clarification or add additional context in comments.

3 Comments

@JonathanLeffler, Look at the first line in the OP. I quote I am writing a script to remove files from directories (if given).
My bad...though not as bad an idea as the requested script. The 'my current script' example doesn't even pretend to remove any files.
@JonathanLeffler, I agree with you. I'll edit the answer so fewer people will shoot themselves in the foot with this answer.
1

If you're writing a script that removes files based on an input directory:

  • If you are trying to remove files in the current directory it is a good idea to make sure the current directory is NOT the root level directory.
  • Check the length of the actual directory name. If the directory name is short, the user may be trying to remove a top level directory that you do not want to remove.
  • On LINUX, rm may have the --preserve-root option. You may want to consider using this option; it will prevent the user from trying to remove the root filesystem.
  • #!/bin/sh
    if test $# -ne 1; then 
        CURRENT_DIR=`pwd`
        if test ${CURRENT_DIR} = "/"; then
            echo "ERROR: Do not remove files in the root directory"
            exit 1
        fi
    
        #Perform removal commands in the current directory
        #with safeguards
        #
        exit 0
    fi
    DIR=$1
    size=${#DIR} 
    if test $size -le 5; then
        echo "ERROR: The directory name is too short"
        exit 1
    fi
    
    #Perform the removal commands here with safeguards
    #
    

    1 Comment

    Thanks - I added a test for the current directory

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.