Directory and File Management Commands in Linux

Managing files and directories is an essential part of using Linux. This guide simplifies the key commands so that even a beginner or non-native English speaker can understand and use them easily.


Index

  1. Creating Files with the touch Command
  2. Reading File Content
  3. Writing to a File
  4. Deleting Files and Directories
  5. Creating Directories
  6. Copying Files and Directories
  7. Moving and Renaming Files
  8. Using Wildcards

Creating Files with the touch Command

The touch command is used to create empty files or update the timestamps of existing files.

To create a file
touch file_name

Example:

touch myfile.txt

This creates an empty file named myfile.txt.

To create multiple files at once
touch file1.txt file2.txt file3.txt

This creates file1.txt, file2.txt, and file3.txt in one command.

Update Timestamps

If the file exists, the touch command updates its last access and modification time to the current time.

Switches for touch
  • -a: Update only the access time.
    Example: touch -a file_name
  • -c: Don’t create the file if it doesn’t exist.
    Example: touch -c file_name
  • -m: Update only the modification time.
    Example: touch -m file_name
  • -t: Set a specific timestamp.
    Example: touch -t 202312012359 file_name
    This sets the file’s time to December 1, 2023, 23:59.

Reading File Content

Here are commands to view file content:

cat

Displays the file’s content in the terminal.
Example: cat myfile.txt

more

Shows the file’s content one screen at a time. Use arrow keys to navigate.
Example:

less

Similar to more, but you can also scroll backward.
Example: less myfile.txt

Displays the first few lines of a file (default is 10).
Example: head myfile.txt

tail

Displays the last few lines of a file (default is 10). Great for logs.
Example: tail myfile.txt


Writing to a File

echo
Writes text to a file.
Example:

This overwrites the file with “Hello, World!”

To append text:

echo "More text" >> myfile.txt
printf
Similar to echo, but with more formatting options.
Example:
printf "Line 1\nLine 2\n" > myfile.txt
nano
A simple text editor to write or edit files.

Example: nano myfile.txt
Use these shortcuts in nano:
  • Ctrl+O: Save changes.
  • Ctrl+X: Exit.
  • Ctrl+K: Cut a line.
  • Ctrl+U: Paste the cut line.

Deleting Files and Directories

The rm and rmdir commands are used to delete files and directories.

  • To delete a file:bashCopy coderm file_name Example: rm myfile.txt
  • To delete a directory:arduinoCopy codermdir directory_name
  • To delete multiple files:bashCopy coderm file1.txt file2.txt
Switches for rm
  • -i: Prompts for confirmation before deleting each file.
    Example: rm -i file_name
  • -r: Deletes directories and their contents recursively.
    Example: rm -r myfolder
  • -v: Shows a message for each file deleted.
    Example: rm -v file_name

Creating Directories

Use the mkdir command to create directories.

  • To create a single directory:arduinoCopy codemkdir directory_name
  • To create multiple directories:arduinoCopy codemkdir dir1 dir2 dir3
Switches for mkdir
  • -v: Shows a message for each directory created.
    Example: mkdir -v myfolder
  • -p: Creates parent directories if they don’t exist.
    Example: mkdir -p parent/child
  • -m: Sets permissions while creating a directory.
    Example: mkdir -m 755 myfolder

Copying Files and Directories

Use the cp command to copy files or directories.

  • To copy a file:bashCopy codecp source_file destination
  • To copy multiple files to a directory:bashCopy codecp file1.txt file2.txt /path/to/destination/
Switches for cp
  • -r: Copies directories and their contents recursively.
    Example: cp -r source_dir destination_dir
  • -i: Prompts before overwriting files.
    Example: cp -i file_name destination
  • -p: Preserves file attributes (permissions, timestamps).
    Example: cp -p file_name destination

Moving and Renaming Files

Use the mv command to move or rename files and directories.

  • To move a file:bashCopy codemv source_file destination
  • To rename a file:bashCopy codemv old_name new_name
  • To move multiple files:bashCopy codemv file1.txt file2.txt /path/to/destination/
Switches for mv
  • -i: Prompts before overwriting files.
    Example: mv -i file_name destination
  • -b: Creates a backup before overwriting.
    Example: mv -b file_name destination

Using Wildcards

Wildcards make file operations faster by matching patterns:

1. *

Matches any number of characters.
Example:

ls *.txt

Lists all .txt files.

2. ?

Matches a single character.
Example:

ls file?.txt

Lists files like file1.txt, file2.txt.

3. {}

Matches specific patterns.
Example:

touch file{1,2,3}.txt

Creates file1.txt, file2.txt, file3.txt.

4. []

Matches a range or set of characters.
Example:

ls file[1-3].txt

Lists files like file1.txt, file2.txt, file3.txt.


This guide provides the essential commands to help you manage files and directories in Linux. Practice them to master file system management!