how to create a zip file of the cd in bash script - bash

i want to create the zip file of the current working directory in bash script. AND Redirect the stdout of zip command to a file called zip-output.txt.
i have a current workind directory called "music" and my script populates it with a lot of content. thus after it is populated i want to convert it to "music.zip" . but the contents of the "music" directory shouldn't be altered or zipped.
/Users/xyz/Downloads/music
this is the path to the cd "music" if need be and the file "zip-output.txt" could be created at the path "/Users/xyz/Downloads"
ik this is easy but i am new. please guide me

Do you mean something like this?
zip -r music.zip music/ | tee zip-output.txt

Related

In Bash how to read the properties of a file?

I am very new to bash scripting.
I need to make a bash script which finally copies files from one directory to another directory.
But target directory for all the files does not have be the same for all the files in the source directory. If the target directory does not exists then it must be created. The name of the new directory depends on the creation date of the concerning file. The format of the directory name is YYYY_MM_DD.
Therefor I must read the properties of the file, and then check if the target directory exists. If not, then the create the directory and copy the file. If it exists then copy the file.
How can I read the properties of a file? How can I create a new directory in format YYYY_MM_DD? How can I check if the directory already exists?
To know the target directory, the script needs to read the properties of every file in the source directory via a loop. How can I loop through the files in a directory?
Have I made myself clear? If not, ask me for more information.
Thank you.
Carlos Wiesemann
To read the last modification date file in a directory via a loop
ls | stat --format "%n - %y" *
or if you don't like the ls command, as Cyrus sugested, remove it ;-)
stat --format "%n - %y" *

Shell Script to rename file with string from another filename

I have been stuck with a task where I need to rename a file with a substring from another filename and then move it to a directory.
What I want to achieve here is as below
I have a file named dummy.txt and another file named 20150416demo.xml
What I want is the file abc.txt to be renamed with the date from the xml file i.e. dummy20150416.txt
The final step is to move it to a different location. Appreciate you responses
Thanks
To rename/concatenate and move according to what you want you can use this script:
#!/bin/bash
file="20150416demo.xml"
date=${file:0:8}
mv abc.txt /destination/path/dummy$date.txt

open a folder from a name in txt file BASH

I have a text file fold.txt that contains one line fold_nam:
$ cat fold.txt
fold_nam
This name in the the text file is an output that was created during a program run of a folder's name that now contains other files that I need to work with.
I am writing a big script and now I need to enter this folder and I need to get the name from the text file. I tried several things but cannot really work it out.
There's no need to use cat:
cd $(<fold.txt)
If you want to read the line into a variable: read -r folder_name < fold.txt
You should be able to do this:
cd $(cat fold.txt)
or
cd `cat fold.txt`

How can I display a text file within a bash script?

Im trying to display a text file in a directory within the folder my script is in. I tried things like:
mypath=`realpath $0`
FILE="$realpath/Folder/Text.txt"
cat $FILE
And
FILE="$PWD/Folder/Text.txt"
cat $FILE
but they include the name of the file instead of just the its running from. I also want it to work with symbolic links.
cat "$(dirname -- "$0")/Folder/Text.txt"
That is, send the file Text.txt in the directory Folder below the directory where this script is located to standard output.
This will work with symlinks. And yes, all the quotes are significant.
You can do:
cat "./Folder/Text.txt"
the period denotes the current folder you are in. a double period "../Folder/Text.txt" denotes a folder up in the directory tree.

How do I Unzip a file, replace a file with a new copy and re-zip the file in Apple script?

I have a string with the path to a .ipa file:
set ipa_path to POSIX path of ipa_file
Now I want to:
Un-zip the .ipa file (its really a zip file)
Replace a file in the zip called "embedded.mobileprovision" with a new version of the file.
Re-zip the file and replace the original ipa file.
So for I have:
do shell script "unzip " & ipa_path
Is this right so far? I just started to learn AppleScript today ...
You don't really need AppleScript for this. The command line zip utility has the -r option to replace existing files (of the same name and relative path) within a zip archive. Here's a quote from the man page man zip:
if foo.zip exists and contains foo/file1 and foo/file2, and the
directory foo contains the files foo/file1 and foo/file3, then:
zip -r foo.zip foo
will replace foo/file1 in foo.zip and add foo/file3 to foo.zip.
Of course, you can still wrap the calls to zip in AppleScript do shell script commands.

Resources