Terminal is a great application. And we usually use Terminal for editing scripts and invoking things. But what about invoking Terminal from, well, Terminal. For starters, let’s look at opening a Terminal session to the root of the boot volume (aka /):
open -a Terminal /
The -a option, when used with the open command, allows you to define which application that the item defined in the following position will open in. For example, you could open an XML file in Xcode
open -a Xcode /usr/share/postgresql/pg_hba.conf.sample
You could then open Terminal by passing other commands into the command. For example, to open a new Terminal window to the current working directory:
open -a Terminal `pwd`
Of course, you could accomplish the same thing with:
open -a Terminal .
Or pass the output of other commands through the open command. For example, the following command opens a new file in TextEdit that contains the output of an ls command:
ls | open -f
Adding -g to any of this leaves the new window in the background rather than bringing it to the foreground, which is the default behavior. Finally, open can also be used to open URLs, but I’ve covered that sort of use for open in the past
krypted December 21st, 2011
Posted In: Mac OS X
CLI, Command line, Lion, ls, Mac OS X, open a new terminal window from the command line, open command, pipe, pwd, terminal
My last article showed how to interface with the clipboard in Mac OS X
. Windows 7 comes with the same feature, but instead of pbcopy it’s simply clip. Since you don’t ls, we’ll pipe the output of dir into the clipboard:
dir | clip
Enjoy & no more complaining that I like one platform more than the other – you know who you are!
krypted January 15th, 2010
Posted In: Windows XP
clip, clipboard, Mac OS X, pasteboard, pbcopy, pipe, Shell, windows, Windows 7
The pipe (|) character is used to combine multiple commands. A pipe is a temporary storage place where the output of one command is stored and then passed as the input for a second command. Pipes are used to run more than two commands from the same command line. The sort command is used to sort data. When you run the ls –l command, you will see a listing of the files in a directory with each file shown on a separate line. When you use a pipe after the command and then sort your results, you will sort the data listed on the screen by the list command. The sort command has a variety of arguments that allow you to sort files using different options:
- -n, sorts by name.
- -n .3 Use the first field but the third position in that field.
- -r Sort from last to first.
- -c Check for whether the data has already been sorted.
- -i Ignore nonstandard characters.
- -f Use lowercase letters the same as uppercase would be used when sorting.
- -b Ignore blanks when they are found as the first letter in a string.
- -d Sort based on only letters, digits, and blanks.
- +POS1, +POS2, +POS3, etc. Sort by any position in a line.
A field in the sort command is what you are sorting by. The fields are separated by spaces. When you use sort –n +1, you are sorting the data by the second field of the name. To combine all of this, if you used the ls –l | sort –n +2, you would create a directory listing and sort the data according to the third character of a string of data. For example, the output of some files might end up being:
Input user data.xml
Input my data.xml
Input user files.xml
Input my files.xml
Input my password.xml
Use my password.xml
Another great use for the pipe is to combine the tail command with a pipe followed by a grep command to search for strings inside of files of a certain name or type. This can be helpful when reviewing logs for web servers. An example of the syntax for this command would be tail –f /var/log/httpd/access_log | grep 404, which would show an administrator all accesses to their websites that resulted in a 404 error page (provided the logs are working).
krypted October 11th, 2009
Posted In: Mac OS X, Mac OS X Server, Ubuntu, Unix
Command line, Linux, ls, Mac OS X, pipe, sort