Archive for the 'Lessons' Category

Want To Do a Quick Find/Replace? Use ’sed’!

If you are ever need to do just a quick and simple find and replace and don’t want to do it in an editor? You can use the sed command.

If you are familiar with vi, the find and replace string will be very familiar. The format of the find and replace is:

sed 's/oldstring/newstring/g' file.txt

However, if you are wanting these changes to be reflected in a file, you will need to perform an output at the end of your sed string, like so:

sed 's/oldstring/newstring/g' file.txt
> changedfile.txt

I have provided an example of how to use the principles below:

How To Change Login Screen Background Via Terminal

Sick and tired of the same old bluish background of the Mac OS X login screen? I have now found out a way to change it out. Simply modify/replace the following files:

/System/Library/CoreServices/DefaultDesktop
.jpg
(Leopard users)

/Library/Preferences/com.apple.loginwindow (Tiger users: modify this file)

Once this has been done, open a Terminal window and execute this line at the command prompt:

sudo defaults write
/Library/Preferences/com.apple.loginwindow
DesktopPicture '/Path/To/Picture.jpg'
(all on one line)

Once this has been done, log out of your account. When you log out, you should instantly see the changes that you have made.

Want Color in Your Text Editor?

If you are a programmer or a web designer, and want to have color designation to your coding without having to buy Dreamweaver or any other pricey HTML editors, I would recommend downloading Vim. Vim is a vi-based clone that allows you to view your code, HTML, CSS, etc. in a color-coded style.

This app can be downloaded by doing: sudo apt-get install vim

VIM can also be an alternative to using vi, which only displays code in monochrome. Here is a comparison of between the physical appearances of vi and vim:

(more…)

Having trouble finding text with grep? Try grepping with color

Having trouble finding a needle in a haystack? You can always use grep --color to find what you are looking for. This method is best used for finding a specific piece of text in a document that is all on one line, or a unique string of text within a repetitious document. I have provided the syntax for this grep option below:

grep --color "text" filename

Example:

LSPCI and LSUSB

The commands listed in this post are more for Linux than Unix. To find out information about the hardware of your computer, you can run an lspci command. Lspci will output the specifications of your hardware in a list style arrangement as shown below:

If you want to get more specific in your lspci output (e.g. wanting just to display a certain brand), you can always do lspci | grep -i "Productname" which will only return results that includes the string from your grep command. For example, if I wanted to find all of the hardware made by Broadcom, I would do lspci | grep -i "broadcom" and the results would return only hardware that mentions Broadcom.

(more…)

Need to find a SMALL amount of text in a BIG directory? No problem!

Have you ever tried to grep in a directory that has a vast amount of files and you received an error like this? (below)

If so, there is a simple way around this. Just use this: find . | xargs grep "yahoo" * | more. I will break down these processes for you:

  • find . - Find is a command that outputs the location of a file. In this case by specifying a ., the search will only be done in the present directory.
  • xargs - This allows a command to be executed from standard input. This also helps force the grep command (coming next) to return output, even when the directory is too large.
  • grep - The way we will be using this command is to search through a file for a specific string. If you are looking for straight text, it is best to use quotes (e.g. “text”) to help the command line decipher exactly what you are looking for. More info on grep can be found on this earlier post.
  • more - This command is optional for this process. More would be best used if you were expecting a full page of output returned from your search, and want to be prompted for the next page. This option would be very beneficial if the string you are searching for would be common in your results.

Apropos

Ever wanted to use a command to do something, but couldn’t quite figure out the actual command to accomplish what you need done? Try using apropos! Apropos is a way to browse through the help area of the Unix/Linux operating system for a command that will help achieve your goal. For example, if I wanted to find commands that relate to “secure”, I would enter apropos secure at the command line, and the kernel would return the results listed below along with any services that involve ’secure’:

As always, if you wanted to get a more elaborate description on a command or service, you can always use man (shortened for: manual). This will display a full description, and various options of a command which you have found using apropos.

Alias

The alias command is very useful. It is kind of a shortcut in some ways. I get tired of tying ls -la so I can see all the info for my files so what i do is type alias ls 'ls -la'. What this command does is that it sets up an alias so that every time I type ls the computer treats is as though I typed ls -la. There are many other things you can do also. Say you have a long command you run a lot. You can alias it to something like mc.

Ex: alias mc 'locate log_error | grep -R 192.168.1.1 *'

Your command can be as long or as short that you want to alias. I also use it to setup ssh connections.

Ex: alias con1 'ssh usrname@192.168.1.1'

Now any time I type con1 it will try to make an ssh connection to that ip address.

The diff command

The diff command is a very useful command to see the differences between two files. An example of this command would be

diff file1.txt file2.txt

It takes file2.txt and compares it to file1.txt and displays the differences that are in file2.txt. You may also want to use the -b options in the command because that will allow it to ignore differences in white space. This command is very useful in programming when you have a backup version of a script that you just modified. If the script no longer works it can help you track down bugs, by looking at backup version of the script that does work.

The kill command

Today we are going to look at a command that you don’t use very much in a Unix environment. The kill command. It is used when an application is stuck or not responding. The basic form of the command is kill [option]… PID. The PID is the process ID, you can get it by using the top command that I talked about previously. The most common option for the kill command is the -9 option. It will kill the process immediately.

Example

kill -9 2342

If you type top again you should see that the process that was associated with that PID is no longer running.