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.
Linux provides specific commands for creating both files and directories, allowing for effective organization of the filesystem.
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
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.
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
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
To copy directories and their contents, use the -r (recursive) option:
1cp -r projects /home/user/backup/
Other useful cp options include:
| Option | Description |
|---|---|
-i | Interactive mode, prompts before overwriting |
-v | Verbose, displays what is being copied |
-p | Preserves file attributes (permissions, timestamps) |
-u | Update, copies only when source is newer |
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:
| Option | Description |
|---|---|
-i | Interactive mode, prompts before overwriting |
-v | Verbose, displays what is being moved |
-f | Force, overrides confirmations |
-u | Update, moves only when source is newer |
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
To remove directories and their contents, use the -r (recursive) option:
1rm -r projects
Important rm options include:
| Option | Description |
|---|---|
-i | Interactive mode, prompts before each removal |
-f | Force removal without prompting |
-v | Verbose, displays what is being removed |
Warning: The
rmcommand permanently deletes files. There is no recycle bin or trash to recover from. Use with caution, especially with the-rand-foptions.
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.
Linux file permissions are represented by three sets of read (r), write (w), and execute (x) permissions for:
The chmod (change mode) command modifies file and directory permissions. It can be used with either symbolic or numeric 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
Permissions can also be set using octal (base-8) numbers:
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:
| Permission | Numeric | Description |
|---|---|---|
| rwxrwxrwx | 777 | Full access for everyone (rarely used) |
| rwxr-xr-x | 755 | Executable script or directory with restricted write access |
| rw-r–r– | 644 | Regular file with read access for all, write for owner |
| rwx—— | 700 | Private directory or script for owner only |
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
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.
(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.
(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.
(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.
| Command | Function |
|---|---|
| A. touch | 1. Removes files and directories |
| B. mv | 2. Changes file permissions |
| C. chmod | 3. Creates or updates file timestamps |
| D. rm | 4. 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.
(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.
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.