Apr 24 2007
LCoTD: Directory Operations
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Today I’ll be covering directory operations, which are- as you probably guessed- commands that manipulate directories. Specifically, cd, pwd, basename, dirname, mkdir, rmdir, and rm -r.|
|
First, a quick overview of what each does:
cd: Change your current directory
pwd: Print the name of your current directory, i.e., “where you are now” in the filesystem
basename: Print the final part of a file path
dirname: Remove the final part of a file path
mkdir: Create a directory
rmdir: Delete an empty directory
rm -r: Delete a nonempty directory and its contents
cd [directory]
The cd (change directory) command sets your current working directory. With no directory supplied, cd defaults to your home directory. It comes with bash.
pwd
The pwd command prints the absolute path of your current working directory:
$ pwd
/users/george/mydirpwd is part of bash.
basename path
The basename command prints the final component in a file path; so for the example above:
$ basename /users/george/mydir
mydirbasename is part of coreutils and can be found in /bin.
dirname path
The dirname command removes the final component from a file path:
$ dirname /users/george/mydir
/users/georgedirname simply manipulates a string that is a directory name. It does not change your current working directory. It is part of coreutils and can be found in /usr/bin.
mkdir [options] directories
mkdir creates one or more directories:
$ mkdir d1 d2 d3Useful Options
-p If you supply a directory path (not just a simple directory name), create any necessary parent directories automatically: mkdir -p /one/two/three will create /one and /one/two if they don’t already exist, then /one/two/three.
-m mode: Create the directory with the given permissions: mkdir 0755 mydir By default, your shell’s umask controls the permissions.
mkdir is part of coreutils and can be found in /bin.
rmdir [options] directories
The rmdir (remove directory) command deletes one or more empty directories you name. To delete a nonempty directory and its contents, use (carefully) rm -r directory. Use rm -ri directory to delete interactively (it will ask for confirmation), or rm -rf directory to annihilate without any error messages or confirmation.
Useful Options
-P: If you supply a directory path (not just a simple directory name), delete not only the given directory, but the specified parent directories automatically, all of which must be otherwise empty. So rmdir -p /one/two/three will delete not only /one/two/three, but also /one/two and /one if they exist.
And that’s it for today folks. Tomorrow, I’ll cover File viewing commands, and there sure are a lot of em! Until then, feel free to ask any questions or leave any comments below.
Sphere: Related ContentIf you found this article useful and use StumbleUpon, please give it a
thumbs up so more people can read it! Thank you!
Please take the time to check out this thread and leave a comment letting me know what you would like to see from this site. It's still relatively young and trying to find its way- you can make design pitstop the resource you always wanted!
