A bash function to operate on the most recently modified file or directory in the current working directory.
Run the installation script to automatically add the function to your shell configuration:
./install.shThe installer will:
- Auto-detect your shell (bash/zsh) and config file
- Add or update the
latestfunction in your config - Handle existing installations gracefully
- Provide clear feedback on what was done
# 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 --helpIf you prefer manual installation, source the function in your shell:
source latest.shOr add it to your .bashrc or .zshrc:
echo "source /path/to/latest.sh" >> ~/.bashrclatest [command] [arguments...]# 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 fileThe 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
Run the test suite:
./test_latest.shThe 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
latest.sh- Main function implementationinstall.sh- Easy installation/uninstallation scripttest_latest.sh- Comprehensive test suite for CItest_fixtures/- Test files and directoriesREADME.md- This documentation
To update an existing installation, simply run the installer again:
./install.shThe installer will automatically remove the old version and install the new one.
To remove the function from your shell configuration:
./install.sh uninstall