Linux - Working with Directory
Linux Directory Structure have covered how directories provide a tree like structure for the file system.
Directories are just containers for files and directories. Directories can be accessed by their name, a full path, or shortcut.
When working on Linux systems, a period (
.) is often referred to as dot. This is no different than hotmail do com instead of hotmail period come. So, in Linux a single dot represents this or current directory. Two dots represent the parent directory. You can think of the parent directory as being the directory above your current directory. The
cd command provides a shortcut too. If you type
cd - , then you will be placed into your previous directory. In addition, if you type
cd alone, then you will be placed into your home directory e.g. /home/robin .
||This or current directory|
||The parent directory|
||Change to the previous directory|
||Change to home directory|
The directory separator in Linux is the forward slash
/. So you can think of directories as ending in
/, and often this is understood. For example, if you run
cd /home/robin , that is the same thing as running
In this section, we have talked about the environment variable of
$PATH determines your command search path. You can specify a command with a full path. You can execute commands that are not in your path. And if you want to execute a command in the current directory you are in, you'd type
$PATH environment variable determines where your shell will look for commands.
$ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
which tells us that if we run
cat , that it would run bin/cat
$ which cat /bin/cat $ cat sample.txt hello world! This is content.
We can also specify the full path to that and we get the same result.
$ /bin/cat sample.txt hello world! This is content.
However, if we want to run a different cat, then we could do something like this:
$ /home/robin/new_cat/cat This is a new cat. Hi ~
Or, you can cd into this directory and run the command like this:
$ cd /home/robin/new_cat $ ./cat This is a new cat. Hi ~
Creating and Removing Directories
If you want to create a directory you can use the
Note: parameter in the breakets means it is optional.
||Create a directory|
||Only remove directories that are empty (no file content)|
||Recursively delete everything (all files and directories) in that directory.|
-p is optional here. The
-p stands for parents so if we want to create a directory structure that is more than just one directory deep we need to specify
-p, so that it will create the parent directories.
$ mkdir -p one/two/three
Important: An important thing to note when you are working at the command line in Linux, there is no undo. When you delete something, it is gone. When you recursively delete something, all those files and directories are gone. You can't get them back. So, be careful when you are using
rm -rf. One suggestion is to run
ls to check the directories and files before
mkdir won't be able to create the threedir/ directory because its parent directory twodir/ does not yet exist.
$ mkdir onedir/twodir/threedir mkdir: cannot create directory 'onedir/twodir/threedir': No such file or directory
In order to create a depth 3 directory, you need to specify
$ mkdir -p onedir/twodir/threedir
rmdir command only remove empty directories.
$ rmdir onedir rmdir: failed to remove 'onedir': Directory not empty
If you want to recursively remove them, you can type:
$ rm -rf onedir
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync