bash

Can a Bash script tell what directory it's in?

How do I get the path of the directory in which a bash script is located FROM that bash script. For instance, lets say I want to use a bash script as a launcher for another application. I want to change working directory to the one where the bash script is located so I can operate on the files in that directory like so: $ ./applicatio...

Advanced directory switching in bash

I know a few advanced ways, to change directories. pushd and popd (directory stack) or cd - (change to last directory). But I am looking for quick way to achieve the following: Say, I am in a rather deep dir: /this/is/a/very/deep/directory/structure/with\ lot\ of\ nasty/names and I want to switch to /this/is/another/very/deep/dir...

How can I send the stdout of one process to multiple processes using (preferably unnamed) pipes in Unix (or Windows)?

I'd like to redirect the stdout of process proc1 to two processes proc2 and proc3: proc2 -> stdout / proc1 \ proc3 -> stdout I tried proc1 | (proc2 & proc3) but it doesn't seem to work, i.e. echo 123 | (tr 1 a & tr 1 b) writes b23 to stdout instead of a23 b23 ...

How do I use a pipe in the exec parameter for a find command?

I'm trying to construct a find command to process a bunch of files in a directory using two different executables. Unfortunately, -exec on find doesn't allow to use pipe or even \| because the shell interprets that character first. Here is specifically what I'm trying to do (which doesn't work because pipe ends the find command): fi...

Splitting a file and its lines under Linux/bash

I have a rather large file (150 million lines of 10 chars). I need to split it in 150 files of 2 million lines, with each output line being alternatively the first 5 characters or the last 5 characters of the source line. I could do this in Perl rather quickly, but I was wondering if there was an easy solution using bash. Any ideas? ...

Error handling in BASH

What is your favorite method to handle errors in BASH? The best example of handling errors in BASH I have found on the web was written by William Shotts, Jr at http://www.linuxcommand.org. William Shotts, Jr suggests using the following function for error handling in BASH: #!/bin/bash # A slicker error handling routine # I put a va...

What is the best way to extract a version string from a file?

I want to use a file to store the current version number for a piece of customer software which can be used by a start-up script to run the binary in the correct directory. For Example, if the run directory looks like this: . .. 1.2.1 1.2.2 1.3.0 run.sh current_version And current_version contains: 1.2.2 I want run.sh to descend i...

What is your single most favorite command-line trick using Bash?

We all know how to use <ctrl>-R to reverse search through history, but did you know you can use <ctrl>-S to forward search if you set stty stop ""? Also, have you ever tried running bind -p to see all of your keyboard shortcuts listed? There are over 455 on Mac OS X by default. What is your single most favorite obscure trick, keyboa...

What are your most important console aliases?

Which one alias would you choose to keep if your .bash_alias/.bashrc/etc could only contain one line? ...

Grabbing every 4th file

I have 16,000 jpg's from a webcan screeb grabber that I let run for a year pointing into the back year. I want to find a way to grab every 4th image so that I can then put them into another directory so I can later turn them into a movie. Is there a simple bash script or other way under linux that I can do this. They are named like so.....

Delete all but the 4 newest directories

How would you do this in bash ...

how do you search for files containing dos line endings (CRLF) with grep under linux?

something like grep -IUr --color '\r\n' . the above seems to match for literal 'rn' which is not what is desired the output of this will be piped through xargs into todos to convert crlf to lf like this grep -IUrl --color '^M' . | xargs -ifile fromdos 'file' ...

Bash or Ksh ?

I'm not new to *nix, however lately I have been spending a lot of time at the prompt to do my development for my job. My question is what are the advantages of using Korn Shell or Bash Shell? Where are the pitfalls of using one over the other? Looking to understand from the perspective of a user, rather than purely scripting. ...

How can I create a command line (unix/linux) instruction that uses variables to execute numerous commands?

I need to rearrange some content in various directories but it's a bit of a pain. In order to debug the application I'm working on (a ruby app) I need to move my gems into my gem folder one at a time (long story; nutshell: one is broken and I can't figure out which one). So I need to do something like: sudo mv backup/gem/foo gem/ sudo ...

Redirect command to input of another in Python

I would like to replicate this in python: gvimdiff <(hg cat file.txt) file.txt (hg cat file.txt outputs the most recently committed version of file.txt) I know how to pipe the file to gvimdiff, but it won't accept another file: $ hg cat file.txt | gvimdiff file.txt - Too many edit arguments: "-" Getting to the python part... # hg...

What does $$ mean in the bash shell?

I once read that one way to obtain a unique filename in bash for temp files was to use a double dollar sign ($$). This does produce a number that varies from time to time... but if you call it repeatedly, it returns the same number. (The solution is to just use the time.) I am curious to know what $$ actually is, and why it would be ...

design patterns or best practices for shell scripts

Does anyone know of any resources that talk about best practices or design patterns for shell scripts (sh, bash etc...)? ...

What is a good equivalent to Perl lists in bash?

In perl one would simply do the following to store and iterate over a list of names my @fruit = (apple, orange, kiwi); foreach (@fruit) { print $_; } What would the equivalent be in bash? ...

Best practises for holding passwords in shell / Perl scripts?

I've recently had to dust off my Perl and shell script skills to help out some colleagues. The colleagues in question have been tasked with providing some reports from an internal application with a large Oracle database backend, and they simply don't have the skills to do this. While some might questions whether I have those skills eith...

What is the Bash command to create a hardlink to a directory in OS X?

How do you create a hardlink (as opposed to a symlink or a Mac OS alias) in OS X that points to a directory? I already know the command "ln target destination" but that only works when the target is a file. I know that Mac OS, unlike other Unix environments, does allow hardlinking to folders (this is used for Time Machine, for example) b...