Shell script to move directories

I am working on a project that requires batch processing of a large number of image files. To keep things simple, I wrote a script that will create directories nand move files mto them based on user input.

My problem is now to understand directory traversal through a shell script.

I added this snippet at the end of the sorting script described above

dirlist=$(find $1 -mindepth 1 -maxdepth 1 -type d)

for dir in $dirlist
do
  cd $dir
  echo $dir
  ls
done

When I launched it in the Pano2 folder, which has two internal folders, I always had an error

./dirTravel: line 9: cd: Pano2/05-15-2012-2: No such file or directory

However, after that I get a list of files from the specified directory.

What is the reason for the warning? If I add cd ../after ls, I will get a list of folders inside Pano2 /, but not the files themselves.

+3
source share
3

, , . cd ../.. ls pushd popd.

:

for dir in $dirlist
do
  pushd $dir
  echo $dir
  ls
  popd
done

@shellter, , - :

find $1 -mindepth 1 -maxdepth 1 -type d | while read -r dir
do
  pushd "$dir"  # note the quotes, which encapsulate whitespace
  echo $dir
  ls
  popd
done
+3

- :

dirlist=$(find $1 -mindepth 1 -maxdepth 1 -type d)

for dir in $dirlist
do
  (
  cd $dir
  echo $dir
  ls
  )
done

- , , , " ".

, , , . ([-_.A-Za-z0-9]), , .

+3

ls. :

find "$1" -mindepth 1 -maxdepth 1 -type d -exec ls {} \;

, , , bash -c -exec:

find "$1" -mindepth 1 -maxdepth 1 -type d -exec bash -c 'cd "$1"; echo "$1"; ls' -- {} \;

bash -c , .

+2

All Articles