OS X Shell - 'Clickable' script to erase files? - macos

I use Latex to write my documents. Latex creates MANY auxiliary files to compile a document. I often times want to clean my working directory.
While I worked on Windows, I used to keep a .bat file in the working directory that looked like this:
del *.aux
del *.pdf
del *.log
del *.bak
del *.gz
del *.bbl
del *.blg
which I could just click on to get rid of all auxiliary files in the current directory.
Now, I want to do the same on my Mac. I have created a .sh file like this:
#!/bin/bash
echo "Cleaning files..."
rm *.aux
rm *.bak
rm *.bbl
rm *.blg
rm *.gz
rm *.log
rm *.pdf
echo "Done!"
which I know I can run (i.e. invoke from command line), but I cannot click on - which is more convenient because not always I will be using Terminal.
I should stress the fact that the script should delete the files in the directory where it was clicked from!
How can I convert this script into a "clickable" one?
I appreciate any input!

Based on this reference (Getting the source directory of a Bash script from within), I ended up solving my problem with the following code:
#!/bin/bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
echo "Cleaning files..."
cd $DIR
rm *.aux
rm *.bak
rm *.bbl
rm *.blg
rm *.gz
rm *.log
rm *.pdf
echo "Done!"
#read -p "Press [Enter] to continue..."
It works very well to clean all the nasty files left behind by Texmaker!

See this answer for making a shell script double-clickable, but note that there is no concept of a "current directory" when you launch a script from the Finder.

Related

Equivalent to robocopy on MacOs - Copy full folder structure but with empty files

What I want to achieve
I would like to copy the structure of an existing folder including folders and files but I would like the size of the files to be 0. So it would create an empty file, with the same name and extension.
Windows
I know how to do it by running : robocopy source destination /create /e /xc /xn /xo
Question
What is the equivalent command I could use on Mac ?
Here is a script that might do what you want.
#!/usr/bin/env bash
source=$1
dest=$2
while IFS= read -rd '' file; do
mkdir -p "$dest${file%/*}" || exit
touch -r "$file" "$dest$file"
done < <(find "$source" -type f -print0)
Howto use
./myscript source/ destination/

Unix shell scripting

I have write shell script to find the particular filename in the directory and create list file to copy the filenames automatically in that list file. But my script is not working list file is not created automatically. I don't know the issue in my script.
Scripts='/app/file'
SrcFiles='/app/file/Mainfiles'
cd "$SrcFiles"
touch SOURCE.LIST
chmod 777 SOURCE.LIST
cd "$Scripts"
cd "$SrcFiles"
for f in *.csv
do
cp -v "$f" /app/file/Mainfiles/SOURCE.LIST/"${f%.csv}"
done
Please try below
search_dir="/app/file/Mainfiles"
for entry in "$search_dir"/*
do echo "$(basename $entry)" >> "/app/file/Mainfiles/SOURCE.LIST"
done

Create a subdirectory in bash based on $1

I have a HD full of directories named for CD albums which contain *.wav files.
I want a bash script that takes $1: mp3-squeeze kind_of_blue
and the script changes dir to "kind_of_blue",
creates a directory called $1_MP3 example: "kind_of_blue_MP3",
and lame outputs the mp3's to the newly created "kind_of_blue_MP3" directory.
Sadly, what I have creates "$album" not "kind_of_blue_MP3". Anyone have a solution?
#!/bin/bash
#to convert .wav to .mp3 using lame utility
album=$1
cd $album
mkdir -p '$album_MP3'
for i in *.wav ; do
echo $i
bname=`basename $i .wav`
lame --preset fast extreme $i $bname.mp3
done
mkdir mp3 && mv *.mp3 mp3
Wrong quotes. Plus, you have to end the variable name.
mkdir -p "${album}_MP3"

Remove files from one folder that contained in another folder

I'am trying to write simple script that will get files name from one folder and search them in another folder and remove if found them in that folder.
Got two folder like
/home/install/lib
/home/install/bin
/home/install/include
and
/usr/local/lib
/usr/local/bin
/usr/local/include
I want to remove all file's from /usr/local/lib{bin,include} that contains in /home/install/lib{bin,include}. For example having
/home/install/lib/test1
/usr/local/lib/test1
scritp will remove /usr/local/lib/test1. I tried to do it from each separate directory
/home/install/lib:ls -f -exec rm /usr/local/lib/{} \;
but nothing. Can you help me to manage with this simple script?
Create script rmcomm
#!/bin/bash
a="/home/install/$1"
b="/usr/local/$1"
comm -12 <(ls "$a") <(ls "$b") | while read file; do
rm "$b/$file"
done
Then call this script for every pair:
for dir in lib bin include; do rmcomm "$dir"; done
Here's something simple. Remove the echo from the line containing rm to run it after you've ensured it's doing what you want:
#!/bin/bash
dirs[0]=lib
dirs[1]=bin
dirs[2]=include
pushd /home/install
for dir in "${dirs[#]}"
do
for file in $(find $dir -type f)
do
# Remove 'echo' below once you're satisfied the correct files
# are being removed
echo rm /usr/local/$file
done
done
popd

What is causing "Directory not empty" errors?

I've written a script that is mostly working for me. However, the two rmdir commands at the end are sometimes returning Directory not empty errors. How do I beat this? I tried adding r and then -rf which returned Illegal option errors.
I'm a rookie, and I'd be grateful for any help. Here is the full shell script, which, again, is mostly working beautifully:
if [[ -z "${1}" ]]; then
die "FolderName Required"
fi
newDirName="DirectoryName"
newBaseDir="/Users/JSG/Desktop/DataFarm/$1/"
/bin/mkdir -p $newBaseDir/{ProtectedOrig,Data}
echo -n "---Data Folder Setup
---Data Introduction
---Data Audit/Manipulation
---Data Queries" > $newBaseDir/Data/$1_DataJournal.txt
ditto NewData/ NewDataCopy
fab deploy_data_to_s3:data=*
mv NewData/ $newBaseDir/ProtectedOrig/NewData
mv NewDataCopy/ $newBaseDir/Data/NewDataCopy
mv $newBaseDir/Data/NewDataCopy/* $newBaseDir/Data/
rmdir $newBaseDir/Data/NewDataCopy
mv $newBaseDir/ProtectedOrig/NewData/* $newBaseDir/ProtectedOrig/
rmdir $newBaseDir/ProtectedOrig/NewData
chflags -R uchg $newBaseDir/ProtectedOrig/
mkdir NewData
What am I missing? And thanks in advance!
For the rmdir command, you need to add the --ignore-fail-on-non-empty flag so it deletes the directory even if files are in there like so:
rmdir --ignore-fail-on-non-empty $newBaseDir/Data/NewDataCopy
You could also just use rm -r too:
rm -r $newBaseDir/Data/NewDataCopy
From the Wikipedia Entry:
rmdir will not remove a directory if it is not empty in UNIX. The correct way to remove a directory and all its contents recursively is with the rm command.
Check for any files in the directory that start with .. I note you're moving *, but if there's a file called, for example, .hello, then * will not match this file and as a result the directory will not be empty when you come to do an rmdir.

Resources