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 thecd
is/tmp
(IMHO this is more flexible than having/tmp
directly in thefind
command. You have only one place, thecd
, to change, if you want more actions to take place in this folder)-maxdepth 1
and-mindepth 1
make sure thatfind
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
Static Link of Shared Library Function in Gcc
Better Way to Rename Files Based on Multiple Patterns
What's the Difference Between Nohup and Ampersand
How to Get the Total Cpu Usage of an Application from /Proc/Pid/Stat
Automatically Capture Output of Last Command into a Variable Using Bash
How to Set Environment Variable For Everyone Under My Linux System
Split One File into Multiple Files Based on Delimiter
How to Escape Single Quotes in Bash/Grep
Quickly Create a Large File on a Linux System
How to Set the Working Directory of the Parent Process
How to Use Local Docker Images With Minikube
More Elegant "Ps Aux | Grep -V Grep"
Forcing Bash to Expand Variables in a String Loaded from a File
Pipe To/From the Clipboard in a Bash Script
Find and Replace With Sed in Directory and Sub Directories
Awk or Sed to Remove All Text After X Occurence in Each Line of File