I'm working on a project that requires batch processing of a large number of image files. To make things easier, I've written a script that will create n
directories and move m
files to them based on user input.
My issue is to now understand directory traversal via shell script.
I've added this snippet at the end of the sort 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 ran it inside a Pano2 folder, whcih contains two inner folders, I always got an error
./dirTravel: line 9: cd: Pano2/05-15-2012-2: No such file or directory
However, after that, I get the file listing from specified directory.
What is the reason behind the warning? If I add cd ../
after ls
I get the listing of the folders inside Pano2/, but not the files themselves.
I recommend using a sub-shell to switch directories:
dirlist=$(find $1 -mindepth 1 -maxdepth 1 -type d)
for dir in $dirlist
do
(
cd $dir
echo $dir
ls
)
done
The code inside the sub-shell won't affect the parent shell, so it can change directory anywhere without giving you any issues about 'how to get back'.
This comes with the standard caveat that the whole process is fraught if your directory names can contain spaces, newlines or other unexpected and awkward characters. If you keep with the portable filename character set ([-_.A-Za-z0-9]
) you won't run into problems as written.
My guess is that you're going in two levels deep, but only coming back up one level. Try adding a cd ../..
after the ls
, or use pushd
and popd
instead.
For example:
for dir in $dirlist
do
pushd $dir
echo $dir
ls
popd
done
As @shellter points out, if those directories have spaces, then something like this might work better:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With