Monday, 3 December 2012

Frequently used Unix commands

ls --- lists your files 
  • ls -l --- lists your files in 'long format', which contains lots of useful information, e.g. the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified. 
  • ls -a --- lists all files, including the ones whose filenames begin in a dot, which you do not always want to see. 
more filename --- shows the first part of a file, just as much as will fit on one screen. Just hit the space bar to see more or q to quit. You can use /pattern to search for a pattern.

diff filename1 filename2

chmod options filename
File Compression gzip filename --- compresses files, so that they take up much less space. Usually text files compress to about half their original size, but it depends very much on the size of the file and the nature of the contents. There are other tools for this purpose, too (e.g. compress), but gzip usually gives the highest compression rate. Gzip produces files with the ending '.gz' appended to the original filename.

gunzip filename --- uncompresses files compressed by gzip.

mkdir dirname --- make a new directory

cd dirname --- change directory. You basically 'go' to another directory, and you will see the files in that directory when you do 'ls'. You always start out in your 'home directory', and you can get back there by typing 'cd' without arguments. 'cd ..' will get you one level up from your current position. You don't have to walk along step by step - you can make big leaps or avoid walking around by specifying path names.

pwd --- tells you where you currently are.

grep string filename(s) --- looks for the string in the files. This can be useful a lot of purposes, e.g. finding the right file among many, figuring out which is the right version of something, and even doing serious corpus work. grep comes in several varieties (grepegrep, and fgrep) and has a lot of very flexible options. Check out the man pages if this sounds good to you.

who --- tells you who's logged on, and where they're coming from.
touch--creates a file

whoami --- returns your username. Sounds useless, but isn't. You may need to find out who it is who forgot to log out somewhere, and make sure *you* have logged out.

kill PID --- kills (ends) the processes with the ID you gave. This works only for your own processes, of course. Get the ID by using ps. If the process doesn't 'die' properly, use the option -9. But attempt without that option first, because it doesn't give the process a chance to finish possibly important business before dying. You may need to kill processes for example if your modem connection was interrupted and you didn't get logged out properly, which sometimes happens.

telnet hostname --- also lets you connect to a remote host. Use rlogin whenever possible.

man commandname --- shows you the manual page for the command

nohup---. If you want a background process to continue running  even after you log out, you have to use the 'nohup' command to submit that background command.

Use the following format:
   nohup command &

Notice that you place the nohup command before the command you intend to run as a background process.

For example, suppose you want the grep command to search all the files in your current directory for the string word and redirect the output to a file called word.list, and you want to log out immediately afterward. Type the command line as follows:
   nohup grep word * > word.list &

You can terminate the nohup command by using the kill command.

top---Top shows how much processing power and memory are being used, as well as other information about the running processes.

whereis---It locates the binary, source, and manual page files for a command

cut---This command in unix (or linux) is used to select sections of text from each line of files. You can use the cut command to select fields or columns from a line by specifying a delimiter or you can select a portion of text by specifying the range or characters. Basically the cut command slices a line and extracts the text.
You can use the cut command just as awk command to extract the fields in a file using a delimiter. The -d option in cut command can be used to specify the delimiter and -f option is used to specify the field position.

cut -d' ' -f2 file.txt

No comments:

Post a Comment