for-loop over directory to zip subdirectories - bash

My question is really simple but it's been too long since I last read bash-scripts so I cannot understand bash anymore...
I have a folder (name containing spaces) which has x subfolders. Now I want to zip each subfolder in its own zip archive.
The following script is what I have:
#!/bin/bash
cd $1
for folder in $1/* do
zip -r "${folder%/}" "${folder%/}"
done
every time I try to execute it with ./test.sh . I just get
./test.sh: line 4: syntax error near unexpected token `zip'
./test.sh: line 4: `zip -r "${folder%/}" "${folder%/}"'
what is wrong with my thoughts and why?
if I enter a variable (e.g. file="subfolder i want to zip") in the shell and execute zip -r "${file}" "${file}" it just works fine for the one folder.
I appreciate any help and hope my question can be solved easily (so I'm able to understand it).
Thanks a lot in advance.

Try this -
#!/bin/bash
cd $1
for folder in $1/*
do
zip -r "${folder%/}" "${folder%/}"
done
Either put ";" after $1/* or use do in next line.

If you want to use shell tools, you can use a for loop as illustrated in other answers. If you can afford calling another application like find, here's a one-liner:
find "${1}" -maxdepth 1 -mindepth 1 -type d -exec zip -r "{}.zip" "{}" \;

You need a semicolon before the do:
for folder in $1/*; do
zip -r "${folder%/}" "${folder%/}"
done
This is needed to disambiguate the grammar, specifically to indicate "no more strings to iterate over". Eg. this allows to iterate over shell keywords:
for k in do if done; do
...
done

Someone already mentioned it, but you do need a semi color before the do
I would also recommend checking if you are really zipping up a directory.
#!/bin/bash
cd $1
for folder in $1/* ; do
[ -d $folder ] && zip -r "${folder%/}" "${folder%/}"
done

Related

Rename all files of a certain name within all second-level sub-directories

My goal is to automate the following process: search within all second-level sub-directories, and for all files called "Test.pptx" in said sub-directories, rename to "Test - Appended.pptx". Based on responses I have seen to other questions on StackOverflow I have attempted the following code:
for D in *; do
if [ -d "${D}" ]; then
echo "${D}"
for E in "${D}"; do
if [ -d "${E} ]; then
echo "${E}"
for f in "Test.pptx"; do mv "$f" "Test - Appended.pptx"; done
fi
done
fi
done
I set the script executable (using chmod +x) and run it, but get the following errors:
line 7: unexpected EOF while looking for matching `"'
line 12: syntax error: unexpected end of file
I am a relative newcomer to Bash scripts, so I would appreciate any help in diagnosing the errors and achieving the initial goal. Thank you!
use find:
while read -r pptx
do
mv -n "${pptx}" "${pptx%.pptx} - Appended.pptx"
done < <( find . -mindepth 3 -maxdepth 3 -type f -name "*.pptx" )
Be aware, that I did not test it and it might need adaptions on your special case.
As long as the -n option is set in mv it will not overwrite anything.
sub-loops not really needed.
for f in */*/Test.pptx; do mv "$f" "${f%/*}/Test - Appended.pptx"; done
${f%/*} is the current file's full path with everything from the last slash (/) forward stripped off, so if the file is a/b/Test.pptx then ${f%/*} is a/b.

Passing variables to cp / mv

I can use the cp or mv command to copy/mv files to a new folder manually but while in a for loop, it fails.
I've tried various ways of doing this and none seem to work. The most frustrating part is it works when run locally.
A simple version of what I'm trying to do is shown below:
#!bin/bash
#Define path variables
source_dir=/home/me/loop
destination_dir=/home/me/loop/new
#Change working dir
cd "$source_dir"
#Step through source_dir for each .txt. file
for f in *.txt
do
# If the txt file was modified within the last 300 minutes...
if [[ $(find "$f" -mmin -300) ]]
then
# Add breaks for any spaces in filenames
f="${f// /\\ }"
# Copy file to destination
cp "$source_dir/$f $destination_dir/"
fi
done
Error message is:
cp: missing destination file operand after '/home/me/loop/first\ second.txt /home/me/loop/new/'
Try 'cp --help' for more information.
However, I can manually run:
mv /home/me/loop/first\ second.txt /home/me/loop/new/
and it works fine.
I get the same error using cp and similar errors using rsync so I'm not sure what I'm doing wrong...
cp "$source_dir/$f $destination_dir/"
When you surround both arguments with double quotes you turn them into one argument with an embedded space. Quote them separately.
cp "$source_dir/$f" "$destination_dir/"
There's no do anything special for spaces beforehand. The quoting already ensures files with whitespace are handled correctly.
# Add breaks for any spaces in filenames
f="${f// /\\ }"
Let's take a step back, though. Looping over all *.txt files and then checking each one with find is overly complicated. find already loops over multiple files and does arbitrary things to those files. You can do everything in this script in a single find command.
#!bin/bash
source_dir=/home/me/loop
destination_dir=/home/me/loop/new
find "$source_dir" -name '*.txt' -mmin -300 -exec cp -t "$destination_dir" {} +
You need to divide it in to two strings, like this:
cp "$source_dir/$f" "$destination_dir/"
by having as one you are basically telling cp that the entire line is the first parameter, where it is actually two (source and destination).
Edit: As #kamil-cuk and #aaron states there are better ways of doing what you try to do. Please read their comments

Rename all files in a directory by omitting last 3 characters

I am trying to write a bash command that will rename all the files in the current directory by omitting the last 3 characters. I am not sure if it is possible thats why I am asking here.
I have a lots of files named like this : 720-1458907789605.ts
I need to rename all of them by omitting last 3 characters to obtain from 720-1458907789605.ts ---> 720-1458907789.ts for all files in the current directory.
Is it possible using bash commands? I am new to bash scripts.
Thank you!
Native bash solution:
for f in *.ts; do
[[ -f "$f" ]] || continue # if you do not need to rename directories
mv "$f" "${f:: -6}.ts"
done
This solution is slow if you have really many files: star-expansion in for will take up memory and time.
Ref: bash substring extraction.
If you have a really large data set, a bit more complex but faster solution will be:
find . -type f -name '*.ts' -depth 1 -print0 | while read -d $\0 f; do
mv "$f" "${f%???.ts}.ts"
done
With Larry Wall's rename:
rename -n 's/...\.ts$/.ts/' *.ts
If everything looks okay remove dry run option -n.

deleting files in a given subdirectory

I have a few subdirectories in a given folder, where a file d2.sh~ exists. I want to delete this file via following shell script, which, rather than writing in a .sh file I wrote on terminal, on one line. [Edit: been formatted properly here for clarity]
for i in `ls *`; do
if [ -d $i ]; then
cd $i
rm d2.sh~
cd ..
fi
done
This did not give me any errors but it failed to delete d2.sh~ from the subdirectories. So I want to know what mistake I have made above?
find /some/path -type f -name "d2.sh~" -delete
Your first mistake is trying to parse ls. See this link as to why.
Just use for i in *; do ....
If you need recursion then you need to look to find or if you have Bash 4.X you can do:
shopt -s globstar; for i in **/d2.sh~; do rm "$i"; done

Bash scripting, loop through files in folder fails

I'm looping through certain files (all files starting with MOVIE) in a folder with this bash script code:
for i in MY-FOLDER/MOVIE*
do
which works fine when there are files in the folder. But when there aren't any, it somehow goes on with one file which it thinks is named MY-FOLDER/MOVIE*.
How can I avoid it to enter the things after
do
if there aren't any files in the folder?
With the nullglob option.
$ shopt -s nullglob
$ for i in zzz* ; do echo "$i" ; done
$
for i in $(find MY-FOLDER/MOVIE -type f); do
echo $i
done
The find utility is one of the Swiss Army knives of linux. It starts at the directory you give it and finds all files in all subdirectories, according to the options you give it.
-type f will find only regular files (not directories).
As I wrote it, the command will find files in subdirectories as well; you can prevent that by adding -maxdepth 1
Edit, 8 years later (thanks for the comment, #tadman!)
You can avoid the loop altogether with
find . -type f -exec echo "{}" \;
This tells find to echo the name of each file by substituting its name for {}. The escaped semicolon is necessary to terminate the command that's passed to -exec.
for file in MY-FOLDER/MOVIE*
do
# Skip if not a file
test -f "$file" || continue
# Now you know it's a file.
...
done

Resources