Skip to content

A bash helper script to operate on the most recently touched file or directory

License

permacommons/latest.sh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest - Operate on Most Recently Modified Files

A bash function to operate on the most recently modified file or directory in the current working directory.

Installation

Easy Installation (Recommended)

Run the installation script to automatically add the function to your shell configuration:

./install.sh

The installer will:

  • Auto-detect your shell (bash/zsh) and config file
  • Add or update the latest function in your config
  • Handle existing installations gracefully
  • Provide clear feedback on what was done

Installation Options

# Install to auto-detected config file (default)
./install.sh

# Install to auto-detected config file (explicit)
./install.sh install

# Install to a specific config file
./install.sh --config ~/.profile

# Uninstall from auto-detected config file
./install.sh uninstall

# Show help
./install.sh --help

Manual Installation

If you prefer manual installation, source the function in your shell:

source latest.sh

Or add it to your .bashrc or .zshrc:

echo "source /path/to/latest.sh" >> ~/.bashrc

Usage

latest [command] [arguments...]

Examples

# Open the most recent file with default application (xdg-open)
latest

# View the most recent file
latest cat

# Copy the most recent file to backup directory
latest cp ~/Backups/

# Move the most recent file to another directory
latest mv ~/Documents/

# List details of the most recent file
latest ls -la

# Edit the most recent file
latest vim

# Show file type of most recent file
latest file

How it works

The function uses ls -1t to list files sorted by modification time (newest first), then takes the first result and passes it to the specified command.

  • If no command is provided, it defaults to xdg-open
  • Works with both files and directories
  • Handles filenames with spaces correctly
  • Returns error if current directory is empty

Testing

Run the test suite:

./test_latest.sh

The test script includes comprehensive tests for:

  • Basic functionality
  • Command execution with arguments
  • Directory handling
  • Empty directory handling
  • Files with spaces in names
  • Executable files
  • Modification time ordering

Files

  • latest.sh - Main function implementation
  • install.sh - Easy installation/uninstallation script
  • test_latest.sh - Comprehensive test suite for CI
  • test_fixtures/ - Test files and directories
  • README.md - This documentation

Updating

To update an existing installation, simply run the installer again:

./install.sh

The installer will automatically remove the old version and install the new one.

Uninstalling

To remove the function from your shell configuration:

./install.sh uninstall

About

A bash helper script to operate on the most recently touched file or directory

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages