Directory commands
Directory File: It is nothing but folders in windows. Since Unix treats everything as a file and will have hundreds and hundreds of files. From the management and accessing points of view these files have to be organized. Organizational details of files are stored in a file called directory file.Also directories can point to some other directories. These other directories are called sub-directories.
The HOME Directory
This the only directory into which a user enters automatically when he/she logs in. The Home directory can be found out using shell variable called HOME as shown below.
Example
- ~$echo $HOME/home/mypc //myps is the user-name~$
- ~$HOMEbash: /home/mypc Is a Directory~$
any variable which is prefixed by a $ symbol is evaluated by the shell.
The pwd Command: Finding the present working directory
When you logs in by default you are placed into home directory. You can move from one Directory to another, but at any point of time you are located in only one directory. The pwd command is used to know the directory in which you are working currently.
Example
- ~$pwd/home/mypc // This is my current working directory~$
cd: Changing the current directory
You can move around in the file system using cd command. When this command is used with an argument, it changes the current directory to the directory specified as argument.
Example
- ~$ pwd/home/mypc // myps is the current directory~$ cd Desktop/Programs // I am switching to Programs directory from my home directory~/Desktop/Programs$ pwd/home/mypc/Desktop/Programs //Now Programs is the current directory~/Desktop/Programs$ cd .. // cd with two dots switches to the directory, which is one level above the current directory~/Desktop$ pwd/home/mypc/Desktop~/Desktop$ cd // cd without argument simply switches to the home directory~$ pwd/home/mypc~$ cd / // cd with single forward slash( / ) switches to the root directory~$ pwd/ // Here a single forward slash( / ) represents the root directory/$
- /$ cd ~/ // It switches to home directory. The symbol tilde ( ~ ) Represents HOME directory~$ pwd/home/mypc~$cd /home/mypc/MyDirectory/Subdirectory1 // A absolute path( /home/mypc/MyDirectory/Subdirectory1) to Subdirectory1 is provided to cd.~/MyDirectory/Subdirectory1$ pwd/home/mypc/MyDirectory/SubDirectory1 // Now SubDirectory1 is my current directory~/MyDirectory/SubDirectory1$ cd /home/mypc/Desktop/Programs // Directly it switches me to Programsfrom SubDirectory1.~/Desktop/programs$ pwd/home/mypc/Desktop/programs
- ~/Desktop/programs$ cd ../../MyDirectory/SubDirectory1 // Switching back to SubDirectory1 using relative path-name~/MyDirectory/SubDirectory1$ pwd/home/mypc/MyDirectory/SubDirectory1~/MyDirectory/SubDirectory1$ cd ~/Desktop/Programs // It switches me to Programs. The symbol tilde (~ ) Represents HOME directory~/Desktop/Programs$
Here Desktop is subdirectory which is present under home directory and programs is the directory under the Desktop directory. And MyDirectory is also a subdirectory present under home directory, SubDirectory1 is a directory inside theMyDirectory.
The mkdir Command: Making Directory
The mkdir command is used to make one or more new directory. The command is followed by names of the directories to be created.
Example
- ~$mkdir MyDirectory //A directory named MyDirectory is created under the current directory.~$
- ~$mkdir Games Music video // Three directories named Games, Music, video are created under the current directory.~$
- ~$mkdir main main/sub1 main/sub2 main/sub2/sub3~$
The Third Example shows how to create directory--tree. Here main is a parent directory under which two subdirectoriessub1 & sub2 are created, And sub3 is a subdirectory which is created under sub2.
The rmdir Command: Removing Directories
This command is used to remove directories. Like mkdir, rmdir can also delete multiple directories in one shot.
Example
- ~$rmdir MyDirectory //A directory named MyDirectory is removed from the current directory.~$
- ~$rmdir Games Music video // Three directories named Games, Music, video are removed from the current directory.~$
- ~$rmdir main main/sub1 main/sub2rmdir: failed to remove 'main': Directory not empty~$
- ~$rmdir main/sub1 main/sub2 main // Now it removes complete directories tree.~$
From example 3 you can see the error message. rmdir has silently deleted the lowest level subdirectories sub1 and sub2, but not main directory. This error message leads two important rules of rmdir command.
- You can't delete a directory with rmdir unless it is empty.
- You can't delete a subdirectory unless you are placed in a directory which is hierarchically above the one you have chosen to remove.
The mkdir and rmdir commands work only in directories owned by the user. Generally the user is the owner of his/her home directory, and the user can create and remove subdirectories created by the user.
The ls Command: Listing directory content
The ls command is used to obtain a list of all filenames in the current directory.
ls [option] [filename] is the general syntax of the ls command.
Example
- ~$ls // ls without any option display the content of current directory.Desktop Downloads music Public VideosDocuments Pictures Templates~$
- ~$ls Desktop // ls with any Directory name as argument display the content of specified Directory.Programs Pictures Movies images~$
- ~$ls -l Desktop // ls with an option -l Display the long format listing.total 12drwxrwxr-x 4 mypc grp 4096 2014-08-19 16:50 maindrwxrwxr-x 3 mypc grp 4096 2014-08-19 17:00 subdirectory1-rw-rw-r- - 1 mypc grp 4096 2014-08-19 17:20 ordinaryfile1~$
As you can see the listing in third example. First file permission along with the file type (the first character always represent the file type. d represents directory file, hyphen ( - ) represents ordinary file , etc.) appears in the first column, second The link information appears in the second column, the user (owner) name appears in the third column, the group name appears in the fourth column, the size of the file appears in the fifth column, the date and time of creation or last modification appears in the sixth column, and finally the file name appears in the seventh column. And also it is observed that the word total 20. This means that all files listed above occupy a total of blocks on the disk.
Some useful Options that can be used with the ls command.
Option | Description |
|---|---|
| -F | When this option is used, the file names will be displayed with certain identification characters appended to them. Names of directories are followed by a slash ( / ). An asterisk ( * ) follows names of ordinary files taht contains executables, other types of ordinary files are unmarked. |
| -a |
Display all files including hidden files( ( dot ( . ) ,dot-dot ( .. ) and .bbn ) in current directory. You can also use -xa option to list the same in row-wise.
|
| -b | Display nonprinting characters in octal. |
| -c | Display files by file timestamp. |
| -r | In example one it has been shown that ls command without an option lists all files and directories under the current directory in the ASCII collection sequence of filenames in a column.The ls command with -r(reverse) option gives the same list in reverse order. |
| -R | Displays subdirectories as well. |
| -f | Interprets each name as a directory, not a file. |
| -t | The -t option is used to list the files on the basis of the creation or modification time in a long listing format. |
| -p | Displays directories with / |
| -u | The -u option is used to list the files on the basis of their access or usage time. |
| -i | Every file will have a unique non-zero positive integer number called the Inode number associated with it. The -i option along with the ls command is used to list Inode numbers of files in a directory. |
| -q | Displays all nonprinting characters as ? |
| -d | The details of present working directory or sub-directory right below the present working directory can be obtained using the -d option along with the ls command. |
| -m | Displays the names as a comma-separated list. |
| -n | Displays the long format listing, with GID and UID numbers. |
| -x | Displays files as rows across the screen. |
No comments:
Post a Comment