Sie sind auf Seite 1von 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

Das könnte Ihnen auch gefallen