OSX Bash Script: Zip files in directory, Rename move and delete - macos

I have the following directory and am having problems selecting the path:
Server HD/Library/FileMaker Server/Data/Backups/Nightly/NightlyBackups/Databases/*
A) How do I correctly Zip all the files in the * directory,
B) Rename the the Zip file, XXXX_XX_XX_Filemaker Nightly.zip
C) Move XXXX_XX_XX_Filemaker Nightly.zip to HD/Library/FileMaker Server/Data/Backups/Nightly/NightlyBackups/Nightly Zipped folder
Thank you,
John

First, man is your friend. The man command as in manual tells you how to use other commands:
$ man zip
Don't be overwhelmed by the amount of information. The important thing is to see the various command line parameters and some helpful examples. For example, if you want to include all subdirectories, you would need to use the --recurse-paths parameter. Compare this to the --recurse-patterns parameter.
You look like you're a pretty much of a beginner with shell scripting and using the shell on your Mac. Look at Take Control of the Mac Command Line with Terminal. It's a simple straightforward, not-overly-technical, non-programmer introduction to the command line, and into basic shell scripting. It's a great introduction.

Related

How to go to a specific file in Mac Terminal?

I'm trying to follow a walkthrough and this is one of the steps, "go to that same folder in Terminal (if you can't do that you should probably quit now)" I'm not very familiar with Mac's Terminal, but don't feel like quitting. If it helps, I need to run a grep -r "what I'm looking for" command on the file. I really have no idea what I need to do to run that command, but the rest of the walkthrough is pretty thorough, so I know I can follow the next few steps.
You need to move to the directory where that file is stored, for this the command is cd. For example lets say the file is located in a directory names MyDir in the Desktop, the command will be
cd /Users/{YOUR_USERNAME_HERE}/Desktop/MyDir
You can run ls command here to check if the file is actually present in this directory or not

How to create a batch file in Mac?

I need to find a solution at work to backup specific folders daily, hopefully to a RAR or ZIP file.
If it was on PC, I would have done it already. But I don't have any idea to how to approach it on a Mac.
What I basically want to achieve is an automated task, that can be run with an executable, that does:
compress a specific directory (/Volumes/Audio/Shoko) to a rar or zip file.
(in the zip file exclude all *.wav files in all sub Directories and a directory names "Videos").
move It to a network share (/Volumes/Post Shared/Backup From Sound).
(or compress directly to this folder).
automate the file name of the Zip file with dynamic date and time (so no duplicate file names).
Shutdown Mac when finished.
I want to say again, I don't usually use Mac, so things like what kind of file to open for the script, and stuff like that is not trivial for me, yet.
I have tried to put Mark's bash lines (from the first answer, below) in a txt file and executed it, but it had errors and didn't work.
I also tried to use Automator, but it's too plain, no advanced options.
How can I accomplish this?
I would love a working example :)
Thank You,
Dave
You can just make a bash script that does the backup and then you can either double-click it or run it on a schedule. I don't know your paths and/or tools of choice, but some thing along these lines:
#!/bin/bash
FILENAME=`date +"/Volumes/path/to/network/share/Backup/%Y-%m-%d.tgz"`
cd /directory/to/backup || exit 1
tar -cvz "$FILENAME" .
You can save that on your Desktop as backup and then go in Terminal and type:
chmod +x ~/Desktop/backup
to make it executable. Then you can just double click on it - obviously after changing the paths to reflect what you want to backup and where to.
Also, you may prefer to use some other tools - such as rsync but the method is the same.

How identify the source file as a hard link? Bash Script

I'm making a shell script that identifies hard links to a directory, but I need to know the source file.
example:
Ln origen1.txt destino1.txt
Ln origen1.txt destino2.txt
Ln origen1.txt destino2.txt
The output should be origen1.txt, because this is the source file for other hard links. This should be in bash. I need help, Thank you.
You can't. If you have a file file1, and you create a hardlink to it using ln:
ln file1 file2
Then the two files are indistinguishable. A "hard link" is really just the same thing as a normal file entry; it just happens to point to the same file as another entry. You can remove either one and you're back to having a single "hard link" to the file.
Like people have pointed out, hard links are all equivalent. However, you can use find to find all the hard links of a file:
find / -samefile destino2.txt
It won't say which link was the first one, but it will tell you all the possible candidates.

Copying directories recursively using shell script

Should be an easy question for the gurus here, though it's hard to explain it in text so hopefully this is clear. I've got two directories on a box with some flavor of unix on it. I've got a script that I want to use to move all the files and directories from one location to another.
First, an example of how the directories look:
Directory A: final/results/2012/2012-02/2012-02-25/name/files
Directory B: test/results/2012/2012-02/2012-02-24/name/files
So you see they're very similar. What I want to do is move everything from the Directory B 2012 directory, recursively, to the same level of Directory A. So you'd end up with:
someproject/results/2012/2012-02/2012-02-25/name/files
someproject/results/2012/2012-02/2012-02-24/name/files
etc.
I want this script to be future proof though, meaning I don't want the 2012 hardcoded. Also, towards the end of a month you will potentially have data from two different months and both need to be copied into the 2012 directory. So here is the command I used in the shell script file:
CONS="/someproject";
ROOT="/test";
/bin/cp -r ${ROOT}/results/* ${CONS}/results/*
but this resulted in:
/final/results/2012/2012-02/2012-02-25/name/files
and
/final/results/2012/2012/2012-02/2012-02-24/name/files
So as I hope is clear, it started a level below where I wanted it too. Can anyone fill me in on what I'm doing wrong, if they can understand what I'm even trying to explain. My apologies if it's not clear. I'm sure this is a fairly simple fix but I'm not sure what to do. Shell scripting is not a strong point of mine.
One poster suggests rsync, which is overkill.
cp -rp will work fine. if you want to move the files, just mv the directory -- it and everything under it will move too.
The only real problem here is the use of terminating *'s in the command line in the original script. You don't need the *, you're just trying to pass directories to the cp command, you aren't trying to pass it the names of all the files already in the source (and more importantly, the destination).
You could also use a tool like rsync to make sure your source and target are synchronized.
rsync -av ${ROOT}/results/ ${CONS}/results/
You specified that you want to "move" the files, though. Which means deleting the originals after they're copied:
rsync -av --remove-source-files ${ROOT}/results/ ${CONS}/results/
If you start playing around with rsync, be sure to read the man page about how it treats trailing slashes.

Bookmark Directories In Terminal

Looking for a solution to quickly navigate to long paths in a shell (particularly Max OS X Terminal.app).
Say my path is ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently
Instead of cd ~/This/Is/A/....
I would like to be able to store favorites/bookmark directories so I could do "cd myPath"
Are there any binaries or tools available to do something like this?
I've found the packages 'Apparix' and 'Goto' which together make the stuff dreams are made of for us terminal junkies.
Naturally, I had trouble installing Apparix, but I figured it out in the end.
How To Install Apparix on Mac OS X:
Download the tarball from Apparix's homepage.
Unpack the tarball, cd to the unpacked folder.
Run this command ./configure --prefix=$HOME/local && make && make install.
Run man apparix, scroll down to the heading BASH-style functions, copy everything within that section (delimited with ---) and paste it into ~/.bash_profile.
That's it. You should now have Apparix up and running on OS X (further install info and usage is on Apparix's homepage).
Another solution is to use Bashmarks, which allows you to this
$ cd ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently
$ s shortname # save current path as `shortname`
$ cd /
$ g shortname # cd to ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently
You can use aliases (stick them in your ~/.bash_profile if you want them to always load)
alias cd_bmark1='cd ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently'
Then use by just typing
cd_bmark1
into the console
I know you already found an answer that worked for you, but a couple of more lightweight suggestions that might help others looking for similar things
If your directories are relatively fixed, just long and far away from each other, you can use the CDPATH environment variable to add directories to the search path when typing the "cd" command. If the directory name you try to cd to isn't in the current directory, the other entries in your CD path will also be looked at (and it's also tab complete aware, at least in bash and zsh).
Switching to zsh rather than bash and using the excellent directory stacks abilities. With it, you can maintain a history of directories that you've visited, view the history with the "dh" alias, and easily switch to a directory by using quick shortcuts (ex: cd -3 to switch to the 3rd directory in your history stack).
Why not having a symlink ?
ln -s ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently bmark
cd bmark
I use to.sh daily to create and navigate bookmarked paths in bash. It supports tag autocompletion and the ability to easily add/remove bookmarks.
https://github.com/Grafluxe/to.sh
Full disclosure, I wrote this tool :)

Resources