How to Loop Over Directories in Linux

How to loop over directories in Linux?

cd /tmp
find . -maxdepth 1 -mindepth 1 -type d -printf '%f\n'

A short explanation:

  • find finds files (quite obviously)

  • . is the current directory, which after the cd is /tmp (IMHO this is more flexible than having /tmp directly in the find command. You have only one place, the cd, to change, if you want more actions to take place in this folder)

  • -maxdepth 1 and -mindepth 1 make sure that find only looks in the current directory and doesn't include . itself in the result

  • -type d looks only for directories

  • -printf '%f\n prints only the found folder's name (plus a newline) for each hit.

Et voilà!

Loop over directories one level deep and execute script with directory name as argument in bash in that directory

The script is probably exiting early because recon1.sh is failing, and you have set -e (exit if a command fails).

It's probably failing due to cd .. which should not be there.

It may be also be better to keep attempting recon1.sh on all proteins, even if one fails (that's up to you).

Replace

(cd "${fol}" && bash ${path_to_software_folder}/recon1.sh ${dirname} )
cd ..

With:

cd "${fol}" || continue
bash ${path_to_software_folder}/recon1.sh "${dirname}" || echo "recon1.sh failed for ${dirname}" >&2

The working directory gets set correctly. If recon1.sh still fails for any reason, an error is printed, but the script won't exit, and the next protein is attempted.

How to loop through a directory and all of its sub-directories?

This command will give you all the directories within parent

find /parent -type d

use man find for description of the command and all available options

bash - for loop through multiple directories and their files

Would you please try the following:

#!/bin/bash

for i in my_path/*/; do
year=${i%/}; year=${year##*/} # extract year
year2=$(( year + 19 )) # add 19
for j in "$i"*.nc; do
echo cdo "selyear,${year}/${year2}" "$j" "$j"2
done
done

It outputs command lines as a dry run. If it looks good, drop echo and run.

Loop over directories with whitespace in Bash

Give this a try:

for dir in */

Looping over directories in Bash

Never ever parse the output of ls like this (Why you shouldn't parse the output of ls(1)).

Also, your syntax is wrong. You don't mean (), you mean $().

That being said, to loop over directories starting with W you would do (or use the find command instead, depending on your scenario):

for path in /my/path/W*; do
[ -d "${path}" ] || continue # if not a directory, skip
dirname="$(basename "${path}")"
do_stuff
done

As for the output you get from the evil ls-loop, it should not look like that. This is the expected output and demonstrates why you do not want to use ls in the first place:

$ find
.
./c
./a
./foo bar
./b

$ type ls
ls is hashed (/bin/ls)

$ for x in $(ls); do echo "${x}"; done
a
b
c
foo
bar

in bash how do I loop through directories starting with abc and the directory special-dir

The path expansion just expands the glob to list of the actual directories. Word list is space separated, so just use space to separate the next element of the list:

for dir in abc*/ special-dir ; do
echo "$dir"
done

BASH loop through folders and files

You are quoting the wildcard, so you are looking for files whose name literally ends with the character *.

Anyway, you should not use ls to drive loops, and also, you should avoid using uppercase for your private variables.

#!/bin/bash

tar_fol="$HOME/Desktop/try/"
to_fol="$HOME/Desktop/SortedFiles/"

for dire in "$tar_fol"/*/.
do
echo "Checking dir: '$dire'"
for fil in "$dire"/*
do
echo "Checking file: '$fil'"
find ~/Desktop/ -type d -name "${fil##*.}" -exec sh -c 'mkdir -p "$0";
mv "$@" "$0"' "$to_fol" "$fil" {} \+
done
done

I'm not sure I understand exactly what the innermost loop is supposed to accomplish, so there is some guesswork there. The general idea is to pass the destination directory in $0 (obscure hack, but it's fairly common) and the found files to the -exec script.



Related Topics



Leave a reply



Submit