Unix Directory :
In previous articles I explained about basic unix commands and how to work with unix files with examples.In this article i will give how to work with unix directory with real life examples.You can create directory in unix,can be able to move directory,place files in directory,copy the files in directory using multiple commands.I will try to explain each and every command related to directory in this article.Unix uses hierarchical structure of files and directories.
“A directory in simple terms is a file or folder which contains different files and folders.”
Unix Directory with examples :
Home Directory (/) :
When user first log in to the unix server,the specified directory is called as Home directory.Home directory is Unix Directory which is indicated by / sign.User needs to do much more work in home directory only.
To Go to Home Directory from any location:
Command :
$cd ~
$
To Go to any users Home Directory :
Command:
$cd ~ username
$
To go into a last directory then use following command:
Command :
$cd –
/home/amit/linux
PWD Command :
PWD stands for Present Working Directory.This is most used linux command to see the specific Unix Directory on which the user is working on.
Command:
$pwd
/home/amit/linux
$cd .. To come out from current working directory.
Command :
$cd ..
/home/amit/
$ cd / :- It changes to root directory.
Command:
$ cd /
Then
$ pwd
$/ [directly comes to root directory]
Question: How to shift from Root directory to User (Home) directory?
Answer:
E.g.– $ cd Directory_name
$ cd home
$/home/Administrator
Question: How to see directories?
Answer:$dir
To See the list of Directory names following command is used
Command :
$Ls Directory name
$Ls home/amit
Linux
Create Directory in Unix/ Mkdir Command :
user can create the directory using Mkdir command in unix.
Syntax :
$Mkdir Directory_name
Example:
$Mkdir Amit
Mkdir command is used to create Unix Directory in present working directory.If user wants to create directory on the specific path then just use following syntax:
Syntax :
$Mkdir path/Directory_name
Example:
$Mkdir usr/bin/Amit
The above statement will create directory in usr/bin folder.Mkdir command produces no output if successfully created directory.
Creating multiple directories :
User can create multiple directories in unix using following command.
Syntax :
$Mkdir directory1 directory2….directory..n
Example:
$Mkdir Amit Rahul
Above statement will create 2 different directories named ‘Amit’ and ‘Rahul’ in present working directory.
Creating Parent directory:
Sometimes user wants to create a directory where the specified directory is not exist.Means user is trying to create parent directory.
$mkdir /tmp/Pradnya/Unix mkdir: Failed to make directory "/tmp/Pradnya/Unix"; No such file or directory $
If user is facing above error then user needs to use -p option which is used to create parent directory and all the necessary directories directly.
Example :
$mkdir -p /tmp/Pradnya/Unix $
The above statement will create the directory named ‘Pradnya’ in tmp folder and ‘Unix’ in ‘Pradnya’ folder.
rmdir command for removing directory:
To remove directory user will have use rmdir command which stands for ‘Removing Directory’.Before removing directory user needs to check that the specified directory is empty or there is no any file or subdirectory inside that directory.
Syntax:
$rmdir directory_name
Example:
$rmdir Amit
$
The above statement will remove the directory named ‘Amit’.
User can remove multiple directories at a same time using following command:
Syntax:
$rmdir directory_name1 directory_name2…..directory_name..n
Example:
$rmdir Amit Rahul
$
The above statement will remove 2 directories named Amit and Rahul.Rmdir command produces no output if it successfully runs and removes the directory.
$ rmdir:- It removes a directory but directory must be empty.
E.g. $ rmdir Directory_name
$ rm – r Directory_name:-It deletes recursively entire directory structure
$ rm – ri Directory_name:- It deletes recursively entire directory structure with confirmation
$ rm – rf Directory_name:- It deletes recursively entire directory structure with forcibly
CD command for Changing Directories:
One of the most used command in unix is Cd which changes the directory path.User can be able to go to any specified path using cd command.
Syntax:
$cd directory-path
$
directory path is path of directory which you want to change.
Example:
$cd usr/amit/bin
$
The above statement will go to the bin directory and user will able to work in ‘Bin‘ Directory.
Renaming Directories using mv command :
Using mv command user will rename the directory name.
Syntax :
$mv Old_directory New_directory
Example :
$mv Amit pradnya
The above mv statement is used to rename the directory named ‘Amit’ to ‘Pradnya’.
Copying files:-
$ cp :- To copy a file.
E.g.– $ cp Source_File Target_File
Note:Source file must be existing file and target file may be a new file/existing file.
$mv: To Rename/Movea file or directory.
E.g.
$ mv old_file new_file (old file is renamed/moved to new file)
$ mv old_directory new_directory (old directory is renamed/moved to new directory)
For any confirmation use (-i)
Note:–
$ cp àCopy & Paste
$ mv àCut & Paste
Creating Hidden files:
Any file_name/directory_name starts with ‘.’ is hidden file/directory.
E.g.– $ cat > .file_name $ mkdir .directory_name
—————-
Ctrl+d [To save & close file]
$ mv emp .emp àHide existing file
$mv .emp emp à Unhide existing file
$ mv abc .abc à Hide directory
$ mv .abc abc à Unhide directory
Viewing list of files:-
$ ls àIt list current directory all files & sub – directories in ascending order based on ASCII Values.
$ ls -a àIt list all files along with hidden files.
$ ls -r àIt list all files in reverse order (Descending)
$ ls – R àIt list all files recursively.
$ ls – t àIt list all files based on date & time of creation.
$ ls – l àIt list all files in long list format. i.e.9 fields.
$ ls – l File_Name àIt list the file in long list format. i.e.9 fields.
What is .(dot) and . .(dot dot) in Unix :
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.
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).
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. And, cd / takes directly to Root Directory.
Click on Topic You want to learn:
- History of SQL
- SQL Create Table(DDL in SQL)
- SQL DML Statements(INSERT,UPDATE,DELETE)
- SQL Select Statement Execution
- Operators in SQL
- Views in SQL
- Materialized View in SQL
- Joins in SQL
- Inner Join / Outer Join
- Full Outer Join / Cartesian Join
- Union and Union ALL
- Intersect and Minus
- Indexing in SQL
- Rank and Dense Rank
- SubQueries and Correlated Subqueries
- Parser and Optimizer
- Oracle 11 G new Features
- SQL Functions List
- Constraints in SQL
- Database Normalization
- Table Partitioning
- Pivot in SQL
- Difference Between Truncate,Delete and drop
- Oracle System Tables
Unix Tutorials :
4.Create File in Unix using multiple ways
12.paste Command
Hope Everyone will get idea about these basic unix commands related to directory.If you like this article dont forget to comment in comments section.