Skip to content

Exploring the File System

The command line interface (CLI) is a powerful tool that allows you to interact with your computer's file system. It provides a text-based interface for navigating directories, listing files, and performing various file operations. This document will guide you through the essential commands for exploring the file system using the CLI.

Basic Navigation

  • pwd: Print the current working directory. This command displays the full path of the directory you are currently in.
  • ls: List the contents of a directory. By default, it lists the files and subdirectories in the current directory. You can use options like -l for a detailed list (includes file size), -a to show hidden files, or -t to sort by modification time.
  • cd: Change the current directory. Use cd followed by the directory name or path to move to that directory. For example, cd Documents or cd /home/user/Documents. Use cd .. to go up one level in the directory hierarchy.

File Operations

  • touch: Create an empty file. For example, touch newfile.txt creates an empty file named "newfile.txt".
  • cat: Display the contents of a file. For example, cat myfile.txt displays the content of "myfile.txt" in the terminal.
  • cp: Copy files or directories. Use cp source destination to copy. For example, cp myfile.txt myfile_copy.txt or cp -r mydirectory newdirectory (to copy a directory recursively).
  • mv: Move or rename files or directories. The syntax is similar to cp. Use mv source destination. For example, mv myfile.txt myfile_renamed.txt or mv mydirectory newlocation/.
  • rm: Remove files or directories. Use rm myfile.txt to delete a file. To delete a directory and its contents, use rm -r mydirectory (be cautious with this command!).
  • mkdir: Create a new directory. For example, mkdir new_directory creates a directory named "new_directory".

Checking File and Directory Sizes

  • ls -l: As mentioned earlier, using the -l option with ls provides a detailed list of files and directories, including their sizes in bytes.
    • Example: ls -l myfile.txt will show details about "myfile.txt", including its size.
  • du: This command stands for "disk usage." It displays the amount of disk space used by files and directories.
    • du -h: Shows sizes in human-readable format (KB, MB, GB). For example, du -h mydirectory displays the size of "mydirectory" and its subdirectories in a user-friendly format.
    • du -a: Displays the disk usage of individual files, not just directories. For example, du -a mydirectory shows the size of all files within "mydirectory".
    • du -s: Displays only the total size of a directory. For example, du -s mydirectory shows only the total size of "mydirectory".
  • stat: This command displays various file information, including size, permissions, ownership, and more.
    • Example: stat myfile.txt will show detailed information about "myfile.txt", including its size in bytes.

Advanced Techniques

  • Wildcards: Use characters like * (matches any characters) or ? (matches a single character) to select multiple files. For example, ls *.txt lists all files ending with ".txt".
  • Pipes and Redirection: Combine commands using pipes (|) to chain their output. Redirect output using > (overwrite) or >> (append). For example, ls -l | grep "myfile" lists detailed information only for files containing "myfile" in their name.
  • find: Locate files based on various criteria like name, size, type, or modification time. For example, find . -name "*.txt" finds all ".txt" files in the current directory and its subdirectories. You can also use find with -size to locate files based on their size.

Tips and Best Practices

  • Use tab completion to quickly type file and directory names.
  • Be careful when using commands like rm -r, as they can permanently delete files.
  • Consider using a command-line shell like Zsh or Fish for enhanced features and customization options.
  • Regularly practice using CLI commands to improve your efficiency and comfort level.

This markdown document provides a basic introduction to exploring the file system using the CLI, including ways to check file and directory sizes. There are many other commands and techniques available, so continue learning and experimenting to master this powerful tool.