Featured image of post Essential File Management Commands: A Quick Guide

Essential File Management Commands: A Quick Guide

Discover key file management commands for efficient handling of files and directories. Learn to create, move, copy, and delete files, view contents, manage disk usage, and more with practical examples.

# Basic Commands

  • touch: Creates a new empty file.

    • Example: touch newfile.txt creates a new empty file named newfile.txt.
  • mkdir: Creates a new directory.

    • Example: mkdir newdir creates a new directory named newdir.
  • mv: Moves or renames files and directories.

    • Example: mv oldfile.txt newfile.txt renames oldfile.txt to newfile.txt.
  • cp: Copies files and directories.

    • Example: cp file1.txt file2.txt copies file1.txt to file2.txt.
  • rm: Removes files.

    • Example: rm file.txt deletes file.txt.
  • rmdir: Removes empty directories.

    • Example: rmdir emptydir deletes an empty directory named emptydir.

# Pattern Matching and Globbing

  • Globbing with *: Matches any number of characters.
    • Example: rm *.txt removes all files ending with .txt.

# Advanced Globbing with Wildcards

  • [0-9]: Matches any single digit.

    • Example: ls file[0-9].txt lists files like file1.txt, file2.txt, etc.
  • [a-z]: Matches any single lowercase letter.

  • [A-Z]: Matches any single uppercase letter.

  • ?: Matches any single character.

  • **: Used for recursive globbing in newer versions of Bash.

# Viewing File Content

  • cat: Displays the entire content of a file.

    • Example: cat file.txt displays the contents of file.txt.
  • head: Displays the first few lines of a file.

    • Example: head -n 5 file.txt shows the first 5 lines of file.txt.
  • tail: Displays the last few lines of a file.

    • Example: tail -n 5 file.txt shows the last 5 lines of file.txt.

# Counting Words and Disk Usage

  • wc: Counts lines, words, and characters in a file.

    • Example: wc file.txt displays the line, word, and character count of file.txt.
  • du: Shows the disk usage of files and directories.

    • Example: du -sh * shows the size of all files and directories in the current directory in human-readable format.

# Writing and Appending to Text Files

  • Redirection Operator >: Writes output to a file, overwriting it.

    • Example: echo "Hello" > file.txt writes “Hello” to file.txt, overwriting its contents.
  • Appending Operator >>: Appends output to a file.

    • Example: echo "World" >> file.txt appends “World” to file.txt.

# The Standard Streams

  • stdin (Standard Input): Usually the keyboard.
  • stdout (Standard Output): The screen or a file.
  • stderr (Standard Error): Error messages output stream.

# Dual Output with tee

  • tee: Reads from stdin and writes to stdout and files.
    • Example: echo "Hello" | tee file.txt writes “Hello” to both file.txt and the screen.

# Pipe: |

  • Used to pass the output of one command as input to another.
    • Example: cat file.txt | grep "Hello" passes the contents of file.txt to grep to search for “Hello”.

# Sorting and Removing Duplicates

  • sort: Sorts lines in text files.

    • Example: sort file.txt sorts the lines in file.txt.
  • uniq: Removes duplicate lines from sorted data.

    • Example: sort file.txt | uniq sorts file.txt and removes duplicate lines.

# Searching for Patterns with grep

  • grep: Searches for patterns in files.
    • Example: grep "Hello" file.txt searches for the word “Hello” in file.txt.

# Character Replacement and Reversal

  • tr: Translates or deletes characters.

    • Example: echo "hello" | tr 'a-z' 'A-Z' translates lowercase to uppercase, outputting “HELLO”.
  • rev: Reverses lines character-wise.

    • Example: echo "hello" | rev outputs “olleh”.

# The cut Program

  • cut: Removes sections from each line of files.
    • Example: cut -d':' -f1 file.txt cuts out the first field from each line in file.txt, with fields delimited by ‘:’.

# Text Substitution with sed

  • sed: A stream editor for filtering and transforming text.
    • Example: sed 's/old/new/g' file.txt replaces all occurrences of “old” with “new” in file.txt.
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy