BASH: Script entry for recursively moving N level directories

I have the following directory structure, for example:

/test_dir/d /test_dir/d/cron /test_dir/d/cache /test_dir/d/...(more sub dirs) /test_dir/tree /test_dir/tree/a /test_dir/tree/a/a1 /test_dir/tree/a/a2 ...(and so on for b/ and c/ ) 

I wrote the following bash script that effectively moves to the second level /test_dir , so it will reach /test_dir/d/cron or /test_dir/tree/a , but will not go any further. I can’t understand why the recursive script will not travel further, can someone debug the script and indicate my error?

here is what i wrote:

 #!/bin/bash #script to recursively travel a dir of n levels function traverse() { for file in `ls $1` do #current=${1}{$file} if [ ! -d ${1}${file} ] ; then echo " ${1}${file} is a file" else #echo "entering recursion with: ${1}${file}" traverse "${1}/${file}" fi done } function main() { traverse $1 } main $1 

here is the result:

 /test_dir/a is a file /test_dir/b is a file /test_dir//dcache is a file /test_dir//dcron is a file /test_dir//dgames is a file /test_dir//dlib is a file /test_dir//dlog is a file /test_dir//drun is a file /test_dir//dtmp is a file /test_dir/movies is a file /test_dir//treea is a file /test_dir//treeb is a file /test_dir//treec is a file /test_dir//treed is a file 

I know that there are probably more elegant single-line commands for this. But I'm trying to do it in this explicit way. I apologize for the length of this post.

EDIT: using traverse "${1}/${file}"

+6
source share
2 answers

A few problems with the script. It should be like this:

 #!/bin/bash #script to recursively travel a dir of n levels function traverse() { for file in "$1"/* do if [ ! -d "${file}" ] ; then echo "${file} is a file" else echo "entering recursion with: ${file}" traverse "${file}" fi done } function main() { traverse "$1" } main "$1" 

However, the correct path to recursively moving a directory is via the find command:

 find . -print0 | while IFS= read -r -d '' file do echo "$file" done 
+18
source
 /test_dir/treea is a file /test_dir/treeb is a file /test_dir/treec is a file /test_dir/treed is a file 

These are errors. Some that are not only not directories but do not exist are considered files. I think you need to separate your new files from the directory with a slash:

  traverse "${1}/${file}" 

And also make sure they exist too.

Since you are using bash, I would suggest this form:

 #!/bin/bash shopt -s dotglob ## Optionally would allow matches for directories beginning with . shopt -s nullglob function traverse { local a file for a; do for file in "$a"/*; do if [[ -d $file ]]; then traverse "$file" else echo " $file is a file." fi done done } traverse " $@ " 

You can run a script with several arguments.

Fixed version of your script:

 #!/bin/bash #script to recursively travel a dir of n levels function traverse() { for file in $(ls "$1") do #current=${1}{$file} if [[ ! -d ${1}/${file} ]]; then echo " ${1}/${file} is a file" else #echo "entering recursion with: ${1}${file}" traverse "${1}/${file}" fi done } function main() { traverse "$1" } main "$1" 
+3
source

Source: https://habr.com/ru/post/954176/


All Articles