Sunday, January 1, 2012

Unix : Some frequently used commands at glance

less: You might be familiar with ‘cat’. ‘less’ is a much better way to inspect large files on the command line. You’ll get a screen-full of text at a time, but no more. You can move a line or window up or down. You can search for a pattern by typing /pattern. When you’re done, hit q to exit the less viewer.

touch: ‘touch’ is basically for changing file access and modification times. But it’s a bonus that if the file doesn’t exist, it will create it.

history, !!, and !$: ‘history’ shows a numbered list of many of your recent commands on console. Then, to execute one of them, just type an exclamation mark and the history number. It’s even quicker to access the last command and last argument you used. For the latest command, use !!; the usual use case given for this is adding sudo to the front of a command. For the latest argument, use !$;

yes: ‘yes’ outputs argument forever. By default, it prints ‘y’ forever.

cal: Displays a very nice calendar with current date highlighted.

seq: This command prints sequences of numbers. User can define start number, interval, end number and many other options.

bc: A very precise calculator. Really useful for performing calculations on large numbers.

factor: A very good mathematical utility for finding prime integer factors.

stat: display file or file system status

tac: reverse of 'cat', print a file line by line in reverse order.

ldd: prints shared library information. In OS X, we use otool for the same purpose.

iostat: CPU and disk usage stats

lsof: List Open Files

netstat: show network status

PS: I've not given examples but linked to man pages on purpose, so that readers can learn by experiment.

Saturday, December 24, 2011

How to see hidden files in Linux via terminal

A filename that begins with a period is called an invisible filename (or an invisible file or sometimes a hidden file) because ls does not normally display it. The command ls –a displays all filenames, even invisible ones.

$ls –a
--shows all the files ….

Names of startup files usually begin with a period so that they are invisible and do not clutter a directory listing. The .plan file  is also invisible. Two special invisible entries—a single and double period (. and . .)—appear in every directory.

Options with the commands

 

--help – help
-a all
-c create
-f forced / file read or write
-i  interactively
-l long display
-r recursively
-q quiet
-v verbose

Friday, December 23, 2011

grep: Finds a String


The grep (global regular expression print) utility searches through one or more files to see whether any contain a specified string of characters. It does not change the file it searches but simply displays each line that contains the string.

Originally this utility's name was a play on an ed—an original UNIX editor, available on Linux—command: g/re/p. In this command the g stands for global, re is a regular expression delimited by slashes, and p means print.

Example

$ cat memo

Helen:In our meeting on June 6 we discussed the issue of credit.Have you had any further thoughts about it? Alex

$ grep 'credit' memo

discussed the issue of credit.

The grep command above searches through the file memo for lines that contain the string credit and displays a single line that meets this criterion. If memo contained such words as discredit, creditor, or accreditation, grep would have displayed those lines as well because they contain the string it was searching for. The –w option causes grep to match only whole words. You do not need to enclose the string you are searching for in single quotation marks, but doing so allows you to put SPACEs and special characters in the search string.

The grep utility can do much more than search for a simple string in a single file.


lpr, lpq and lprm : Prints a File

The lpr (line printer) utility places one or more files in a print queue for printing. Linux provides print queues so that only one job is printed on a given printer at a time. A queue allows several people or jobs to send output simultaneously to a single printer with the expected results. On machines with access to more than one printer, you can use the –P option to instruct lpr to place the file in the queue for a specific printer, including one that is connected to another machine on the network. The following command prints the file named report:

$ lpr report


Because this command does not specify a printer, the output goes to the default printer, which is the printer when you have only one printer.

The next command line prints the same file on the printer named mailroom:

$ lpr -Pmailroom report


lpq - Queue of print jobs


You can see what jobs are in the print queue by using the lpq utility:

$ lpq

lp is ready and printing

Rank Owner Job Files Total Size

active alex 86 (standard input) 954061 bytes


In this example, Alex has one job that is being printed; no other jobs are in the queue.

lprm – Remove the items from print queue


You can use the job number (86 in this case) with the lprm utility to remove the job from the print queue and stop it from printing:

$ lprm 86


You can send more than one file to the printer with a single command. The following command line prints three files on the printer named laser1:

$ lpr -Plaser1 05.txt 108.txt 12.txt

mv: Changes the Name of a File

The mv (move) utility can rename a file without making a copy of it. The mv command line specifies an existing file and a new filename using the same syntax as cp:

mv existing-filename new-filename


CP copies the file
$ ls
memo

$ cp memo memo.copy

$ ls
memo memo.copy


MV renames the file
$ ls
memo

$ mv memo memo.0130
$ ls
memo.0130

Moving the files from 1 directory to other


The mv utility can be used for more than changing the name of a file.

When used to move one or more files to a new directory, the mv command has this syntax:

mv existing-file-list directory
If the working directory is /home/alex, Alex can use the following command to move the files - names and temp from the working directory to the literature directory:
$ mv names temp literature

This command changes the absolute pathnames of the names and temp files from /home/alex/names and /home/alex/temp to /home/alex/literature/names and /home/alex/literature/temp, respectively. Like most Linux commands, mv accepts either absolute or relative pathnames.

Moving directories from 1 Directory to other


Just as it moves ordinary files from one directory to another, so mv can move directories. The syntax is similar except that you specify one or more directories, not ordinary files, to move:

mv existing-directory-list new-directory
If new-directory does not exist, the existing-directory-list must contain just one directory name, which mv changes to new-directory (mv renames the directory). Although directories can be renamed using mv, their contents cannot be copied with cp unless you use the – r option. Refer to the explanations of tar and cpio for other ways to copy and/or move directories.

Caution: mv can destroy a file

Just as cp can destroy a file, so can mv. Also like cp, mv has a –i (interactive) option.

cp: Copies a File

The cp (copy) utility makes a copy of a file. This utility can copy any file, including text and executable program (binary) files. You can use cp to make a backup copy of a file or a copy to experiment with.

The cp command line uses the following syntax to specify source and destination files:

cp source-file destination-file







The source-file is the name of the file that cp will copy. The destination-file is the name that cp assigns to the resulting (new) copy of the file.


Caution: cp can destroy a file

If the destination-file exists before you give a cp command, cp overwrites it. Because cp overwrites (and destroys the contents of) an existing destination-file without warning, take care not to cause cp to overwrite a file that you need. The cp – i (interactive) option prompts you before it overwrites a file.

The following example assumes that the file named orange.2 exists before you give the cp command. The user answers y to overwrite the file:

$ cp – i orange orange.2

cp: overwrite 'orange.2'? y


The cp command line in Figure 3-2 copies the file named memo to memo.copy. The period is part of the filename—just another character. The initial ls command shows that memo is the only file in the directory. After the cp command, a second ls shows two files in the directory, memo and memo.copy.

Sometimes it is useful to incorporate the date in the name of a copy of a file. The following example includes the date January 30 (0130) in the copied file:

$ cp memo memo.0130


Although it has no significance to Linux, the date can help you find a version of a file that you created on a certain date. It can also help you avoid overwriting existing files by providing a unique filename each day.

Use scp  or ftp  when you need to copy a file from one system to another on a common network.