create and loop through arrays of folders in shell - arrays

Please feel free to rename the question to something more appropriate.
How would I mimic the below zsh using bash instead?
mkdir folder1
mkdir folder2
mkdir folder3
# zsh
folders=(folder*) | print $folders
#folder1 folder2 folder3
# bash
folders=(folder*/) | echo $folders
#folder1
As you can see, this only outputs the first element.
Any pointers would be appreciated, thanks.

Try changing it to:
folders=(folder*); echo "${folders[#]}"
folders[#] gives all the elements in the array
${} expands the output from above command to bash.

If lets say, you have multiple .txt file in some Directory and you want to get/display those folders . you can try something like this:
declare -a folder_arr
i=0
for dir in *.txt; do
folder_arr[i]=$dir
i=$((i+1))
done
for j in $(seq 0 $((i-1)))
do
echo ${folder_arr[$j]}
done
I excuted the above file and was able to get the expected reult.
/temps$ ./dirrr.sh
z.txt

Related

Repeat a specific command for all files located in a folder

I need a script that will go into a directory, execute the command on each file, i tried some commands in a batch file, but i can't figure it out :)
john-wick-parse serialize file_route/filename_with_no_extention
Bash shell for loops
for f in file1 file2 file3 file5
do
echo "We are now processing... $f"
# do something on $f
done
And if you want, you can declare a variable containing the path to the files, one on each line, something like this:
FILES="file1
/path/to/file2
/etc/resolv.conf"
for f in $FILES
do
echo "We are now processing... $f"
# do something on $f
done
There's all sort of options for that, like selecting all files with a certain extension.
So if you were to use the first option:
Our files
-- my_folder
-- file1
-- file2
-- file3
-- etc
Our code
cd /path/to/my_folder
for f in file1 file2 file3 file5
do
echo "We are now processing... $f"
# do something on $f
done

How to make this script work but search for empty files in any directory instead of current directory?

#!/bin/bash
n=0
for f in *; do
[[ -f "$f" ]] && { echo "$f"; ((n++)); }
done
echo :Number of empty files: $n"
currently it checks the current directory for empty files, I would like it to search for empty files in any directory. Any ideas?
Recursively searches for empty files in current directory and below:
find . -empty -type f
Recursively lists empty files in specified directory and below and reports total
findempty
#!/bin/bash
echo :Number of empty files: `find $1 -empty -type f | tee /dev/tty | wc -l`
Example Usage
findempty /tmp
Example Output
/tmp/source/fb/b
/tmp/source/fb/a
/tmp/source/fb/c
/tmp/source/fa/b
/tmp/source/fa/a
/tmp/source/fa/c
/tmp/source/fc/b
/tmp/source/fc/a
/tmp/source/fc/c
/tmp/dest/source/fb/b
/tmp/dest/source/fa/b
/tmp/dest/source/fc/b
:Number of empty files: 12

Exit not working inside loop and if condition

I have a list file containing:
PATH1=/opt/apps/skum_edw/Source_Bad/
PATH2=/opt/apps/skum_edw/Source_Backup/
PATH3=/hfd
PATH4=/opt/apps/skum_edw/Target_Backup/
Also I have a script to check whether dir is present or not:
cat Path.lis | cut -d'=' -f2 | while read path
do
[ -d $path ]
then
echo $path is present
else
echo $path is not present
exit 1
fi
done
echo That is the end of script
The problem is that the exit 1 is not working. And I am getting last line also as output. How to do it? And what is the reason of this?
There's no IF, while there is an FI.

Batch: Run an Awk-Script on all text-files in the folder

basically I want to run the following command on every text-file automatically:
awk -f myScript.awk file1.txt > new\file1.txt
awk -f myScript.awk file2.txt > new\file2.txt
...
Then move the processed files to the folder \old.
move *.txt \old
should work for that part.
How do I create the correct for-loop, so that the output of the awk program has the same name as the input, just in the new folder?
OK, try this:
for %%i in (*.txt) do awk -f myScript.awk "%%~fi" > "new\%%~nxi"

How do I sort a directory full of pictures into sub-directories based on the image name?

I want to take all of the files in /media/mdrive/dump/:
1COD-234355.jpg
MAK-LXT218.jpg
ZIR-CON145.jpg
And create and sort them into the following directories:
/media/mdrive/dump/1/1COD-234355.jpg
/media/mdrive/dump/M/MAK-LXT218.jpg
/media/mdrive/dump/Z/ZIR-CON145.jpg
How would I do that?
This script takes a directory as the first argument and performs what you need:
#!/bin/bash
DIR="$1"
if [ -z "$DIR" ]; then
echo >&2 "Syntax: $0 <directory>"
exit 1
fi
if [ ! -d "$DIR" ]; then
echo >&2 "\"$DIR\" is not a directory"
exit 1
fi
cd "$DIR"
for file in *.jpg *.JPG; do
first=${file::1}
mkdir -p $first && mv $file $first/;
done
head -c xx will return the first xx characters of its input (here, the filename). mkdir -p will skip directory creation if it already exists.
to make two directories you could try something like
dir "/media/mdrive/dump/1/" :: CD would also work here
mkdir folder 1
mkdir folder 2
from here I think you can continue with your IF statements and so forth.
all you need to do is set the dir commands with the Direct path takes the guess work out.
then to check each just do:
start explorer.exe "the folder's path here"
it should open the folder to view the files

Resources