Posts

Showing posts with the label Unix

Replace String in Unix

To replace all instances of a string in a directory (subdirectories included) do: Code: perl -e "s/FIND/REPLACE/g;" -pi.save $(find path/to/DIRECTORY -type f) The above will make a backup temp file of your original  If you do not want a temp file with the .save extension then do: Code: perl -e "s/FIND/REPLACE/g;" -pi $(find path/to/DIRECTORY -type f) -------------------- Example: You want to replace all instances of the word "design" with "dezine" in the directory /public_html/company/info you can execute the command from document root as  Code: perl -e "s/design/dezine/g;" -pi.save $(find public_html/company/info -type f) or you can execute the command from public_html/company/ (a directory above) as: Code: perl -e "s/design/dezine/g;" -pi.save $(find info -type f) ------------------------------ The above commands will search all files (.gif, .jpg, .htm, .html, .txt) so you might see some erro...

How to remove the dreaded '^M' character in unix files

While there are lots of ways to go about solving the issue, two methods that I find to my liking are: 1. Using 'tr' tr -d '^M' < [input filename] > [output filename] e.g; tr -d '^M' < opx_LOD_GEN_110_003110000.ini > test3.txt 2. Using vi editor. :%s/(ctrl-v)(ctrl-m)//g Simply type in the above in your vi editor to get the desired effect. And note that you have to press 'ctrl' AND 'V' and later 'ctrl' AND 'm'. It's not the same as the '^' and 'M' character. Both of them worked for me. Details can be found here: 1) http://www.tech-bits.com/index.php?View=entry&CategoryID=6&EntryID=54 2) http://www.devdaily.com/unix/edu/un010011/

What does “> /dev/null 2>&1″ mean?

Taken from http://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/ I remember being confused for a very long time about the trailing garbage in commands I saw in Unix systems, especially while watching compilers do their work. Nobody I asked could tell me what the funny greater-thans, ampersands and numbers after the commands meant, and search engines never turned up anything but examples of it being used without explanation. In this article I’ll explain those weird commands. Here’s an example command: wibble > /dev/null 2>&1 Output redirection The greater-thans ( > ) in commands like these redirect the program’s output somewhere. In this case, something is being redirected into /dev/null , and something is being redirected into &1 . Standard in, out, and error There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other pro...

Unix Sort

One of the most helpful webpages when it comes to how-to in using the sort command. Click me! (ok, so it wasn't a lot. but it does lead you to a good direction doesn't it.)

Not enough disk space

Should you ever see these types of message, there are several 'must-know' unix commands that can be used in order to start investigating. 1. du | sort -n : lists all file usage from smallest to largest. 2. df -k -b : see how much allocation is used in the directory. For more help, go to links below:- 1. An IT-admin guide to clearing up disk space 2. df unix command definitions and usage 3. du unix command definitions and usage

How to kill a Unix process

From http://www.ncl.ac.uk/iss/unix/unixhelp/kill.html and http://www.decf.berkeley.edu/help/unix/kill.html If you are logged in to a Unix system and your session "hangs" or "freezes" (i.e. nothing happens for a long time and it does not respond to Ctrl/Q Ctrl/C or Ctrl/D), you can attempt to unfreeze it yourself by "killing" the process which has stopped your session. You can also use Ctrl/Z to get out of a process in UNIX. Login to the system where your session is frozen Type the command: w usid where usid is your own username. This will produce a line of output for each login something like this: 9:53am up 82 day(s), 15:58, 132 users, load average: 6.50, 6.25, 5.70 User tty login@ idle JCPU PCPU what nabc pts/160 9:12am -csh nabc pts/166 9:53am w nabc Note the contents of the tty column for the frozen login -- in this example attached to pts/...

Unix 'Basename' command

The basename utility deletes any prefix ending in / and the suffix (if present in string) from string, and prints the result on the standard output. It is normally used inside substitution marks (``) within shell procedures. Syntax basename [string] [suffix] string - the leading part of the path name or sting suffix - additional remaining portion of name that if matches will be removed Examples basename myfile.txt .txt - Takes the basename of myfile.txt and strips the .txt portion and then just displays myfile. credits to: http://www.computerhope.com/unix/ubasenam.htm

How to Transfer a File into a Remote Desktop with shitty VPN connection

Few days back I was in this situation where by I had to transfer some files over to a remote desktop (aka the client's server). The shitty part I can't send files over through any websites or webmail; they've banned it all. No FTP softwares and the worst thing is, every time my manager sends them email with the attached packages; them packages are not sent through. Don't know where it got filtered..our site or theirs (I assume it's their's). Some thoughts are given also on mapping my drives so that it'll be accessible at the remote desktop; but some people have that said (read other blogs) the method only works if the remote desktop is not using Win2k and below. So again...i hit a wall. Anyhow, that's where this trick comes in. In essence you turn your file into something readable (read ASCII) and copy and paste the text over the remote desktop. One thing to note though is that not everything can be easily read by the notepad. Introducing 'uuencode...

The UNIX tr command

$> tr ',' '\012' return.txt tr is the program doing the work. ',' is the character I want to replace (in this case a comma). '\012' is the octal value for a newline in UNIX. comma .txt is the file I am reading from. return.txt is the new file I am saving my changes to. from : http://www.flashcodersny.org/wordpress/?p=19

Appending to Perl's @INC array

The @INC array is a list of directories Perl searches when attempting to load modules. To display the current contents of the @INC array: perl -e "print join(\"\n\", @INC);" The following two methods may be used to append to Perl's @INC array: 1. Add the directory to the PERL5LIB environment variable. 2. Add use lib ' directory ' ; in your Perl script. For more information, read the perlrun manpage or type perldoc lib . ripped from : http://www.brandonhutchinson.com/perl_inc.html

How to use diff in UNIX

The following were taken from http://www.unixtutorial.org/2008/02/compare-text-files-using-diff/. All credits goes to them. If you need to compare two text files in Unix, you're mostly likely to use the diff command. Today I'll talk about the simplest scenario: you want to compare two files and understand if there are any differences. Suppose you have two files in /tmp directory: /tmp/1.txt: aaa bbb ccc ddd eee fff ggg and /tmp/2.txt: bbb c c ddd eee fff ggg hhh I have deliberately created them so short and simple - this way it's easier to explain how the comparison works. If there are no differences between the files, you will see no output, but if two text files are indeed different, all the text mismatches will be highlighted using the standard diff output: $ diff /tmp/1.txt /tmp/2.txt 1d0 c c 7a7 > hhh Lines like "1d0" and "3c2" are the coordinates and types of the differences between the two compared files, while lines like " " and ...

Finding files in UNIX

The following guide is a direct rip-off from this website: http://www.hccfl.edu/pollock/Unix/FindCmd.htm All credits goes to him. The find command is used to locate files on a Unix or Linux system. find will search any set of directories you specify for files that match the supplied search criteria . You can search for files by name, owner, group, type, permissions, date, and other criteria. The search is recursive in that it will search all subdirectories too. The syntax looks like this: find where-to-look criteria what-to-do All arguments to find are optional, and there are defaults for all parts. (This may depend on which version of find is used. Here we discuss the freely available GNU version of find , which is the version available on YborStudent .) For example where-to-look defaults to . (that is, the current working directory), criteria defaults to none (that is, show all files), and what-to-do (known as the find action ) defaults to -print (that is, displa...