0% found this document useful (0 votes)
62 views12 pages

Session5 Handling Directories PDF

This document discusses shell programming and handling directories. It covers navigating directories using relative and absolute paths, creating directories with mkdir, copying directories with cp, renaming/moving directories with mv, and deleting directories with rmdir and rm. The document is intended to teach programmers how to perform common directory operations from the shell.

Uploaded by

Firuza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views12 pages

Session5 Handling Directories PDF

This document discusses shell programming and handling directories. It covers navigating directories using relative and absolute paths, creating directories with mkdir, copying directories with cp, renaming/moving directories with mv, and deleting directories with rmdir and rm. The document is intended to teach programmers how to perform common directory operations from the shell.

Uploaded by

Firuza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Shell Programming

A Necessity for all Programmers

Handling Directories
Nagesh Karmali | Firuza Karmali

Department of Computer Science and Engineering


IIT Bombay
You will learn to ...

• E

Shell Programming – A Necessity for all Programmers By Nagesh | Firuza 2/12


Understanding Path - Root \

Shell Programming – A Necessity for all Programmers By Nagesh | Firuza 3/12


Paths

• Absolute path: /home/user1/Desktop/ShellProgramming


• Relative path: user1/Desktop/ShellProgramming
(Assuming that the current directory is home)
• Navigating: cd
• One level up (Parent directory): ..
• Checking current directory: pwd

Shell Programming – A Necessity for all Programmers By Nagesh | Firuza 4/12


Navigating

• Navigate to directory
cd /home/user1/Desktop/ShellProgramming Errors
• Move one level (directory) up • If the directory does not
cd .. =⇒ /home/user1/Desktop exist
• Move 2 levels up • If filename is given instead
cd ../../ =⇒ /home of directory
• pwd =⇒ /home

Shell Programming – A Necessity for all Programmers By Nagesh | Firuza 5/12


Create Directory

• Create multiple directories


mkdir dir1 dir2 dirN
• Create parent and child directories (hierarchy): -p
mkdir -p shellprogramming/examples/session5
If any of the directory does exists, then it will be created

Shell Programming – A Necessity for all Programmers By Nagesh | Firuza 6/12


Copy Directory

• Copies directory: from src to dest


cp -r <src> <dest>
• Copies all source files/directories to dest directory
cp -r <src1> <src2> ... <srcN> <dest>
• Same filenames in the <dest> directory will be overwritten
• Confirmation to overwrite
• Use option: -i
• Prompts for each file whether to overwrite or not: y/n
• Errors
• If dest is a filename

Shell Programming – A Necessity for all Programmers By Nagesh | Firuza 7/12


Rename/Move directory

• Move src file/directory to dest directory


<src>: File/Directory name
<dest>: Existing destination directory or new directory name
mv <src> <dest>
• Move and rename file
mv <oldFile.txt> <destDir/newFile.txt>
• Moves all source files/directories to dest directory
mv <src1> <src2> ... <srcN> <dest>
• Errors
• If dest directory does not exist
• If dest is an existing file

Shell Programming – A Necessity for all Programmers By Nagesh | Firuza 8/12


Delete directory
• Deletes empty directories
rmdir <dir1> <dir2> ... <dirN>
• Deletes directories (recursively) and all its content
rm -r <dir1> <dir2> ... <dirN>
• Files/Directories deleted cannot be recovered
• Confirmation to descend and delete
• Use option: -i
• Prompts whether to descend and delete each file/directory y/n
• Errors (rmdir)
• If directory doesnot exist
• If directory is not empty
• If file is mentioned instead of directory
• Errors (rm)
• If directory doesnot exist
Shell Programming – A Necessity for all Programmers By Nagesh | Firuza 9/12
N

• N

Shell Programming – A Necessity for all Programmers By Nagesh | Firuza 10/12


Now, you can ...

• Us

Shell Programming – A Necessity for all Programmers By Nagesh | Firuza 11/12


Thank you
1

Shell Programming – A Necessity for all Programmers By Nagesh | Firuza 12/12

You might also like