Working with Files and Directories In Unix(Shell Scripting).
commands used in Unix in order to read, write and access a file and directories
Working with files and directories:
How to know the location where I am:
$ pwd
- pwd means present working directory.
To view the files and directory in present working directory:
$ ls
Options:
$ ls -l
$ ls -r
To Navigate one directory from another directory:
- By using cd (change directory) command
for example:
$ cd directory_name (to go to any any directory)
$ cd .. (Go back to previous directory)
$ cd ../.. (Go back to previous-to-previous directory)
$ cd ~ (go to parent directory)
To create a directory:
- By using mkdir(make directory) command.
Syntax: -
$ mkdir directory_name
# don't give space in name of directory if you give space, it will create two directory)
for example:
How to create a file:
There are 3 ways of creating a file
- By using touch command.
- By using vi editor.
- By using cat command:
1. By using touch command
- we can create an empty file by using touch command
e.g.:
$ touch file_name
2. By using vi editor:
- we can create a file by using vi editor and we can add up content too at the same time.
3. By using cat command:
- create a file.
- you could add new contents, append with existing content, overwrite the contents.
- read the file
By using cat command, you can do 3 operations:
> : Override
>> : Append
< : read
A) $ cat > filename
- kernel will check whether a file with the provided name is already present in the current pwd.
if yes, it is present
→ then it will re write or override the new contents to the existing contents
else No, the file is NA
→ then it will create a file with the provided name and write in the new contents - to mark the EOF (end of file) ctrl + d → close it
ctrl + c — forcefully quitting the operation.
e.g., →
B) $ cat < filename or cat filename
→ read the contents of the file
e.g., →
C) cat >> filename
- kernel will check whether a file with the provided name is already present in the current pwd.
- if yes, it is present
→ it will append the new contents to the existing contents.
else No, the file is NA.
→ it will create a file with the provided name and write in the new contents.
e.g., →
OPTIONS:
-n → number all the lines
-b → number of nonblank lines
-s →squeeze the multiple \n with a single \n.
How to remove directory :
- by rmdir command : it is used to remove empty directory.
- by rm -rf command : if directory contains some files then we can remove it forcefully by this command.
- Syntax of rmdir :
$ rmdir new_created_directory
for example :
Syntax for rm :
- “rm” command can be used to remove files and non empty directories.
$ rm options file_name
- Options :
for example :
$ rm -i file3
for e.g. :
Thank you…