0

I have a shell script that loops into each directory and finding a file. The problem I have is shell script is unable into cd to a directory that contains space. Here is example script.

First method:

for dir in "$(find /usr/tom/public/junk -maxdepth 3 -type f -name testfile)"; do
        cd "$(dirname "$dir")"
        printf "%-54s" $(basename $PWD)
       done 

second method:

find "/usr/tom/public/junk" -maxdepth 3 -type f -name testfile -print |
  while IFS= read -r dir
  do
    cd "$(dirname "$dir")"
    printf "%-54s" $(basename $PWD)
    done 

Error : No such file or directory ( because some directories contains spaces)

my requirement is -> When a "test" file is found in a directory then it has to delete that directory.

I am running this script on ubuntu.And the weird part is, this piece of code working in my Mac but not on ubuntu server. I have no idea why it is not working on ubuntu. Please help me.

Marko Previsic
  • 1,820
  • 16
  • 30
Jwary
  • 137
  • 2
  • 16
  • [Loop over directories with whitespace in Bash](https://stackoverflow.com/q/4895484/608639) and friends. Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck). – jww Jul 12 '19 at 07:52

2 Answers2

0

The problem with spaces in directorynames can be circumventing by using read to process the output from find. There is an very thorough explanation for that in this answer. For your situation, something like this would work:

find /usr/tom/public/junk 3 -type f -name testfile | while read dir; do
    cd "$(dirname $dir)"
done
BakaKuna
  • 585
  • 4
  • 13
  • Thanks for your reply. It is reading the "testfile" in directory which is correct. Now I have an "if" condition to delete the directory.Unfortunately it is not deleting the directory. It is only listing the directories that has the "testfile" – Jwary Jul 12 '19 at 08:18
  • I don't see the if condition in your question. Do you want to now how to remove the directories? In that case `rm -rf $(banename $PWD)` should work. – BakaKuna Jul 12 '19 at 08:26
  • It is deleting only the folder that has "testfile" but not the entire directory – Jwary Jul 12 '19 at 09:54
  • I don't have completely clear what command you are using, but to reliably get the parent directory of any path, you can use `dirname`. – BakaKuna Jul 15 '19 at 06:47
  • @BakaKua , Thanks for your reply. I figured it out and it is now working. – Jwary Jul 16 '19 at 08:28
0

Use double quotes to the directory names.

mahendra@test-Ubuntu:~$ cd /test
mahendra@test-Ubuntu:-Ubuntu:/opt$ cd "new test"

As the folder you want to connect has spaces in the name, you must surround the name with quotes in order for the Shell to read it correctly (as one name).

now you need to check how you can pass "" to directories in the shell script.

another way is you can use the backslash

cd test\ Text\ 2/

the backslash followed by a space explicitly denotes a space.

suggestion: do not create a directory with space in Linux.

mahendra rathod
  • 1,438
  • 2
  • 15
  • 23
  • Yes, I am passing double quotes to a directory and subfolders. The script is now able to cd into a directory but as soon as it finds a text file then script has to delete the entire directory. Deleting a directory part is not working – Jwary Jul 12 '19 at 08:39
  • confirm, you don't know how many subdirectories are there & after getting what types of files you want to delete the parent directory or one level up directory? – mahendra rathod Jul 12 '19 at 08:45