DEV Community

yuna song
yuna song

Posted on

Linux Basics: Essential File System Commands

1. cd — Change Directory

Changes the current working directory.

Syntax

cd <path>
Enter fullscreen mode Exit fullscreen mode

Common Usage

cd /              # Go to the root directory
cd ~              # Go to the current user's home directory
cd ..             # Move up one directory
cd -              # Return to the previous directory
cd ./Documents    # Relative path
cd /var/log       # Absolute path
Enter fullscreen mode Exit fullscreen mode

Path Types

  • Absolute path: Starts with / and specifies the full path from the root directory.
  • Relative path: Starts from the current directory (e.g. ./, ../, or a directory name).

Tips

  • Press Tab for auto-completion.
  • Use quotes for directory names containing spaces.
cd "My Folder"
Enter fullscreen mode Exit fullscreen mode

2. ls — List Files and Directories

Displays the contents of a directory.

Syntax

ls [options] [path]
Enter fullscreen mode Exit fullscreen mode

Common Options

ls          # List files
ls -l       # Long format
ls -a       # Include hidden files
ls -la      # Long format + hidden files
ls -lh      # Human-readable file sizes
ls -t       # Sort by modification time
ls -S       # Sort by file size
ls -r       # Reverse sort order
ls -R       # Recursive listing
ls -d */    # Show directories only
Enter fullscreen mode Exit fullscreen mode

Useful Combinations

ls -altr    # Hidden files, long format, time sort, reverse order
ls -alF     # Append indicators (/, *, etc.)
ls -lthr    # Human-readable, time sort, newest at the bottom
Enter fullscreen mode Exit fullscreen mode

3. pwd — Print Working Directory

Displays the absolute path of the current directory.

Syntax

pwd
Enter fullscreen mode Exit fullscreen mode

Options

pwd -L      # Logical path (default)
pwd -P      # Physical path (resolve symbolic links)
Enter fullscreen mode Exit fullscreen mode

Example:

/home/user/workspace
Enter fullscreen mode Exit fullscreen mode

4. mkdir — Make Directory

Creates one or more directories.

Syntax

mkdir [options] <directory>
Enter fullscreen mode Exit fullscreen mode

Common Usage

mkdir project
mkdir dir1 dir2 dir3
mkdir -p parent/child/grandchild
mkdir -v new_folder
mkdir -m 755 secure_folder
Enter fullscreen mode Exit fullscreen mode

Useful Options

  • -p : Create parent directories if needed.
  • -v : Show created directories.
  • -m : Set permissions when creating the directory.

5. rm — Remove Files and Directories

Deletes files or directories permanently.

Warning: Deleted files cannot be recovered from the Trash.

Syntax

rm [options] <file>
Enter fullscreen mode Exit fullscreen mode

Common Usage

rm file.txt
rm -i file.txt
rm -r directory
rm -f file.txt
rm -rf directory
Enter fullscreen mode Exit fullscreen mode

Multiple Files & Wildcards

rm file1.txt file2.txt
rm *.log
Enter fullscreen mode Exit fullscreen mode

Important Options

  • -i : Ask for confirmation.
  • -r : Remove directories recursively.
  • -f : Force deletion without prompts.

Never run

sudo rm -rf /

This command can destroy the entire operating system.


6. cp — Copy Files and Directories

Copies files or directories while keeping the original intact.

Syntax

cp [options] <source> <destination>
Enter fullscreen mode Exit fullscreen mode

Common Usage

cp file1.txt file2.txt
cp -r dir1 dir2
cp file1.txt file2.txt ~/backup/
cp -a /etc/config.conf ~/backup/
Enter fullscreen mode Exit fullscreen mode

Useful Options

  • -r / -R : Copy directories recursively.
  • -a : Archive mode (preserve attributes, permissions, timestamps, and symbolic links).
  • -p : Preserve file metadata.
  • -i : Confirm before overwriting.
  • -f : Force overwrite.

7. mv — Move or Rename Files

Moves files/directories or renames them.

Syntax

mv [options] <source> <destination>
Enter fullscreen mode Exit fullscreen mode

Common Usage

mv file1.txt ~/Documents/
mv file1.txt file2.txt
mv folder1 ~/backup/
mv file1.txt file2.txt ~/backup/
Enter fullscreen mode Exit fullscreen mode

Useful Options

  • -i : Confirm before overwriting.
  • -f : Force overwrite.
  • -n : Never overwrite existing files.
  • -v : Show each move operation.

Top comments (0)