Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to give the option to the users to run the script in a different directory. However when the script changes the directory, the mkdir does not work. I change the directory like this:

case $1 in

but when I run the script, it runs in the directory that it exists. it does not change the directory.

here is the complete code:

#File sorter
set -e    # Abort in case of error
START=$(date +"%T")
case $1 in
echo "trigger took place in $PWD"
for i in *; do
  echo "processing \"$i\" ..."
  case $i in
      [ -d archive ] || mkdir archive
      mkdir "archive/${i%.*}"
      unzip -d "archive/${i%.*}" "./$i"
      echo -e "\"$i\" extracted in $PWD/archive/${i%.*}" >> report.txt;;
      mkdir -p "${i##*.}"
      mv -- "$i" "${i##*.}/"
      echo -e "\"$i\" moved to $PWD/${i##*.}" >> report.txt;;
      tmp=$(TMPDIR=. mktemp -d)
      mv -- "$i" "$tmp/"
      mv -- "$tmp" "$i";;
echo "Done in $SECONDS seconds!"
END=$(date +"%T")
echo "Start time: $START" >> report.txt
echo "End time: $END" >> report.txt
share|improve this question

migrated from stackoverflow.com Mar 4 '12 at 17:11

This question came from our site for professional and enthusiast programmers.

A comment not on the question: why don't you use cd to change directory? – enzotib Mar 4 '12 at 7:38
up vote 3 down vote accepted

I'm not surprised that changing PWD doesn't do anything.

Simply do a cd "$2"

but when I run the script, it runs in the directory that it exists.

It runs in the working directory of the shell you executed it.. If you're in the directory of the script, then yes, it will execute there.

share|improve this answer
thanks, I didn't think it's so easy :D – Erfan Mar 4 '12 at 11:08

The PATH variable is reserved for finding programs that the shell will use. It is a list of directories, separated by colons (:) which is searched in order until the program is found. From the manpage of bash


The search path for commands. It is a colon-separated list of directories in which the shell looks for commands (see COMMAND EXECUTION below). A zero-length (null) directory name in the value of PATH indicates the current directory. A null directory name may appear as two adjacent colons, or as an initial or trailing colon. The default path is system-dependent, and is set by the administrator who installs bash. A common value is ''/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin''.

If you change that variable, it changes how the shell finds programs, like mkdir.

I suggest changing the variable to path or dir (case matters in Linux/UNIX).

share|improve this answer
Where does he use the PATH var? I cannot see it. – enzotib Mar 4 '12 at 7:36
@enzotib I edited the question, first it was PATH then I tried it with PWD – Erfan Mar 4 '12 at 10:38
PWD is a variable that is set by the shell. To change the directory, use cd _directory_. For example, cd /var/log. If returns a non-zero return value if the shell is not able to change the current directory. The cd is a builtin command in the shell, not a program. – Arcege Mar 4 '12 at 13:42
Also, you're not doing anything about the fact that if $1 is -d, you're not removing it and $2 from the list of files to operate on. Take a look at the 'shift' operator in the bash documentation. – jsbillings Mar 4 '12 at 14:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.