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.

Getting the help for commands on Linux

Most GNU utilities have a ––help option that displays information about the utility.

 

$ cat --help

Usage: cat [OPTION] [FILE]...
Concatenate FILE(s), or standard input, to standard output.
-A, --show-all equivalent to -vET
-b, --number-nonblank number nonblank output lines
-e equivalent to -vE
-E, --show-ends display $ at end of each line

...

If the information that ––help displays runs off the screen, send the output through the less pager (page 31) using a pipe:
$ ls --help | less

Aborting Execution of jobs in Linux / Unix

To terminate a program from a character-based display, press the interrupt key (CONTROL-C or sometimes DELETE or DEL). When you press this key, the Linux operating system sends a terminal interrupt signal both to the program you are running and to the shell. Exactly what effect this signal has depends on the program. Some programs stop execution immediately; others ignore the signal. Some programs take other actions. When it receives a terminal interrupt signal, the shell displays a prompt and waits for another command.

If these methods do not terminate the program, try stopping the program with the suspend key (typically CONTROL-Z), giving the jobs command to verify the job number of the program, and using kill to abort the program. The job number is the number within the brackets at the left end of the line that jobs displays ([1]). The kill command sends a signal to the job specified as its argument. You must precede the job number with a percent sign (%1):

$ bigjob
^Z
[1]+ Stopped bigjob

$ jobs
[1]+ Stopped bigjob

$ kill %1
[1]+ Stopped bigjob

$ RETURN
[1]+ Killed bigjob



By default kill sends a software termination signal (–TERM). When this signal does not work, try using a kill (–KILL) signal:
$ kill -KILL %1

A running program cannot ignore a kill signal—it is sure to abort the program. The kill command returns a prompt; press RETURN again to see the confirmation message.


What are emulators for Linux?

Linux supports programs, called emulators, that run code intended for other operating systems. By using emulators you can run some DOS, Windows, and Macintosh programs under Linux. Wine (www.winehq.com) is an open-source implementation of the Windows API on top of X and UNIX/Linux; QEMU (fabrice.bellard.free.fr/qemu) is a CPU-only emulator that executes x86 Linux binaries on non-x86 Linux systems.

What do GNU stands for?

GNU, which stands for Gnu's Not UNIX, is the name for the complete UNIX-compatible software system which I am writing so that I can give it away free to everyone who can use it.

List of Environment variables in Linux


Variable name Stored information
DISPLAY used by the X Window system to identify the display server
DOMAIN domain name
EDITOR stores your favorite line editor
HISTSIZE size of the shell history file in number of lines
HOME path to your home directory
HOSTNAME local host name
INPUTRC location of definition file for input devices such as keyboard
LANG preferred language
LD_LIBRARY_PATH paths to search for libraries
LOGNAME login name
MAIL location of your incoming mail folder
MANPATH paths to search for man pages
OS string describing the operating system
OSTYPE more information about version etc.
PAGER used by programs like man which need to know what to do in case output is more than one terminal window.
PATH search paths for commands
PS1 primary prompt
PS2 secondary prompt
PWD present working directory
SHELL current shell
TERM terminal type
UID user ID
USER(NAME) user name
VISUAL your favorite full-screen editor
XENVIRONMENT location of your personal settings for X behavior
XFILESEARCHPATH paths to search for graphical libraries

Which shell am I using?

 

To find out which shell you are currently using, type the following command:

echo $SHELL

The result of that command will tell you what your current shell is and may look something like:

/bin/bash

That would mean that you are using the bash shell. On a side note, you would also realise that this shell resides in the bin directory.

Preserve permissions and ownership when copying files using cp

We use the -p flag to tell cp to preserve the permissions and ownership of the files involved. So if we wanted to copy a folder recursively preserving permissions and ownership of the folder and the files contained in it, we would use the following command (preferably as root):

cp -R -p /source/folder/ /destination/folder/

All the files in the source folder would be copied to the destination folder and the permissions and owners would be preserved.

If we had a single file it would be similar:

cp -p /path/to/file-a /path/to/file-b

To check that the owner and permissions have been preserved, we can simply use:

ls -altp /destination/folder/

The result of the above command would show us the permissions that the files in the destination folder have.