GithubHelp home page GithubHelp logo

benlinton / slugify Goto Github PK

View Code? Open in Web Editor NEW
300.0 3.0 36.0 11 KB

Bash command that converts filenames and directories to a web friendly format.

License: MIT License

Shell 40.19% Groff 59.81%

slugify's Introduction

Bash Command: Slugify

Slugify is a bash command that converts filenames and directories to a web friendly format.

Usage Help

Simply enter the slugify command without any arguments or with the -h option to view the usage help.

$ slugify
usage: slugify [-acdhintuv] source_file ...
   -a: remove spaces immediately adjacent to dashes
   -c: consolidate consecutive spaces into single space
   -d: replace spaces with dashes (instead of default underscores)
   -h: help
   -i: ignore case
   -n: dry run
   -t: treat existing dashes as spaces
   -u: treat existing underscores as spaces (useful with -a, -c, or -d)
   -v: verbose

Usage Examples

Note, most examples below are run in verbose mode (-v) to help illustrate the results.

Verbose mode is unnecessary in real world scenarios.

Provide escaped filenames:

$ slugify -v My\ \ file.txt
rename: My  file.txt -> my__file.txt

Alternatively provide unescaped filenames inside quotes:

$ slugify -v "My  file.txt"
rename: My  file.txt -> my__file.txt

Globs (like * and ?) work as well:

$ slugify -v *.txt
rename: My file.txt -> my_file.txt
ignore: my_web_friendly_filename.txt

Provide an unlimited number of arguments:

$ slugify -v "My first file.txt" "My second file.txt"
rename: My first file.txt -> my_first_file.txt
rename: My second file.txt -> my_second_file.txt

Directories are also supported:

$ slugify -v "My Directory"
rename: My Directory -> my_directory

Consolidate consecutive spaces into single spaces:

$ slugify -vc "My    consolidated    file.txt"
rename: My    consolidated    file.txt -> my_consolidated_file.txt

Replace spaces with dashes:

$ slugify -vd "My dashed file.txt"
rename: My dashed file.txt -> my-dashed-file.txt

The -d option replaces each space with a dash.

$ slugify -vd "My  dashed  file.txt"
rename: My  dashed  file.txt -> my--dashed--file.txt

Combine -d with -c (consolidate spaces) for a single dash between each word.

$ slugify -vdc "My  dashed  file.txt"
rename: My  dashed  file.txt -> my-dashed-file.txt

Ignore case:

$ slugify -vi "UPPER CASE FILE.txt"
rename: UPPER CASE FILE.txt -> UPPER_CASE_FILE.txt

Play it safe with a dry run:

Dry run mode does not alter the filesystem in any way.

$ slugify -n *
--- Begin dry run mode.
rename: My file.txt -> my_file.txt
ignore: web_friendly_filename.txt
--- End dry run mode.

Dry run mode also allows you to test filenames that don't exist. Great for testing!

$ slugify -n "Ghost File.txt"
--- Begin dry run mode.
not found: Ghost File.txt
rename: Ghost File.txt -> ghost_file.txt
--- End dry run mode.

Dry run mode automatically enables verbose mode so there is no need to include the -v option with -n.

Handle spaces adjacent to dashes:

In this example, without -a the dashes end up surrounded by underscores.

$ slugify -v "The Beatles - Yellow Submarine.mp3"
rename: The Beatles - Yellow Submarine.mp3 -> the_beatles_-_yellow_submarine.mp3

But with -a the adjacent spaces are removed.

$ slugify -va "The Beatles - Yellow Submarine.mp3"
rename: The Beatles - Yellow Submarine.mp3 -> the_beatles-yellow_submarine.mp3

The -a only removes spaces immediately adjacent to a dash, which may not be the desired effect (below three spaces on either side of the dash get converted into two underscores because of -a).

$ slugify -va "The Beatles   -   Yellow Submarine.mp3"
rename: The Beatles   -   Yellow Submarine.mp3 -> the_beatles__-__yellow_submarine.mp3

But -c consolidates spaces into a single space and then -a will remove the left over adjacent single spaces.

$ slugify -vac "The Beatles   -   Yellow Submarine.mp3"
rename: The Beatles - Yellow Submarine.mp3 -> the_beatles-yellow_submarine.mp3

Convert existing underscores into dashes

The -u treats underscores as spaces and -d converts spaces into dashes.

$ slugify -vud "Spaces Dashes-And_Underscores.txt"
rename: Spaces Dashes-And_Underscores.txt -> spaces-dashes-and-underscores.txt

Convert existing dashes into underscores

$ slugify -vt "Spaces Dashes-And_Underscores.txt"
rename: Spaces Dashes-And_Underscores.txt -> spaces_dashes_and_underscores.txt

slugify's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

slugify's Issues

Doesn't really follow the Unix principles

Sorry to nitpick, this is a good little util, but it doesn't really follow the unix principles.

I should be able to pipe a format to this util like so:

$ echo "Some String" | slugify -d
some-string

It'd also be great if the output was less noisy, so either:

$ slugify -v My\ \ file.txt
my__file.txt

(or no output, as nothing is startling?):

$ slugify -v My\ \ file.txt

Also, just personal opinion, but I think it'd probably be better if the filesystem functionality was optional, rather than default behaviour.

Like I said, sorry to nitpick, just doesn't seem to run the way one would expect!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.