Browse Courses

File Directory Management Command

Essential Linux file and directory management commands including mkdir, touch cp, mv, rm, and chmod, with practical examples for creating, copying, moving and setting permissions.

This document covers essential Linux commands for managing files and directories. It explains how to create, copy, move, and delete files and directories, set permissions, and manage file ownership. These fundamental commands provide the tools needed for effective file system management and organization in Linux environments.


Creating Files and Directories

Linux provides specific commands for creating both files and directories, allowing for effective organization of the filesystem.

Creating Directories with mkdir

The mkdir (make directory) command creates new directories in the filesystem. Its basic syntax is:

1mkdir [options] directory_name

To create a single directory:

1mkdir projects

Multiple directories can be created simultaneously:

1mkdir docs reports templates

Nested Directory Creation

By default, mkdir cannot create parent directories that don’t exist. The -p (parents) option enables the creation of the entire directory path:

1mkdir -p projects/website/css

This command creates the projects directory, the website subdirectory inside it, and finally the css subdirectory.

Creating Files with touch

The touch command is primarily used to create empty files or update the access and modification times of existing files:

1touch filename.txt

To create multiple files at once:

1touch file1.txt file2.txt file3.txt

The touch command can also be used to update the timestamp of an existing file without modifying its content:

1touch -a existing_file.txt  # Updates access time only
2touch -m existing_file.txt  # Updates modification time only

Copying and Moving Files

Copying Files and Directories with cp

The cp (copy) command creates duplicates of files and directories. Its basic syntax is:

1cp [options] source destination

To copy a file to another location:

1cp report.txt /home/user/backup/

To copy and rename a file:

1cp original.txt copy.txt

Copying Directories

To copy directories and their contents, use the -r (recursive) option:

1cp -r projects /home/user/backup/

Other useful cp options include:

OptionDescription
-iInteractive mode, prompts before overwriting
-vVerbose, displays what is being copied
-pPreserves file attributes (permissions, timestamps)
-uUpdate, copies only when source is newer

Moving and Renaming with mv

The mv (move) command moves files or directories to another location and is also used to rename files. Its basic syntax is:

1mv [options] source destination

To move a file to another directory:

1mv report.txt /home/user/documents/

To rename a file:

1mv oldname.txt newname.txt

Moving directories works the same way:

1mv projects /home/user/backup/

Unlike cp, the mv command doesn’t require a recursive option for directories.

Common mv options include:

OptionDescription
-iInteractive mode, prompts before overwriting
-vVerbose, displays what is being moved
-fForce, overrides confirmations
-uUpdate, moves only when source is newer

Removing Files and Directories

Deleting Files with rm

The rm (remove) command deletes files and directories. Its basic syntax is:

1rm [options] file

To remove a single file:

1rm unwanted.txt

To remove multiple files:

1rm file1.txt file2.txt file3.txt

Removing Directories

To remove directories and their contents, use the -r (recursive) option:

1rm -r projects

Important rm options include:

OptionDescription
-iInteractive mode, prompts before each removal
-fForce removal without prompting
-vVerbose, displays what is being removed

Warning: The rm command permanently deletes files. There is no recycle bin or trash to recover from. Use with caution, especially with the -r and -f options.

Removing Empty Directories with rmdir

The rmdir command removes empty directories only:

1rmdir empty_directory

If the directory contains files or subdirectories, rmdir will fail. This serves as a safety feature to prevent accidental deletion of content.

Changing File Permissions

Understanding File Permissions

Linux file permissions are represented by three sets of read (r), write (w), and execute (x) permissions for:

  1. The file owner
  2. The group owner
  3. All other users

Modifying Permissions with chmod

The chmod (change mode) command modifies file and directory permissions. It can be used with either symbolic or numeric notation:

Symbolic Notation

1chmod [who][operation][permissions] file

Where:

  • who is u (user/owner), g (group), o (others), or a (all)
  • operation is + (add), - (remove), or = (set exactly)
  • permissions is r (read), w (write), or x (execute)

Examples:

1chmod u+x script.sh         # Add execute permission for the owner
2chmod g+rw,o-rw report.txt  # Add read and write for group, remove for others
3chmod a+r document.txt      # Add read permission for everyone

Numeric Notation

Permissions can also be set using octal (base-8) numbers:

  • Read (r) = 4
  • Write (w) = 2
  • Execute (x) = 1

Examples:

1chmod 755 script.sh    # rwxr-xr-x (owner: rwx, group: r-x, others: r-x)
2chmod 644 document.txt # rw-r--r-- (owner: rw-, group: r--, others: r--)
3chmod 700 private.key  # rwx------ (owner: rwx, no permissions for group or others)

Common permission combinations include:

PermissionNumericDescription
rwxrwxrwx777Full access for everyone (rarely used)
rwxr-xr-x755Executable script or directory with restricted write access
rw-r–r–644Regular file with read access for all, write for owner
rwx——700Private directory or script for owner only

Changing File Ownership

The chown command changes the owner and group of files and directories:

1chown user:group filename

To change only the owner:

1chown user filename

To change only the group:

1chown :group filename

For recursive ownership changes on directories:

1chown -R user:group directory

Conclusion

File and directory management commands form the foundation of Linux system administration and everyday usage. The ability to create, copy, move, and remove files and directories efficiently, along with setting appropriate permissions and ownership, is essential for maintaining an organized and secure filesystem. These commands provide powerful tools for managing content in Linux environments, from personal workstations to enterprise servers.


FAQs

The primary purpose of the mkdir command in Linux is to create new directories (folders) within the filesystem. It allows users to organize their files by creating containers to group related files together. The mkdir command can create both single directories and, when used with the -p option, entire directory hierarchies at once.

  1. rm can only remove files while rmdir can remove both files and directories
  2. rm requires superuser privileges while rmdir can be used by any user
  3. rmdir can only remove empty directories while rm can remove both files and directories
  4. rm is a temporary removal while rmdir is permanent
(3) The rmdir command can only remove empty directories and will fail if the directory contains any files or subdirectories. This serves as a safety feature to prevent accidental deletion of content. The rm command can remove both files and directories (when used with the -r option) regardless of whether they contain content.

Without the -r (recursive) option, the cp command would fail if “projects” is a directory. The error message would likely indicate that the source is a directory and suggest using the -r option. The cp command requires the -r option to copy directories and their contents, whereas the mv command does not require this option to move directories.

  1. cp project_dir /backup/
  2. cp -r project_dir /backup/
  3. cp -p project_dir /backup/
  4. cp -rp project_dir /backup/
(4) The cp -rp command is the correct choice. The -r option enables recursive copying of directories and their contents, while the -p option preserves the file attributes including permissions, ownership, and timestamps. This ensures that the backup is an exact copy of the original files with all their metadata intact.

  1. It can use both symbolic and numeric notation to set permissions
  2. The numeric value 777 grants read, write, and execute permissions to all users
  3. The command chmod g+w file.txt adds write permission for the group
  4. The chmod command can only be used by the root user
(4) The statement that the chmod command can only be used by the root user is incorrect. Any user can use chmod to change permissions on files they own. The root user can change permissions on any file, but regular users can modify permissions on their own files without requiring root privileges.

The touch command can only be used to create new files and cannot modify existing files.

False. While the touch command can create new empty files if they don’t exist, its primary purpose is actually to update the access and modification timestamps of existing files without changing their content. This is useful for triggering actions based on file modification times or refreshing file timestamps.

CommandFunction
A. touch1. Removes files and directories
B. mv2. Changes file permissions
C. chmod3. Creates or updates file timestamps
D. rm4. Moves or renames files and directories
A-3, B-4, C-2, D-1. The touch command creates or updates file timestamps, mv moves or renames files and directories, chmod changes file permissions, and rm removes files and directories.

  1. The mv command was created to replace the deprecated cp command
  2. Linux file operations require multiple commands for simplicity and safety
  3. File operations need different approaches for copying versus relocating
  4. Most file operations in Linux require both copying and moving steps
(3) The existence of both mv and cp commands suggests that file operations need different approaches for copying versus relocating. The cp command creates a duplicate while preserving the original, which is necessary when you want to maintain the source file. The mv command relocates the file without creating a duplicate, which is more efficient when you want to change the file’s location or name without keeping the original.

A system administrator would use the command chown -R www-data:www-data /var/www/ to recursively change the ownership of all files and directories within the web directory. The -R option makes the ownership change recursive, affecting all files and subdirectories. This is a common operation when setting up web servers to ensure the web server process has appropriate access to all website files.

When chmod 644 is executed on a file that previously had permissions set to 755, the file loses its execute permissions for all users (owner, group, and others) while maintaining read/write permissions for the owner and read-only permissions for group and others. Specifically, the permissions change from rwxr-xr-x (755) to rw-r–r– (644). This is a common change when converting an executable script to a regular data file.