Tuesday, September 20, 2011

UNIX Command: Looking into files.

file - to identify file types
head- peak at the first few lines
tail - view last few lines
cat - view contenst of file
more - view larger files.


file:
A program that can easily offer you a good hint as to the contents of a file by looking at the first few lines. The disadvantage of this commnad is, it is not 100% accurate
eg:- A text file has executable permission and the initial contents of the files look like a C program the file command interpret it as an executable program rather that an english text file.

% file newtext.txt helloworld.txt 'shiyas resume.doc'
newtext.txt: ascii text
....
...
photos: directory

% file *
will analyze all files in home directory
 
head: Use it to view up to the first few hundred lines of a very long file, actually. You can specify the number as well.
% head newfile.txt
first few lines are displayed

% head -4 newfile.txt
first 4 lines are displayed

Can supply multiple files as well
% head -4 newfile.txt passwd
=====>passwd<====
......................................

=====>newfile.txt<===
.......................................


Can be used along with pipes (|):
% who | head -5
the output of who is supplied to head -5 and lists down first 5 users in the list.


tail:
Provide last lines of the file.
combine the two, head and tail, so you can see just the tenth, eleventh, and twelfth lines of a file?
%

cat:
flags: -v -s
head -12 .cshrc | tail -3

Monday, September 19, 2011

UNIX questions

What is the output of % echo $DATE?
Ans: Nothing is displayed.

What is the output of %date?
ans: date with time

In my directory /u/home/phoenix I have one file name magazine.txt and a directory magazine. What will be the output for ls -l magaz* ? Will it list both the file and directory or only one among them? If one, which one?

ls -l magaz* will list the magazine.txt file only.
If you need to see the directory then you shoudl run ls -ld magaz*

Does the command mkdir has arguments?
ans: No

What if you create a directory with same name as one that exist already?
ans: Will throw error saying file already exist

umask is set to 0222, decode the defaul permission for this value.
ans:
The value for umask is just opposite of what the real privilege is set, hence
0 indicates owner has all access except execute : rw-
2 indicates group has all privilege except write and execute: r--
2:r--
Hence finally we have: -rw-r--r--

What is command use for moving a file?
ans: mv

What is the command used for renaming a file?
ans: mv

I have a directory in which there are many directories and files and some are as below:
diectories:DIR1, DIR2,DIR3
files:DIR1.txt, DIR2.txt
there are no files in DIR1 and DIR2, whereas DIR3 has two files hello.txt and hi.txt
Now i ran ls- l DI*, what will be the output?

ls -ld DI* what will be the output?

What is the result, cp newdir latestdir, where newdir is a directory?
cp: omitting directory 'newdir'

what is the resutl, cp newfile.txt
ans: cp: missing destination file operand after newfile.txt
Try 'cp --help' for more information.

What is the result on executing umask 77777?
bash: umask: 777777: octal number out of range

I have changed the umask value from the default one (022) to 777 and then I exit from the terminal and logged in again. What will the value of umask.

ans:022.
Each time you login to the terminal umask value get reset always.

Why Shell Scripts always require both read and execute permission?

Any program works fine in UNIX even it has only execute permission. By giving execute privilege alone for any program we make sure that, the users of this particular program has the permission to run the program but not to examine or copy or modify the code that we have written.

Shell scripts belongs to a special class of programs, which fails to execute if only execute privilege is granted on those. Why is this becasue, shell script, which acts as a UNIX command line macro facility, which helps in clubbing togther a series of commands into a single file and run them as a single program. Hence for the shell to execute this program (or commands inside the file), it need to read the file first and then execute.

So shell script always need both read and execute permissions!!

UNIX Commands

ls : List files in a directory

ls -a : List hidden dot files as well in a directory

ls -C -F: List files with slash ('/') for current driectory

ls -C -F /: List files with slash ('/') for root directory

ls -l: List files in long format.

ls -lr: List files in long format and in reverse order based on file/directory name.

ls -lt: List files in long format and ordered based on file/directory modified date. The most recently accessed files.

ls -lrt: List files in long format and in reverse order based on file/directory modified date.

ls -s: List the filenames along with its size. The total size of all files in the directory is given in the first line. Size is given in Kilobites rounded upwards.

ls -s will list all the files under the current directory and its size as well. If you are intereseted to see size details of any specific directory you can give the command as below
ls -s <dirname>

we can specify the filename as well
ls -s <filename>

You can specify multiple filenames or dierctories as well and seperate them by space.

ls -s <dir1> <dir2> <file1>
Output:
10 file1

dir1:
 total 456
     2 file1
     4 file2
     etc...
dir2:
 total 100
     45 file1
     50 file2
     etc...
ls -f: Suffices the file type at the end of the filename
Different types of suffices
       - / indicates directory
       - * Indicates program
       - @ indicates a symbolic link to another file or directory.

ls -m: List the contents of a directory comma seperated.

ls -1: List the content of directory in a single column.


-a
List all files, including any dot files.
-F
Indicate file types; / = directory, * = executable.
-m
Show files as a comma-separated list.
-s
Show size of files, in blocks (typically, 1 block = 1,024 bytes).
-C
Force multiple-column output on listings.-1 Force single-column output on listings.
ls -x: Change the default sorting order from column first then row to row first then column

ls -


pwd- stands for present working directory and will  display the absolute path of your current directory.

bc: Used for infix mathematical calcualtions.
$bc
12*4
48
quit
$


-1
Force single-column output on listings.
-a
List all files, including any dot files.-C Force multiple-column output on listings
-d
List directories rather than their contents.
-F
Indicate file types; / = directory, * = executable.
-l
Generate a long listing of files and directories.
-m
Show files as a comma-separated list.
-r
Reverse the order of any file sorting.
-R
Recursively show directories and their contents.
-s
Show size of files, in blocks (typically 1 block = 1,024 bytes).
-t
Sort output in most-recently-modified order.-x

Touch Command:
helps you create new files on the system
The main reason that
updated, as the following example demonstrates.
touch is used in UNIX is to force the last-modified time of a file to be
%
ls -l iecc.list
-rw------- 1 taylor 3843 Oct 6 18:02 iecc.list
%
touch iecc.list
%
ls -l iecc.list-rw------- 1 taylor 3843 Oct 10 16:22 iecc.list

If you try to use the
creates the file:

Compress:
compress textfile.txt

compress -v textfile.txt

Uncompress:
uncompres textfile.txt / uncompress textfile.txt.z
touch command on a file that doesn’t exist, the program
Sort output in row-first order.
Flag Meaning

mv
% ls -1
Payroll
newfile.txt
photos
newphotos

% mv newfile.txt latestfile.txt
% ls -1
Payroll
latestfile.txt
photos
newphotos

All the contents of the newfile.txt is moved into the latestfile.txt and latestfile.txt is removed from directory. The same is applicable in the case of directories also. We can entirely move the content of a directory into a new one. If you look closer you can find that this is nothing but just renaming the folder or file. Then what is actual moving of file or dirctory?

% mv newphotos ./Photos
% ls -1
Payroll
latestfile.txt
helloworld.txt
Photos
% ls -1 ./Photos
newphotos

Here the entire newphotos directory is moved into Photos directory. Similarly we can move the files as well from directory to directory.

What if I execute the below query?
% mv latestfile.txt helloworld.txt
ans: The contents of latestfile.txt is overwritten into helloworld.txt. A popup will come before overriding for which if give 'y' as input itwill override else wont.

rmdir:
rmdir is used to remove any directory. One advantage with rmdir is that, it removes only empty data, hence no need to worry on deleting the content of a directory unknowigly.
On executing rmdir against a directory which is not empty we get a message as below.
rmdir: failed to remove 'dir': Directory not empty

rm:
Used to remove files. Cant be used for directory (will throw error). This command removes any file. This command removes the file permanantly, cant be restored at any point of time. You can remove multiple files at a time.
For the safe side always use -i flag along with rm command, where i stands for interactive, which will ask us before removing the file
rm -r  is a dangerous one as it will delete files recursively

How could you check, what type of login shell you're running?
% grep pnaraya /etc/passwd
pnaraya:x:1000:1000:pnaraya,,,:/u/pnaraya:/bin/bash

on the password entry the login shell is specified. (/bin/bash)