Tuesday, September 2, 2014

Unix-Pathnames-Listing Directories

Pathnames

pwd (print working directory)

Pathnames enable you to work out where you are in relation to the whole file-system. For example, to find out the absolute pathname of your home-directory, type cd to get back to your home-directory and then type
% pwd
The full pathname will look something like this -
/home/its/ug1/ee51vn
which means that ee51vn (your home directory) is in the sub-directory ug1 (the group directory),which in turn is located in the its sub-directory, which is in the home sub-directory, which is in the top-level root directory called " / " .
Unix File structure

More about home directories and pathnames

Understanding pathnames

First type cd to get back to your home-directory, then type
% ls unixstuff
to list the conents of your unixstuff directory.
Now type
% ls backups
You will get a message like this -
backups: No such file or directory
The reason is, backups is not in your current working directory. To use a command on a file (or directory) not in the current working directory (the directory you are currently in), you must either cd to the correct directory, or specify its full pathname. To list the contents of your backups directory, you must type
% ls unixstuff/backups

~ (your home directory)

Home directories can also be referred to by the tilde ~ character. It can be used to specify paths starting at your home directory. So typing
% ls ~/unixstuff
will list the contents of your unixstuff directory, no matter where you currently are in the file system.
What do you think
% ls ~
would list?
What do you think


% ls ~/..
 
ls is the most widely used command in unix or linux. ls command is used to list the contents of a directory. Learn the power of ls command to make your life easy. The syntax of ls command is
ls [options] [pathnames]
1. Write a unix/linux ls command to display the hidden files and directories?
To display the hidden files and directories in the current directory use the -a option of the ls command.
> ls -a
.  ..  documents  .hidden_file  sum.pl
Hidden files are the one whose name starts with dot (.). The las -a displays the current directory (.) and parent directory (..) also. If you want to exclude the current directory, parent directory, then use -A option.
> ls -A
documents  .hidden_file  sum.pl
2. Write a unix/linux ls command to classify the files with special characters
The -F option to ls command classifies the files. It marks the
  • Directories with trailing slash (/)
  • Executable files with trailing asterisk (*)
  • FIFOs with trailing vertical bar (|)
  • Symbolic links with trailing at the rate sign (@)
  • Regular files with nothing
> ls -F
documents/  sum.pl link@
3. Write a unix/linux ls command to print each file in a separate line?
The -1 option to the ls command specifies that each file should be displayed on a separate line
> ls -1
documents
sum.pl
4. Write a unix/linux ls command to display the inode number of file?
In some cases, you want to know the inode number of a file. Use -i option to the ls command to print the inode number of a file.
> ls -i1
10584066 documents
3482450 sum.pl
5. Write a unix/linux ls command to display complete information about the files?
The -l option provides lots of information about the file type, owner, group, permissions, file size, last modification date.
> ls -l
total 16
drwxr-xr-x 2 matt db 4096 Jan 30 23:08 documents
-rw-r--r-- 1 matt db   49 Jan 31 01:17 sum.pl
  • The first character indicates the type of the file. – for normal file, d for directory, l for link file and s for socket file
  • The next 9 characters in the first field represent the permissions. Each 3 characters refers the read (r), write (w), execute (x) permissions on owner, group and others. – means no permission.
  • The second field indicates the number of links to that file.
  • The third field indicates the owner name.
  • The fourth field indicates the group name.
  • The fifth field represents the file size in bytes.
  • The sixth field represents the last modification date and time of the file.
  • And finally the seventh field is the name of the file.
6. Write a unix/linux ls command to sort the files by their modification time?
The -t option allows the ls command to sort the files in descending order based on the modification time.
> ls -t1
sum.pl
documents
7. Write a unix/linux ls command to sort the files in ascending order of modification time?
The -r option reverses the order of the files displayed. Combine the -t and -r options to sort the files in ascending order.
> ls -rt1
documents
sum.pl
8. Write a unix/linux ls command to print the files recursively?
So far the ls command prints the files in the current directory. Use the -R option to recursively print the files in the sub-directories also.
> ls -R
.:
documents  sum.pl

./documents:
file.txt
9. Write a unix/linux ls command to print the files in a specific directory?
You can pass a directory to the ls command as an argument to print for the files in it.
> ls /usr/local/bin
10. Write a unix/linux ls command to display files in columns?
The -x option specifies the ls command to display the files in columns.

> ls -x

Changing to a different directory 

cd (change directory)

The command cd directory means change the current working directory to 'directory'. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.
To change to the directory you have just made, type
% cd unixstuff
Type ls to see the contents (which should be empty)

Unix-The directories

The directories . and ..

Still in the unixstuff directory, type
% ls -a
As you can see, in the unixstuff directory (and in all other directories), there are two special directories called (.) and (..)

The current directory (.)

In UNIX, (.) means the current directory, so typing
% cd .
NOTE: there is a space between cd and the dot
means stay where you are (the unixstuff directory).
This may not seem very useful at first, but using (.) as the name of the current directory will save a lot of typing, as we shall see later in the tutorial.

The parent directory (..)

(..) means the parent of the current directory, so typing
% cd ..
will take you one directory up the hierarchy (back to your home directory). Try it now.
Note: typing cd with no argument always returns you to your home directory. This is very useful if you are lost in the file system.

No comments: