I also use this natty little function in my .bash_profile to cd to the last opened finder location.
This is a script written by a colleague. http://www.visualcortex.co.uk/.
Add the following function to your .bash_profile in Apple OSX. Now open a directory in Finder, then open terminal and type cdf, your terminal will now change directory to the same one as the last Finder opened.
# function to change directory to the one set in the last opened finder.
cdf () {
currFolderPath=$( /usr/bin/osascript <<" EOT"
tell application "Finder"
try
set currFolder to (folder of the front window as alias)
on error
set currFolder to (path to desktop folder as alias)
end try
POSIX path of currFolder
end tell
EOT
)
echo "cd to \"$currFolderPath\""
cd "$currFolderPath"
}
It gives you an icon in Finder that changes the directory in your terminal to the one in the Finder window. There are some options, like have it open a new terminal, or change the folder using pushd, so you can popd once your done and return to the folder it originally was in.
I like the script you provided, though, as it saves me from having to use the mouse (no button click) and I can easily select which terminal to do it in. Note that I had to remove the quotes around the first EOT to get it to work.
Nothing against this post in particular, but it's much more useful in the long term to learn how to write stuff like this yourself. Compare knowing how to cook with just having lots of recipes.
While there are certainly some people who will pick apart examples like these and learn new tricks, I've seen enough copy-and-paste programming to know that many won't.
I have used production batch processing systems that routinely store enough files that
ls *5*
will fail because the argument list was too big for execve(2). Would I design it that way? No, but people do (sometimes without really thinking about it), and you still have to get work done.
New users should be taught find(1) because it always works.
Right, I understand the problem. Find and xargs can do wonders. However, that's usually an edge case (in my professional experience). I'm certainly not proclaiming the advanced-ness of 'ls' in this circumstance.
However, in the example in the article, he was using "ls" piped to "grep" in order to find all files with a 5 in the name. That would suffer from the same consequences you mentioned, on such systems, and is inefficient and verbose.
Note, I agree with your advice to teach "find" (and "xargs") to users.
Sorry, I got so caught up the frustration that I forget to post that as my real frustration. :P That was exactly what I first thought when I saw that, and was posting the `find` calls as just a easier / more comprehensive method of looking, esp when you start wanting recursive searches. The fact that you can add tons of different search filters to `find` that you can't have with `ls` really make it the goto way of finding files, rather than piping ls to some form of grep or whatnot.
I find it very much easier to just enter irb or run ruby with -e and hack around with backquoted strings instead of using bash to loop through and manipulate filenames.
a silly usecase would be reversing all the file names:
And regardless of whether it's necessary in the first place, why is it a bash script rather than a sh script? Very few of these use bash-specific extensions, and the BSDs don't necessarily have bash installed.
Things like that make porting from Linux to BSD and other Unices tons of fun.
Here is a one-line example, given two parameters. $1 is the archive name prefix and $2 is the directory to archive.
tar -cvzf ${1}-$(date +%Y%m%d-%H%M).tar.gz $2
Note, if $2 contains path information (such as absolute path), that path will be stored in the tar. So it is usually used with the working dir as the parent of $2.
Is there any way to give grep some context? It's great knowing what file a word came from, but I've never figured out how to get grep to spit out the leading and following lines too.
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a
line containing a group separator (--) between contiguous groups of
matches. With the -o or --only-matching option, this has no effect and
a warning is given.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines. Places a
line containing a group separator (--) between contiguous groups of
matches. With the -o or --only-matching option, this has no effect and
a warning is given.
-C NUM, -NUM, --context=NUM
Print NUM lines of output context. Places a line containing a group
separator (--) between contiguous groups of matches. With the -o or
--only-matching option, this has no effect and a warning is given.
CommandLineFu is a much more generalized command-line collection, has a few clever ones. Not just web developers, but anyone who uses *nix in general. Specially the top rated ones
Just a little warning: The "for i in..." paradigm breaks if the strings you're iterating over have spaces in them. The way around that is to either use find and/or xargs instead of "for i in", or to first set the IFS variable to a value that doesn't contain spaces.
This is a script written by a colleague. http://www.visualcortex.co.uk/. Add the following function to your .bash_profile in Apple OSX. Now open a directory in Finder, then open terminal and type cdf, your terminal will now change directory to the same one as the last Finder opened.