Creating and Removing Files and Directories Under Linux

Here is an absolute beginner post on creating and removing Files and Directories under Linux.

Creating Files

The touch command creates a new empty file.

$ touch filename

You can create multiple files with the same command. If the file name or directory name already exists, the touch command updates the modification time and access time to the current date and time. You can use absolute or relative path names on the command line when creating new files.

To create an empty file named file1 in the /tmp directory, enter the following commands:

$ cd /tmp
$ touch space
$ ls -l file1
$ ls -l file
-rw-r--r--  1 geek  wheel  0 Dec 13 22:05 file

To create multiple empty files use the touch commands with the file names in one line as shown below.

$ touch file1 file2 file3
$ ls -lrt file*
-rw-r--r--  1 geek  wheel  0 Dec 13 22:19 file2
-rw-r--r--  1 geek  wheel  0 Dec 13 22:19 file1
-rw-r--r--  1 geek  wheel  0 Dec 13 22:19 file3

Creating Directories

The mkdir command creates new directories.

 $ mkdir directory_name

and/or

$ mkdir -p directory_names

Include the –p option if the directory name includes a path name. The command used with the -p option creates all of the non-existing parent directories that do not yet exist in the path to the new directory. You can use absolute or relative path names on the command line when creating new directories.

For example, create a new directory, named dir1, within the /tmp directory.

$ cd /tmp
$ mkdir dir1

You can use the command ‘ls -ld’ to view the created directory.

$ ls -ld dir1
drwxr-xr-x  2 geek  wheel  64 Dec 13 22:26 dir1

To create a new directory named dir_in located inside a directory named dir_out, use the mkdir command with the -p option. The dir_out directory does not yet exist.

$ mkdir -p dir_out/dir_in

To create the dir1, dir2, and dir3 directories, enter the mkdir command with all the directory names in one line as shown below.

$ mkdir dir1 dir2 dir3
$ ls -ld dir*
drwxr-xr-x  2 sandy  wheel  64 Dec 13 22:26 dir1
drwxr-xr-x  2 sandy  wheel  64 Dec 13 22:28 dir2
drwxr-xr-x  2 sandy  wheel  64 Dec 13 22:28 dir3

Removing Files

You can permanently remove files from the directory hierarchy with the rm command.

$ rm -option filename

The rm command is a destructive command if not used with the correct option. The table describes the options that you can use with the rm command when removing files and directories.

Option Description
-r Includes the contents of a directory and the contents of all subdirectories when you remove a directory
-i Prevents the accidental removal of existing files or directories

The –r option allows you to remove directories that contain files and subdirectories. The -i option prompts you for confirmation before removing any file.
– A yes response completes the removal of the file.
– A no response aborts the removal of the file.

For example, remove the file named file1 from the /tmp directory.

$ cd /tmp
$ rm file1

Lets see an example of using the -i option to delete the files.

$ rm -i file2
remove file2? y

Removing Directories

You can use the rm command with the -r option to remove directories that contain files and subdirectories.

$ rm -options directories

For example, remove the dir1 directory and its content by using the rm –r command.

$ cd /tmp
$ rm -r dir1
$ ls -ld dir1
ls: dir1: No such file or directory

If you do not use the -r option with the rm command while removing directories, the following error message appears:

rm: directoryname: is a directory.

To interactively remove a directory and its contents, use the –i option along with the rm –r command. For example,

$ rm -ir dir2
examine files in directory dir2? y
remove dir2/file2? y
remove dir2/file1? y
remove dir2? y

The rmdir command removes empty directories.

$ rmdir directories

For example to remove the empty directory dir3, use the command below.

$ cd /tmp
$ rmdir dir3

To remove a directory in which you are currently working in, you must first change to its parent directory.

Related Post