Tuesday, October 19, 2010

Creating & Removing Directories & Files

Unix / Linux - Getting Started


In this post I will show you how to

  • create a directory
  • change a directory or directory path
  • show the current / present working directory or path and switch between the current and the previous directories or paths
  • create a plain text  file
  • delete  a directory, a file, and a directory structure completely

Command: mkdir

Usage:

$ whatis mkdir
mkdir                (1)  - make directories
mkdir                (2)  - create a directory
$

Examples:

$ mkdir scripts
The above command creates an empty directory and name it scripts.


We can also define a sub-directory to be created beneath the top directory or in a specified directory path:

$ mkdir first/second/third
mkdir: cannot create directory `first/second/third': No such file or directory





As the specified directories first and second do not already exist or the path is invalid so we need to use the option "-p" to create the parent directories as well:


$ mkdir -p first/second/third



Let's check the contents of the parent directory first by recursively listing the directory contents:


$ ls -R first/
first/:
second

first/second:
third

first/second/third:
$


Command: cd

Usage:

$ whatis cd
cd [builtins]        (1)  - bash built-in commands, see bash(1)
$

Examples:


$ cd first
The above changes directory to first if it is found in the current directory / location or path. We can specify a path also:

$ cd /tmp/samples


Command: pwd

Usage:

$ whatis pwd
pwd                  (1)  - print name of current/working directory
pwd [builtins]       (1)  - bash built-in commands, see bash(1)
$


Examples:

$ pwd
/tmp/samples
$
The above command shows the complete path to our current working directory (CWD). We can check the previous directory name by using a special environmental or system variable "OLDPWD" as shown below:

 $ echo $OLDPWD
/root

Conversely we can use a variable that gives us the same information as produced by the command pwd:

$ echo $PWD
/tmp/samples
Let's change our current directory and go back in the previous one:

$ cd -
/root
$
Please note the hyphen "-" after "cd". We could have as well used:

$ cd /root

To directly go to the home directory we can use the option "~" or simply type "cd" without any option:

$ pwd
/tmp/samples
$ cd ~
$ pwd
/root

$ cd -
/tmp/samples
$ cd
$ pwd
/root

Command: rmdir

Usage:

$ whatis rmdir
rmdir                (1)  - remove empty directories
rmdir                (2)  - delete a directory
$

Examples:

 $ pwd
/root/first/second
$ ls -R
.:
third

./third:
$ rmdir third
$ ls -R
.:

The command "rmdir" deletes or removes only an empty directory.


$ rmdir first/
rmdir: `first/': Directory not empty
$
To recursively delete a directory structure we can pass the "-r" option to the command "rm" as shown below:

$ rm -r first/

Command: rm

Usage:

$ whatis rm
rm                   (1)  - remove files or directories
$

Examples:

$ rm hi
We can pass "-i" to interactively remove a file or directory:

$ rm -i hello
rm: remove regular empty file `hello'? y
We can pass "-f" to forcefully remove a file or directory, and "-r" to recursively delete a directory and all its sub-directories and files in one go.

Command: touch


Usage:

$ whatis touch
touch                (1)  - change file timestamps
$

Examples:

$ touch HelloWorld

The above command creates a plaint text file and name it "HelloWorld". The file contains nothing, i.e. it's empty.

$ touch HelloWorld
$ ls -l HelloWorld
-rw-r--r--  1 root root 0 Oct 20 21:38 HelloWorld

Had the file already been created, its time-stamp would have been updated:
$ touch HelloWorld
$ ls -l HelloWorld
-rw-r--r--  1 root root 0 Oct 20 21:39 HelloWorld
$ touch HelloWorld
$ ls -l HelloWorld
-rw-r--r--  1 root root 0 Oct 20 21:40 HelloWorld
For creating a text file that can contain some text or data we can use "cat" whose primary function is to concatenate and display contents of text files.


Command: cat


Usage:

$ whatis cat
cat                  (1)  - concatenate files and print on the standard output


Examples:

$ cat > HelloWorld
Hello, world!
This is a plain text file.

<Hit Ctrl + c to Cancel Entering Text or Ctrl + d to Terminate the Operation>
Let's display the contents of the file we created above:

$ cat HelloWorld
Hello, world!
This is a plain text file.
Note:

  • We do not use the greater then  ">" symbol after cat when we want to display the contents of the file. The angle bracket ">" acts as an Input Redirection Operator. The input is coming from the commonly used standard input device, i.e. the computer keyboard.
  • On some systems hitting Ctrl + d may terminate the log-in session as it is also used to Exit / Log off from an opened session. 
  • cat > fileName creates a new file if it doesn't already exist. If it exists then its contents are overwritten.

Let's create two text files and enter some data, and then concatenate them and display their contents:

$ cat > OS
Unix
Linux
Mac
<Hit Ctrl + c>

$ cat > Desktop
KDE
GNOME
CDE
<Hit Ctrl + c> 

$ cat OS Desktop
Unix
Linux
Mac
KDE
GNOME
CDE
For creating, editing, and viewing of text files and searching for a specific piece of information or text in them we use a Text Editor such as Vi or Vim and Emacs. I will cover them in some other post.

No comments:

Post a Comment