Posted on ::

File and Directory Management Commands

ls: List directory contents

ls - List all files and directories in current location

    ls

List all, including hidden files (those starting with a .):

    ls -a

Long listing format with details (permissions, owner, size, date):

    ls -l

Show human-readable file sizes (e.g., KB, MB):

    ls -lh

Show all files, including hidden, in long format:

    ls -la

List contents of the ‘data’ directory:

    ls data/
```b
List files in all subdirectories recursively:
```bash
    ls -R
```b
List directories only:
```bash
    ls -d */
```b
Sort by modification time (most recent first):
```bash
    ls -lt
```b
List files in reverse order
```bash
    ls -r
```b
Show entries separated by commas:
```bash
    ls -m
```b

Show all scripts in the ‘scripts’ directory with details:
```bash
    ls -l scripts/
```b
Recursively list everything in ‘logs’ and its subdirectories:
```bash
    ls -lR logs/
```b
List all files in ‘results’ directory, sorted by modification date:
```bash
    ls -lt results/
```b
List only directories in root:
    ```bash
    ls -d */

pwd: Present working directory

Print the current working directory: Command that displays the full path of your current directory

    pwd

cd: Change directory

Command that displays the full path of your current directory

    cd
    cd ~

Move to the parent directory:

    cd ..

Go to the root directory: Switches to the filesystem’s root.

    cd/

Change to a specific directory by absolute path: Return to the previous directory:

    cd -

Examples

    cd data/
    cd logs/
    cd ..
    cd /home/user/root/scripts/
    cd results/
    cd -
    cd
    cd root/data/

mkdir: Make new directory

Create a single directory:

    mkdir new_folder

Create multiple directories at once:

    mkdir dir1 dir2 dir3

Create a directory inside another existing directory:

    mkdir data/archive

Create nested parent-child directories with -p:Automatically creates all missing parent directories (2025, July, etc.).

    mkdir -p data/2025/July/reports

Create a directory with spaces in its name:

    mkdir "project backup"

Create directories named by date:

    mkdir "results_$(date +%Y-%m-%d)"

rmdir: Remove directory

The rmdir command is used to remove empty directories in Unix/Linux systems. It cannot delete directories that contain files or subdirectories.

Remove a single empty directory: Deletes empty_dir if it is empty

    rmdir empty_dir

Remove multiple empty directories at once: Removes dir1, dir2, and dir3 if all are empty

    rmdir dir1 dir2 dir3

Use the verbose flag to display messages on deletion: Outputs a confirmation message for each directory removed

    rmdir -v logs/

rm: Remove files or directories

If you need to delete a directory that contains files or other directories, use the rm -r command instead:

    rm -r non_empty_dir

Remove all files in the current directory (non-hidden):

    rm *

Remove all files in a specific subdirectory:

    rm logs/*

Remove files of a specific type or pattern:

    rm *.log

Remove all files and folders (minus hidden ones):

    rm -r *

Force delete all files without confirmation (dangerous):

    rm -f *

To delete hidden files (e.g., .gitignore, .env), you need to target them with:

    rm .*

cp: Copy files and directories

Copy a file to another location

    cp file.txt data/

Copy and rename file:

    cp file.txt backup_file.txt

Copy multiple files to a directory

    cp file1.txt file2.txt scripts/

Copy an entire directory recursively

    cp -r scripts/ backup_scripts/

Ask before overwriting (interactive):

    cp -i results/data.csv data/

Force overwrite without asking:

    cp -f logs/today.log logs/backup.log

Copy files while preserving timestamps, permissions, etc.:

    cp -p scripts/tool.sh scripts/archive/

Archive mode (preserves everything, handles symlinks, and hidden files):

    cp -a results/ archive_results/

Create a backup of an existing file (adds suffix):

    cp --backup=numbered logs/activity.log logs/activity.log.back

Copy all .txt files to another directory:

    cp *.txt data/

Copy hidden files (those starting with .):

    cp -a .config scripts/

Back up all scripts:

    cp -a scripts/ scripts_backup

mv: Move or rename files and directories

The mv command (move) is essential in Unix/Linux for moving or renaming files and directories

Move a file to another directory:

    mv file.txt data/

Move and rename a file:

    mv report.txt old_report.txt

Move multiple files to a directory:

    mv *.csv results/

Move an entire directory to another location:

    mv logs/ old_logs/

Move a subdirectory into a parent directory:

    mv data/archive/ logs/

Prompt before overwriting files:

    mv -i scripts/tool.sh scripts/backup.sh

Force overwrite existing files (no prompt):

    mv -f logs/today.log logs/yesterday.log

Move all .log files except hidden ones:

    mv *.log logs/

Move hidden files (those starting with .):

    mv .env .gitignore data/

More Examples:

Moves all shell scripts into the new bash_tools/ directory under scripts/.

    mv scripts/*.sh scripts/bash_tools/

Moves all results from June 2025 into an archive.

    mv results/2025-06-* results/archive/

Renames the temp directory to intermediate.

    mv data/temp/ data/intermediate/

## tree: View files and directories as a tree

View current directory as a tree:
```bash
    tree

Show hidden files too:

    tree -a

Limit depth of subdirectories:

    tree -L 2

List directories only (no files):

    tree -d

Show full file paths in output:

    tree -f

Save the directory structure for documentation:

    tree > root_tree.txt

touch: Create an empty file

The touch command in Unix/Linux is used to create empty files and update the timestamps of existing files without modifying their contents

Create a new empty file: bashbash touch notes.txt


Create multiple empty files at once
```bash
    touch file1.txt file2.txt file3.txt

Create files with brace expansion:

    touch data_{1..5}.csv

Update only the access time of a file:

    touch -a report.txt

Update only the modification time:

    touch -m script.sh

Set a specific time and date: Assigns a custom timestamp (format: YYYYMMDDhhmm.ss)

    touch -t 202507230800.00 logs/daily.log

cat: View a file’s content

The cat command (short for “concatenate”) is widely used in Unix/Linux for viewing, combining, and manipulating text files

Display the contents of a single file: bashbash cat data/info.txt

Display multiple files in sequence:
```bash
    cat scripts/tool.sh logs/latest.log

Add line numbers to output

    cat -n results/summary.txt

Create a new file and add content (end with Ctrl+D):

    cat > notes.txt

Append text to an existing file:

    cat >> logs/today.log

Combine multiple files into a new file:
```bash
    cat data/file1.txt data/file2.txt > data/combined.txt

Append the contents of one file to another:

    cat results/new.csv >> data/archive.csv

Display content page by page (for large files):

    cat longfile.txt | more

Check configuration files side by side:

    cat scripts/config.sh data/params.ini

Prepare a new data file from scratch: Enter data, then Ctrl+D to save.

    cat > data/input.txt

less, more: View large files page by page

Both less and more are pager programs used in Unix/Linux to view the contents of text files one screen at a time, especially useful for large files that don’t fit in one terminal window.

View a file one page at a time:

    less logs/latest.log

View multiple files:

    less logs/*.log

Navigate between multiple log files with :n (next) or :p (previous). Search forward in the file:Type /pattern and press Enter to find “pattern”. Search backward:Type ?pattern and press Enter. Go to a specific line number:Inside less, type 123 then press Enter to jump to line 123. Quit less:Press q.

head / tail: Display first/last lines of a file

The head and tail commands in Linux are used to view the beginning and end parts of text files, respectively. Both are especially useful for inspecting logs, configuration files, or any large files efficiently.

Display the first 10 lines of a file (default):

    head logs/latest.log

Show the first N lines (e.g., 20 lines):

    head -n 20 data/info.txt

Display first N lines from multiple files:

    head -n 10 logs/*.log

Display the last 10 lines of a file (default):

    tail logs/latest.log

Show the last N lines (e.g., 15 lines):

    tail -n 15 data/output.csv

Monitor multiple files simultaneously:

    tail -f logs/app.log logs/error.log

Extract a specific line (e.g., line 5) using head and tail:

    head -n 5 file.txt | tail -n 1

Display lines 10 to 20:

    head -n 20 file.txt | tail -n 11
Table of Contents