DEV Community

Cover image for Windows CMD – File & Directory Management
Sajjad Rahman
Sajjad Rahman

Posted on

Windows CMD – File & Directory Management

Windows CMD – File & Directory Management

1️⃣ Directory Basics (dir)

Show files and folders

dir
Enter fullscreen mode Exit fullscreen mode

Important entries

  • . → current directory
  • .. → parent directory
  • <DIR> → folder
  • File size shown in bytes

2️⃣ Creating Files with echo

Create a file with text

echo "sajjad" > samina.txt
Enter fullscreen mode Exit fullscreen mode

Read file content

type samina.txt
Enter fullscreen mode Exit fullscreen mode

Output

"sajjad"
Enter fullscreen mode Exit fullscreen mode

⚠ Why quotes appear?

Because quotes are literal characters inside echo.

✔ To avoid quotes:

echo sajjad > samina.txt
Enter fullscreen mode Exit fullscreen mode

3️⃣ Creating an Empty (Blank) File – IMPORTANT CONCEPT

Command used

echo 2>err.txt
Enter fullscreen mode Exit fullscreen mode

❓ Why is 2 used here?

This is error stream redirection.

Stream Meaning
0 Standard Input
1 Standard Output
2 Standard Error

What actually happens?

  • echo produces no error
  • Error stream (2) is empty
  • Empty stream redirected → file created with 0 bytes

✔ This is a CMD trick to create an empty file.

Better & common methods

type nul > file.txt
Enter fullscreen mode Exit fullscreen mode

or

echo. > file.txt
Enter fullscreen mode Exit fullscreen mode

CMD


4️⃣ Deleting Files (del)

del na.txt
Enter fullscreen mode Exit fullscreen mode

Check after deletion

type na.txt
Enter fullscreen mode Exit fullscreen mode

Output

The system cannot find the file specified.
Enter fullscreen mode Exit fullscreen mode

✔ File deleted successfully


5️⃣ Renaming Files (rename)

rename err.txt error.txt
Enter fullscreen mode Exit fullscreen mode

✔ File name changes only, content remains same


6️⃣ Creating & Moving Directories

Create directory

mkdir samsaj
Enter fullscreen mode Exit fullscreen mode

Move file into directory

move samina.txt samsaj
Enter fullscreen mode Exit fullscreen mode

Change directory

cd samsaj
Enter fullscreen mode Exit fullscreen mode

Go back

cd ..
Enter fullscreen mode Exit fullscreen mode

7️⃣ Path Symbols Explained

Symbol Meaning
. Current directory
.. Parent directory
\ Windows path separator

Example:

move samina.txt .\samsaj
Enter fullscreen mode Exit fullscreen mode

8️⃣ Copying Files (copy)

copy samina.txt sajjad.txt
Enter fullscreen mode Exit fullscreen mode

✔ Creates duplicate file with same content


9️⃣ Removing Directories (rmdir) – VERY IMPORTANT

Delete empty directory

rmdir sam
Enter fullscreen mode Exit fullscreen mode

❌ If directory is NOT empty

The directory is not empty.
Enter fullscreen mode Exit fullscreen mode

✔ Correct way to delete directory with files

rmdir /S sam
Enter fullscreen mode Exit fullscreen mode
  • /S → deletes all files + subdirectories
  • Confirmation required

❌ Wrong commands (do not work):

rmdir -S sam
rmdir \S sam
Enter fullscreen mode Exit fullscreen mode

🔟 Comparing Files (fc)

fc sajjad.txt samina.txt
Enter fullscreen mode Exit fullscreen mode

Output meaning

  • Shows line-by-line differences
  • Useful for file integrity checking

1️⃣1️⃣ Symbolic Links (Soft Links) – mklink

⚠ CMD must be Run as Administrator

Create symbolic link

mklink linkfile file3.txt
Enter fullscreen mode Exit fullscreen mode

Verify

dir
Enter fullscreen mode Exit fullscreen mode

Output

<SYMLINK> linkfile [file3.txt]
Enter fullscreen mode Exit fullscreen mode

✔ Both files point to same data
✔ Reading/writing link affects original file


1️⃣2️⃣ Searching Files with dir

Search specific file recursively

dir /s torjan.txt
Enter fullscreen mode Exit fullscreen mode

Search all .txt files

dir /s *.txt
Enter fullscreen mode Exit fullscreen mode

1️⃣3️⃣ Finding Files using forfiles

Command

forfiles /P I:\ /S /M torjan.txt /C "cmd /c echo @PATH"
Enter fullscreen mode Exit fullscreen mode

Flags explained

Flag Meaning
/P Starting directory
/S Recursive search
/M Filename or pattern
/C Command to run
@PATH Full file path

❗ Access denied errors?

Normal behavior — system protected folders ($RECYCLE.BIN, System Volume Information)

✔ File still found successfully.


1️⃣4️⃣ Finding Text Inside Files – find

❌ Wrong usage

find "pass" I:
Enter fullscreen mode Exit fullscreen mode

✔ Correct usage

find "pass" filename.txt
Enter fullscreen mode Exit fullscreen mode

Example:

find "pass" I:\ethical-test\samsaj\s299\s299.txt
Enter fullscreen mode Exit fullscreen mode

Output

pass : s299
pass : s265
Enter fullscreen mode Exit fullscreen mode

find is case-sensitive and basic


1️⃣5️⃣ Advanced Text Search – findstr (Recommended)

Search word

findstr "pass" s299.txt
Enter fullscreen mode Exit fullscreen mode

Output

pass : s299
pass : s265
sam pass samina password
sajjad password pass
Enter fullscreen mode Exit fullscreen mode

Why findstr is better?

✔ Case-insensitive (by default)
✔ Supports patterns
✔ Searches multiple lines cleanly


✅ Key Takeaways (Revision Summary)

  • echo > file → creates file with content
  • echo 2> file → creates empty file via error redirection
  • type → read file
  • del → delete file
  • rename → rename file
  • mkdir / rmdir → directory management
  • /S is mandatory to delete non-empty folders
  • fc → compare files
  • mklink → symbolic link (admin required)
  • dir /s → recursive file search
  • forfiles → powerful file finder + executor
  • findstr → best for searching text inside files

Top comments (0)