Terminal Directory Operations Commands

Terminal is an application that runs commands given by user, actually there is a special program inside Terminal called Shell – which reads in commands and execute them on Mac OS operating system.Terminal’s only job is to open windows and manage shells, while shells actually do real work of Command Execution. You can enter commands in Terminal and then Shell Program running inside terminal will execute those commands.

Commands are a kind of way to interact with Operating System and File System. These can be used for creating files/directories or checking status of processes being run by the Operating System. That’s why these can be really useful, moreover terminal commands can be used for building programming projects as well. Like you can build a Django Framework based website just from Terminal. Moreover, Terminal can be used for compiling/executing multiple files in a large Software Engineering Project. That’s why every beginner in Software Engineering should be aware about What is Terminal, How does it works and what are commands which can be used in it? Also if in case you already don’t know what is terminal then check out this article – What is Terminal in MAC OS?.
Anyway let’s get into what Directory Operations can be used in Mac Terminal.

Directory Operations Commands

These are commands which can be used for dealing with Directories in Mac FileSystem.

Directory Operation CommandDescription
cdChange the current working directory
pwdPrints out name of current working directory
basenamePrints out file part of a file path
dirnamePrints out name of directory that contains a file
mkdirCreates a new directory
rmdirDeletes an empty directory
rm -rDeletes a non-empty directory and all of content(either files or directories) in it

cd – Directory Operation Command

Change the current working directory.
cd command in general can be written as cd [directory]
In the below picture, cd command changes current directory from being gagan to root directory ~

cd directory operation command

pwd – Directory Operation Command

Prints out name of current working directory.
pwd command in general can be written as only pwd
In the below picture, pwd command prints out /Users/gagan which is current working directory.

pwd directory operation command

basename – Directory Operation Command

Prints out file part of a file path
basename command in general can be written as basename path
In the below picture, basename /Users/gagan/Desktop/Folder1/file1.txt command prints out file1.txt

basename directory operation command

dirname – Directory Operation Command

Prints out name of directory that contains a file.
dirname command in general can be written as dirname path
In the below picture, dirname /Users/gagan/Desktop/Folder1/file1.txt command prints out /Users/gagan/Desktop/Folder1

dirname directory operation command

mkdir – Directory Operation Command

Creates a new directory
mkdir command in general can be written as mkdir [options] directory/directories

Options for mkdir

Options for mkdir CommandDescription
mkdir -pCreates a directory path, even creates more directories for creating the necessary required directory
mkdir -m modeAssign access permissions while creating directory

mkdir -p Command

In the below picture, command mkdir -p computer/science/hub for creating hub directory will create directories computer, science as well.

mkdir -p command

mkdir -m mode Command

In the below picture, command mkdir -m 0755 ComputerScienceHub will create a directory ComputerScienceHub but as mode = 0755 so that’s why directory ComputerScienceHub will be readable by anyone. But writable by only user who created it.

mkdir -m command

rmdir – Directory Directory Operation

Deletes an empty directory.
rmdir command in general can be written as rmdir [options] directory/directories

Options for rmdir Command

Options for rmdir CommandDescription
rmdir -pDeletes all the directories in a path

rmdir -p command

Deletes all the directories in a path. In the below picture, Folder1 have directories computer -> science -> hub and a file file1.txt, command rmdir -p computer/science/hub will remove all of directories in path. So only file1.txt file would be left in Folder1.

rmdir -p command

rm – Directory Operation Command

Deletes a non-empty directory and all of content(either files or directories) in it.
dirname command in general can be written as rm [options] path.

Options for rm command

Options for rm CommandDescription
rm -r or rm -rfRemoves directory
rm -riAsks user yes/no question, before actually removing directory

rm -r Command

Removes directory. In the below picture, Folder1 have computer -> science -> hub directories. Executing rm -r computer command will remove computer -> science -> hub.

rm -r Command

rm -rf Command

Removes directory. In the below picture, Folder1 have computer -> science -> hub directories. Executing rm -r computer command will remove computer -> science -> hub.

rm -rf Command

rm -ri Command

Asks user yes/no question, before actually removing directory. In the below picture, command rm -ri computer will first ask user whether to delete or not.

rm -ri Command

Final Thoughts

I hope that this article helped you to understand Terminal Directory Operation Commands. If in case you have some question or doesn’t understand anything from this article, then let me know in comments below. I’ll try to reply as soon as possible for me.

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts