Shell Commands: A Beginner's Guide to Files and Directories

Shell Commands: A Beginner's Guide to Files and Directories

github.com/kristofer june 2025, v2

The shell (also called terminal or command line) is a text-based interface for interacting with your computer’s operating system. While graphical interfaces let you click on folders and files, the shell lets you navigate and manipulate files using typed commands. This might seem less intuitive at first, but the shell is incredibly powerful and efficient once you learn the basics. Many programming tasks, server administration, and automation require shell knowledge, making it an essential skill for developers.

Navigation Commands

Navigation commands help you move around your computer’s file system and understand where you are. Think of your file system as a tree structure with folders (directories) containing files and other folders. These commands let you explore this structure efficiently.

pwd (Print Working Directory)

Shows your current location in the file system. When working in the shell, you’re always “inside” a specific directory, and pwd tells you exactly where that is. This is essential because many commands work relative to your current location.

1pwd
2# Output: /home/username/documents

cd (Change Directory)

Move between directories. This is like double-clicking on folders in a graphical interface, but using text commands. Understanding relative paths (like .. for parent directory) versus absolute paths (starting from the root directory) will make navigation much more efficient.

 1# Go to home directory
 2cd
 3cd ~
 4
 5# Go up one directory
 6cd ..
 7
 8# Go up two directories
 9cd ../..
10
11# Go to specific directory
12cd Documents
13
14# Go to absolute path
15cd /home/username/Documents
16
17# Go to previous directory
18cd -

ls (List Directory Contents)

View files and directories in your current location. The ls command has many useful options (flags) that change how information is displayed. Learning these options helps you quickly find the information you need about your files.

 1# Basic listing
 2ls
 3
 4# Detailed list with permissions and sizes
 5ls -l
 6
 7# Show hidden files
 8ls -a
 9
10# Detailed list including hidden files
11ls -la
12
13# Human-readable file sizes
14ls -lh
15
16# Sort by modification time
17ls -lt
18
19# Sort by size
20ls -lS

Creating and Removing Content

These commands let you create new files and directories, and remove them when no longer needed. Unlike graphical interfaces where deleted files often go to a trash/recycle bin, shell deletion is usually permanent, so it’s important to be careful and double-check before removing files.

mkdir (Make Directory)

Create new directories (folders). The -p option is particularly useful as it creates parent directories automatically if they don’t exist, allowing you to create complex directory structures in one command.

1# Create single directory
2mkdir photos
3
4# Create multiple directories
5mkdir music videos documents
6
7# Create nested directories
8mkdir -p parent/child/grandchild

touch (Create Empty File)

Create new files or update timestamps. While touch is primarily designed to update file timestamps, it’s commonly used to quickly create empty files. This is useful when you need placeholder files or want to create files that other programs will write to later.

1# Create single file
2touch note.txt
3
4# Create multiple files
5touch file1.txt file2.txt file3.txt
6
7# Update file timestamp
8touch existing_file.txt

rm (Remove)

Delete files and directories. This is one of the most dangerous commands in the shell because deletion is permanent. The -r flag means “recursive” (delete directories and their contents), while -f means “force” (don’t ask for confirmation). Always double-check what you’re deleting, especially when using -rf together.

 1# Remove file
 2rm file.txt
 3
 4# Remove empty directory
 5rmdir empty_directory
 6
 7# Remove directory and contents
 8rm -r directory_name
 9
10# Force remove without confirmation
11rm -f file.txt
12
13# Force remove directory and contents
14rm -rf directory_name

Copying and Moving

These commands let you duplicate files and move them to different locations. Understanding the difference between copying (creating a duplicate) and moving (changing location) is important for organizing your files effectively.

cp (Copy)

Copy files and directories. When copying directories, you need the -r (recursive) flag to copy all contents. The last argument is always the destination - this could be a new filename or a directory where you want to place the copy.

 1# Copy file
 2cp source.txt destination.txt
 3
 4# Copy file to directory
 5cp file.txt documents/
 6
 7# Copy directory and contents
 8cp -r source_directory destination_directory
 9
10# Copy multiple files to directory
11cp file1.txt file2.txt destination_directory/

mv (Move/Rename)

Move or rename files and directories. The mv command serves two purposes: moving files to different locations and renaming them. If the destination is in the same directory, you’re renaming; if it’s in a different directory, you’re moving.

 1# Rename file
 2mv oldname.txt newname.txt
 3
 4# Move file to directory
 5mv file.txt documents/
 6
 7# Move multiple files
 8mv file1.txt file2.txt directory/
 9
10# Move and rename directory
11mv old_directory new_directory

Viewing File Contents

These commands let you examine what’s inside files without opening them in an editor. Different commands are useful for different situations - viewing entire files, just the beginning or end, or browsing through long files interactively.

cat (Concatenate)

Display file contents. The name “cat” comes from “concatenate” because it can join multiple files together, but it’s most commonly used to quickly view file contents. It displays the entire file at once, which is perfect for short files but overwhelming for long ones.

1# View file contents
2cat file.txt
3
4# View multiple files
5cat file1.txt file2.txt
6
7# Create file with content
8cat > newfile.txt
9# Type content, press Ctrl+D when done

less

View long files with scrolling. Unlike cat, which dumps the entire file to your screen, less lets you scroll through files page by page. This is essential for viewing large files, log files, or any content that’s longer than your screen can display at once.

1less longfile.txt
2# Use arrow keys to scroll
3# Press 'q' to quit

head and tail

View beginning or end of file. These commands are perfect when you only need to see part of a file. head shows the first few lines (useful for seeing file headers or structure), while tail shows the last few lines (great for checking recent log entries or file endings).

1# View first 10 lines
2head file.txt
3
4# View last 10 lines
5tail file.txt
6
7# View specific number of lines
8head -n 5 file.txt
9tail -n 5 file.txt

Useful Tips

These features and shortcuts will significantly improve your efficiency when working in the shell. They reduce typing, help prevent errors, and make the command line much more user-friendly.

Wildcards

Wildcards are special characters that match patterns in filenames, allowing you to work with multiple files at once without typing each name individually. This is incredibly powerful for batch operations.

1# All files ending in .txt
2ls *.txt
3
4# All files starting with 'data'
5ls data*
6
7# Files with single character wildcard
8ls file?.txt

Tab Completion

Tab completion is one of the most useful shell features - it saves typing and prevents errors by automatically completing commands and filenames.

  • Press Tab to auto-complete commands and file names
  • Press Tab twice to see all possible completions

Command History

The shell remembers commands you’ve used, making it easy to repeat or modify previous commands without retyping everything.

1# View command history
2history
3
4# Search history (press Ctrl+R)
5# Start typing to search through previous commands
6
7# Repeat last command
8!!

Getting Help

When you’re unsure about a command, these tools provide built-in documentation and information. Learning to use help systems effectively makes you more independent and efficient.

1# View command manual
2man command_name
3
4# Quick command help
5command_name --help
6
7# View command location
8which command_name

Best Practices

  1. Use meaningful file and directory names
  2. Avoid spaces in names (use underscores or hyphens)
  3. Be careful with rm -rf (it cannot be undone)
  4. Make regular backups of important files
  5. Use tab completion to avoid typing errors
  6. Keep your files organized in appropriate directories

Remember: Practice these commands in a test directory first until you’re comfortable with how they work. The shell is case-sensitive, so be careful with capitalization.