2.4 Creating, Moving and Deleting Files
2.4 Creating, Moving and Deleting Files
Weight: 2
Description: Create, move and delete files and directories under the home directory.
Key Knowledge Areas:
Files and directories
Case sensitivity
Simple globbing
The following is a partial list of the used files, terms and utilities:
mv, cp, rm, touch
mkdir, rmdir
File Management in Linux
File management in Linux involves handling files and directories through various operations such as creation, modification, organization, and access control within the filesystem.
Linux treats everything as a file, including devices and system configurations.
Ensures efficient data organization and accessibility.
Involves operations like create, copy, move, rename, and delete.
Uses file permissions and ownership for secure access control.
Common commands include cp, mv, rm, ls, cat, and chmod.
Linux categorizes files into three main types, each serving a specific purpose in the system:

1. General Files
These are the most common file types that store user data such as text files, images, and binaries.
Represent regular data like documents, programs, or scripts.
Can be created using the
touchcommand.Form the majority of files in Linux/UNIX systems.
May contain human-readable text (ASCII), executable binaries, or program data.
2. Directories:
These act as containers that organize files and other directories hierarchically.
Similar to folders in Windows.
Store lists of file names and their related metadata.
New directories can be created using the
mkdircommand.
Important directories include:
/:Root directory (base of the system)/home/:User home directories/bin/:Essential user binaries/boot/:Static boot files
3. Device Files:
These files represent hardware devices and handle input/output (I/O) operations.
Used to interact with physical devices like printers, disks, or terminals.
Found mostly in the
/dev/directory.Allow the operating system to treat hardware as if it were a regular file.
Listing Files
ls
To list files and directories in Linux, the ls command is used. It provides a quick view of the contents of a directory, including files, subdirectories, and optional details like permissions, ownership, and timestamps.
Lists all files and directories in the current directory.
Each type of file is displayed with a different color for easy identification.
Directories are typically shown in dark blue.
Helps visually distinguish between files, directories, and other file types.
Makes navigating and understanding directory contents faster and more intuitive.
Running ls -l returns a detailed listing of files and directories in the current directory.
Displays important information such as:
File permissions (who can read, write, or execute)
Owner and group of each file
File size and last modification date
Helps determine which users or groups can access or manage each file, providing insights into system security and file management.
Wildcards and Globbing
File globbing is a feature provided by the UNIX/Linux shell to represent multiple filenames by using special characters called wildcards with a single file name. A wildcard is essentially a symbol which may be used to substitute for one or more characters. Therefore, we can use wildcards for generating the appropriate combination of file names as per our requirement.
Question mark – (?) is used to match any single character. example: ls -l ????.txt
Asterisk – (*) is used to match zero or more characters. example: ls -l *.pl
Square Brackets [] is used to match the character from the range.. example: ls -l [p-s]*
Creating Files
touch
The
touchcommand is used to create a new file in Linux.If the specified file does not exist,
touchcreates a new blank file.If the file already exists, its contents remain unaffected, and only the file’s timestamp may be updated.
This command is a quick and simple way to create empty files for various purposes.
Moving a File
mv
The
mvcommand is used to move a file from one location to another in Linux.It removes the file from the source directory and creates it in the destination directory with the same name and content.
This command can also be used to rename files by specifying a different name at the destination.
Renaming a File
The
mvcommand can also be used to rename a file in Linux.It changes the file name from
filenametonew_filenamewhile retaining the file’s content.Essentially, the original file is replaced with a file of the new name without altering the data.
Deleting a File
rm
The
rmcommand is used to delete a file in Linux.It permanently removes the specified file from the directory.
Use this command carefully, as deleted files cannot be easily recovered.
You can not restore files. use with caution.
Directory management in Linux?
As we said, directories in Linux are special types of files that store references (paths) to other files and directories, organizing the filesystem in a hierarchical structure.
Directories act as containers for files and subdirectories.
The root directory (
/) is the top-level directory in Linux.Special directories like
.(current directory) and..(parent directory) are used for navigation.Each directory can contain multiple files and subdirectories.
Creating Directories
mkdir
To create a directory in Linux you have to use mkdir command which stands for make directory.
example:
Listing Directories
To list directories, you can use the ls command. Adding the -l flag provides detailed information about each item, including whether it’s a file or directory. In the detailed output, entries that start with a ‘d’ (e.g., drwxrwxr-x) indicate directories.
You can also list a directory's content by using ls command
Removing Directories
rmdir
To remove a directory you can use rmdir but for that the directory must be empty
To remove a directory which has contents in it use 'rm' with recursive flags '-r' or '-rf'
This will delete the directory with all its content inside it.
Changing Directories
To change directories you have cd with various attributes.
If directory is somewhere else then give the path also with directory name.
To move back to last directory you were in
To move to the parent directory of your current directory use
To move back to the default directory use (home)
To check in current directory use (while it has no much use case other than scripting but it is still useful to know it)
Copying Directories
To copy a directory, use the
cpcommand with the-r(recursive) flag to ensure all contents inside the directory are copied as well.You can copy a directory from any location by providing the full path of both the source and destination.
This command works even if you’re not in the parent directory of the directory being copied.
Make sure you have the required permissions to access and copy the directory.
Moving or Renaming Directories
To move or rename a directory, for both 'mv' command is used.
To move a directory provide the path of source directory and destination path. You can move a directory from and to anywhere, you just need to provide correct path with directory name which you want to move and the destination where you want to move, required you have permission to do so.
To rename a directory you need to present in the parent directory. just provide the old name and followed by new name.
Checking Directories Size
du
To check a directories/files size use du command and to get in human readable format (kb,mb,gb) use -h with it.
Case Sensitivity in Linux
Case sensitivity in Linux refers to files and directories recognizing differences between lowercase and uppercase characters in filenames. For instance, "File.txt" and "file.txt" would both be treated as two distinct files. This concept is integral to Unix-like operating systems, including Linux.
Linux offers numerous benefits from choosing this design option in file naming: it reduces ambiguity in file names, prevents accidental overwrites, and can increase performance because no case normalization checks need to be performed for every file access. From a security perspective, these features also mitigate attacks that involve character case manipulation to bypass security controls.
globbing
In Linux, a glob is a pattern-matching mechanism used for filename expansion in the shell. The term "glob" represents the concept of matching patterns globally or expansively across multiple filenames or paths. It allows users to specify wildcard patterns to match files or directories based on certain criteria. The globbing feature in Linux provides flexibility and efficiency when working with multiple files.
By using wildcards like an asterisk (*) or question mark (?), users can match filenames or paths that meet specific patterns, simplifying tasks such as listing, copying, moving, or deleting files. Globbing is an integral part of the Linux shell, making it easier to perform operations on groups of files by expanding the specified pattern into a list of matching filenames.
Wildcard Matching
Glob Linux supports several wildcard characters for pattern matching.
Asterisk (*)
The asterisk (*) is a powerful wildcard character in glob Linux that matches any number of characters (including none) within a filename or path segment. It is used for pattern matching.
For example, *.txt matches all files with a .txt extension in the current directory.
Question Mark (?)
The question mark (?) in glob Linux is a wildcard character matching any single character within a filename or path segment. It is useful for matching files with a specific pattern.
For example, file?.txt matches files like file1.txt or fileA.txt, but not file10.txt.
There are more and more way of using globbing byt that 's enough for know.
.
.
.
sources:
https://www.geeksforgeeks.org/linux-unix/file-management-in-linux/ https://www.geeksforgeeks.org/linux-unix/how-to-manage-directories-in-linux/ https://linuxsecurity.com/features/linux-filesystem-case-sensitivity-debate#:~:text=Case%20sensitivity%20in%20Linux%20refers,treated%20as%20two%20distinct%20files. https://www.scaler.com/topics/glob-linux/
Last updated
