├── LICENSE ├── README.md ├── slugify ├── slugify.1 └── slugify.1.ronn /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2012 Benjamin Linton 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bash Command: Slugify 2 | 3 | Slugify is a bash command that converts filenames and directories to a web friendly format. 4 | 5 | ## Usage Help 6 | 7 | Simply enter the slugify command without any arguments or with the -h option to view the usage help. 8 | 9 | $ slugify 10 | usage: slugify [-acdhintuv] source_file ... 11 | -a: remove spaces immediately adjacent to dashes 12 | -c: consolidate consecutive spaces into single space 13 | -d: replace spaces with dashes (instead of default underscores) 14 | -h: help 15 | -i: ignore case 16 | -n: dry run 17 | -t: treat existing dashes as spaces 18 | -u: treat existing underscores as spaces (useful with -a, -c, or -d) 19 | -v: verbose 20 | 21 | ## Usage Examples 22 | 23 | Note, most examples below are run in verbose mode (-v) to help illustrate the results. 24 | 25 | Verbose mode is unnecessary in real world scenarios. 26 | 27 | #### Provide escaped filenames: 28 | 29 | $ slugify -v My\ \ file.txt 30 | rename: My file.txt -> my__file.txt 31 | 32 | #### Alternatively provide unescaped filenames inside quotes: 33 | 34 | $ slugify -v "My file.txt" 35 | rename: My file.txt -> my__file.txt 36 | 37 | #### Globs (like * and ?) work as well: 38 | 39 | $ slugify -v *.txt 40 | rename: My file.txt -> my_file.txt 41 | ignore: my_web_friendly_filename.txt 42 | 43 | #### Provide an unlimited number of arguments: 44 | 45 | $ slugify -v "My first file.txt" "My second file.txt" 46 | rename: My first file.txt -> my_first_file.txt 47 | rename: My second file.txt -> my_second_file.txt 48 | 49 | #### Directories are also supported: 50 | 51 | $ slugify -v "My Directory" 52 | rename: My Directory -> my_directory 53 | 54 | #### Consolidate consecutive spaces into single spaces: 55 | 56 | $ slugify -vc "My consolidated file.txt" 57 | rename: My consolidated file.txt -> my_consolidated_file.txt 58 | 59 | #### Replace spaces with dashes: 60 | 61 | $ slugify -vd "My dashed file.txt" 62 | rename: My dashed file.txt -> my-dashed-file.txt 63 | 64 | The -d option replaces each space with a dash. 65 | 66 | $ slugify -vd "My dashed file.txt" 67 | rename: My dashed file.txt -> my--dashed--file.txt 68 | 69 | Combine -d with -c (consolidate spaces) for a single dash between each word. 70 | 71 | $ slugify -vdc "My dashed file.txt" 72 | rename: My dashed file.txt -> my-dashed-file.txt 73 | 74 | #### Ignore case: 75 | 76 | $ slugify -vi "UPPER CASE FILE.txt" 77 | rename: UPPER CASE FILE.txt -> UPPER_CASE_FILE.txt 78 | 79 | #### Play it safe with a dry run: 80 | 81 | Dry run mode does not alter the filesystem in any way. 82 | 83 | $ slugify -n * 84 | --- Begin dry run mode. 85 | rename: My file.txt -> my_file.txt 86 | ignore: web_friendly_filename.txt 87 | --- End dry run mode. 88 | 89 | Dry run mode also allows you to test filenames that don't exist. Great for testing! 90 | 91 | $ slugify -n "Ghost File.txt" 92 | --- Begin dry run mode. 93 | not found: Ghost File.txt 94 | rename: Ghost File.txt -> ghost_file.txt 95 | --- End dry run mode. 96 | 97 | Dry run mode automatically enables verbose mode so there is no need to include the -v option with -n. 98 | 99 | #### Handle spaces adjacent to dashes: 100 | 101 | In this example, without -a the dashes end up surrounded by underscores. 102 | 103 | $ slugify -v "The Beatles - Yellow Submarine.mp3" 104 | rename: The Beatles - Yellow Submarine.mp3 -> the_beatles_-_yellow_submarine.mp3 105 | 106 | But with -a the adjacent spaces are removed. 107 | 108 | $ slugify -va "The Beatles - Yellow Submarine.mp3" 109 | rename: The Beatles - Yellow Submarine.mp3 -> the_beatles-yellow_submarine.mp3 110 | 111 | 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). 112 | 113 | $ slugify -va "The Beatles - Yellow Submarine.mp3" 114 | rename: The Beatles - Yellow Submarine.mp3 -> the_beatles__-__yellow_submarine.mp3 115 | 116 | But -c consolidates spaces into a single space and then -a will remove the left over adjacent single spaces. 117 | 118 | $ slugify -vac "The Beatles - Yellow Submarine.mp3" 119 | rename: The Beatles - Yellow Submarine.mp3 -> the_beatles-yellow_submarine.mp3 120 | 121 | #### Convert existing underscores into dashes 122 | 123 | The -u treats underscores as spaces and -d converts spaces into dashes. 124 | 125 | $ slugify -vud "Spaces Dashes-And_Underscores.txt" 126 | rename: Spaces Dashes-And_Underscores.txt -> spaces-dashes-and-underscores.txt 127 | 128 | #### Convert existing dashes into underscores 129 | 130 | $ slugify -vt "Spaces Dashes-And_Underscores.txt" 131 | rename: Spaces Dashes-And_Underscores.txt -> spaces_dashes_and_underscores.txt 132 | 133 | -------------------------------------------------------------------------------- /slugify: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | #: Name : slugify 3 | #: Date : 2012-05-01 4 | #: Author : "Benjamin Linton" 5 | #: Version : 1.0.1 6 | #: Description : Convert filenames into a web friendly format. 7 | #: Options : See print_usage() function. 8 | 9 | ## Initialize defaults 10 | script_name=${0##*/} 11 | dashes_omit_adjacent_spaces=0 12 | consolidate_spaces=0 13 | space_replace_char='_' 14 | ignore_case=0 15 | dry_run=0 16 | dashes_to_spaces=0 17 | underscores_to_spaces=0 18 | verbose=0 19 | 20 | ## Initialize valid options 21 | opt_string=acdhintuv 22 | 23 | ## Usage function 24 | function print_usage(){ 25 | echo "usage: $script_name [-$opt_string] source_file ..." 26 | echo " -a: remove spaces immediately adjacent to dashes" 27 | echo " -c: consolidate consecutive spaces into single space" 28 | echo " -d: replace spaces with dashes (instead of default underscores)" 29 | echo " -h: help" 30 | echo " -i: ignore case" 31 | echo " -n: dry run" 32 | echo " -t: treat existing dashes as spaces" 33 | echo " -u: treat existing underscores as spaces (useful with -a, -c, or -d)" 34 | echo " -v: verbose" 35 | } 36 | 37 | ## For each provided option arg 38 | while getopts $opt_string opt 39 | do 40 | case $opt in 41 | a) dashes_omit_adjacent_spaces=1 ;; 42 | c) consolidate_spaces=1 ;; 43 | d) space_replace_char='-' ;; 44 | h) print_usage; exit 0 ;; 45 | i) ignore_case=1 ;; 46 | n) dry_run=1 ;; 47 | t) dashes_to_spaces=1 ;; 48 | u) underscores_to_spaces=1 ;; 49 | v) verbose=1 ;; 50 | *) exit 1 ;; 51 | esac 52 | done 53 | 54 | ## Remove options from args 55 | shift "$(( $OPTIND - 1 ))" 56 | 57 | ## Unless source_file arg(s) found, print usage and exit (0 to avoid breaking pipes) 58 | if [[ ! -n "$1" ]]; then 59 | print_usage 60 | exit 0 61 | fi 62 | 63 | ## Identify case insensitive filesystems 64 | case_sensitive_filesystem=1 65 | case $OSTYPE in 66 | darwin*) case_sensitive_filesystem=0 ;; # OS X 67 | *) ;; # Do nothing 68 | esac 69 | 70 | ## Notify if in dry_run mode 71 | if [ $dry_run -eq 1 ]; then 72 | echo "--- Begin dry run mode." 73 | fi 74 | 75 | ## For each file, directory, or glob 76 | for source in "$@"; do 77 | 78 | ## Verify source exists 79 | if [ ! -e "$source" ]; then 80 | echo "not found: $source" 81 | ## Skip to next loop iteration unless in dry run mode 82 | if [ $dry_run -eq 0 ]; then 83 | continue 84 | fi 85 | fi 86 | 87 | ## Initialize target 88 | target="$source" 89 | 90 | ## Optionally convert to lowercase 91 | if [ $ignore_case -eq 0 ]; then 92 | target=$(echo "$target" | tr A-Z a-z ) 93 | fi 94 | 95 | ## Optionally convert existing underscores to spaces 96 | if [ $underscores_to_spaces -eq 1 ]; then 97 | target=$(echo "$target" | tr _ ' ') 98 | fi 99 | 100 | ## Optionally convert existing dashes to spaces 101 | if [ $dashes_to_spaces -eq 1 ]; then 102 | target=$(echo "$target" | tr - ' ') 103 | fi 104 | 105 | ## Optionaly consolidate spaces 106 | if [ $consolidate_spaces -eq 1 ]; then 107 | target=$(echo "$target" | tr -s ' ') 108 | fi 109 | 110 | ## Optionally remove spaces immediately adjacent to dashes 111 | if [ $dashes_omit_adjacent_spaces -eq 1 ]; then 112 | target=$(echo "$target" | sed 's/\- /-/') 113 | target=$(echo "$target" | sed 's/ \-/-/') 114 | fi 115 | 116 | ## Replace spaces with underscores or dashes 117 | target=$(echo "$target" | tr ' ' "$space_replace_char") 118 | 119 | ## Handle moving the source to target 120 | if [ "$target" == "$source" ]; then 121 | ## If filename hasn't changed, skip move 122 | ## Print if verbose is true 123 | if [ $verbose -eq 1 ]; then 124 | echo "ignore: $source" 125 | fi 126 | elif [ -e "$target" -a $case_sensitive_filesystem -eq 1 ]; then 127 | ## If conflicts with existing filename, skip move and complain 128 | echo "conflict: $source" 129 | else 130 | ## If move is legal 131 | ## Skip move if dry_run is true 132 | if [ $dry_run -eq 0 ]; then 133 | mv "$source" "$target" 134 | fi 135 | ## Print if verbose or dry_run is true 136 | if [ $verbose -eq 1 -o $dry_run -eq 1 ]; then 137 | echo "rename: $source -> $target" 138 | fi 139 | fi 140 | 141 | done 142 | 143 | ## Notify if in dry_run mode 144 | if [ $dry_run -eq 1 ]; then 145 | echo "--- End dry run mode." 146 | fi 147 | -------------------------------------------------------------------------------- /slugify.1: -------------------------------------------------------------------------------- 1 | .\" generated with Ronn/v0.7.3 2 | .\" http://github.com/rtomayko/ronn/tree/0.7.3 3 | . 4 | .TH "SLUGIFY" "1" "January 2016" "" "" 5 | . 6 | .SH "NAME" 7 | \fBslugify\fR \- convert filenames and directories to a web friendly format 8 | . 9 | .SH "SYNOPSIS" 10 | \fBslugify\fR [\-acdhintuv] \fIsource_file\fR \.\.\. 11 | . 12 | .SH "DESCRIPTION" 13 | \fBSlugify\fR converts filenames and directories to a web friendly format\. Before running any command, consider a dry run \fB\-n\fR before hand\. 14 | . 15 | .P 16 | Options include: 17 | . 18 | .TP 19 | \fB\-a\fR 20 | Remove spaces immediately adjacent to dashes\. 21 | . 22 | .TP 23 | \fB\-c\fR 24 | Consolidate consecutive spaces into single space\. 25 | . 26 | .TP 27 | \fB\-d\fR 28 | Replace spaces with dashes (instead of default underscores)\. 29 | . 30 | .TP 31 | \fB\-h\fR 32 | Display help\. 33 | . 34 | .TP 35 | \fB\-i\fR 36 | Ignore case\. 37 | . 38 | .TP 39 | \fB\-n\fR 40 | Dry run\. 41 | . 42 | .TP 43 | \fB\-t\fR 44 | Treat existing dashes as spaces\. 45 | . 46 | .TP 47 | \fB\-u\fR 48 | Treat existing underscores as spaces (useful with \fB\-a\fR, \fB\-c\fR, or \fB\-d\fR)\. 49 | . 50 | .TP 51 | \fB\-v\fR 52 | Verbose mode\. 53 | . 54 | .SH "EXAMPLES" 55 | Note, most examples below are run in verbose mode (\fB\-v\fR) to help illustrate the results\. Verbose mode is unnecessary in real world scenarios\. 56 | . 57 | .P 58 | \fBProvide escaped filenames:\fR 59 | . 60 | .IP "" 4 61 | . 62 | .nf 63 | 64 | $ slugify \-v My\e \e file\.txt 65 | rename: My file\.txt \-> my__file\.txt 66 | . 67 | .fi 68 | . 69 | .IP "" 0 70 | . 71 | .P 72 | \fBAlternatively provide unescaped filenames inside quotes:\fR 73 | . 74 | .IP "" 4 75 | . 76 | .nf 77 | 78 | $ slugify \-v "My file\.txt" 79 | rename: My file\.txt \-> my__file\.txt 80 | . 81 | .fi 82 | . 83 | .IP "" 0 84 | . 85 | .P 86 | \fBGlobs (like * and ?) work as well:\fR 87 | . 88 | .IP "" 4 89 | . 90 | .nf 91 | 92 | $ slugify \-v *\.txt 93 | rename: My file\.txt \-> my_file\.txt 94 | ignore: my_web_friendly_filename\.txt 95 | . 96 | .fi 97 | . 98 | .IP "" 0 99 | . 100 | .P 101 | \fBProvide an unlimited number of arguments:\fR 102 | . 103 | .IP "" 4 104 | . 105 | .nf 106 | 107 | $ slugify \-v "My first file\.txt" "My second file\.txt" 108 | rename: My first file\.txt \-> my_first_file\.txt 109 | rename: My second file\.txt \-> my_second_file\.txt 110 | . 111 | .fi 112 | . 113 | .IP "" 0 114 | . 115 | .P 116 | \fBDirectories are also supported:\fR 117 | . 118 | .IP "" 4 119 | . 120 | .nf 121 | 122 | $ slugify \-v "My Directory" 123 | rename: My Directory \-> my_directory 124 | . 125 | .fi 126 | . 127 | .IP "" 0 128 | . 129 | .P 130 | \fBConsolidate consecutive spaces into single spaces:\fR 131 | . 132 | .IP "" 4 133 | . 134 | .nf 135 | 136 | $ slugify \-vc "My consolidated file\.txt" 137 | rename: My consolidated file\.txt \-> my_consolidated_file\.txt 138 | . 139 | .fi 140 | . 141 | .IP "" 0 142 | . 143 | .P 144 | \fBReplace spaces with dashes:\fR 145 | . 146 | .IP "" 4 147 | . 148 | .nf 149 | 150 | $ slugify \-vd "My dashed file\.txt" 151 | rename: My dashed file\.txt \-> my\-dashed\-file\.txt 152 | . 153 | .fi 154 | . 155 | .IP "" 0 156 | . 157 | .P 158 | The \-d option replaces each space with a dash\. 159 | . 160 | .IP "" 4 161 | . 162 | .nf 163 | 164 | $ slugify \-vd "My dashed file\.txt" 165 | rename: My dashed file\.txt \-> my\-\-dashed\-\-file\.txt 166 | . 167 | .fi 168 | . 169 | .IP "" 0 170 | . 171 | .P 172 | Combine \fB\-d\fR with \fB\-c\fR (consolidate spaces) for a single dash between each word\. 173 | . 174 | .IP "" 4 175 | . 176 | .nf 177 | 178 | $ slugify \-vdc "My dashed file\.txt" 179 | rename: My dashed file\.txt \-> my\-dashed\-file\.txt 180 | . 181 | .fi 182 | . 183 | .IP "" 0 184 | . 185 | .P 186 | \fBIgnore case:\fR 187 | . 188 | .IP "" 4 189 | . 190 | .nf 191 | 192 | $ slugify \-vi "UPPER CASE FILE\.txt" 193 | rename: UPPER CASE FILE\.txt \-> UPPER_CASE_FILE\.txt 194 | . 195 | .fi 196 | . 197 | .IP "" 0 198 | . 199 | .P 200 | \fBPlay it safe with a dry run:\fR 201 | . 202 | .P 203 | Dry run mode does not alter the filesystem in any way\. 204 | . 205 | .IP "" 4 206 | . 207 | .nf 208 | 209 | $ slugify \-n * 210 | \-\-\- Begin dry run mode\. 211 | rename: My file\.txt \-> my_file\.txt 212 | ignore: web_friendly_filename\.txt 213 | \-\-\- End dry run mode\. 214 | . 215 | .fi 216 | . 217 | .IP "" 0 218 | . 219 | .P 220 | Dry run mode also allows you to test filenames that don\'t exist\. Great for testing! 221 | . 222 | .IP "" 4 223 | . 224 | .nf 225 | 226 | $ slugify \-n "Ghost File\.txt" 227 | \-\-\- Begin dry run mode\. 228 | not found: Ghost File\.txt 229 | rename: Ghost File\.txt \-> ghost_file\.txt 230 | \-\-\- End dry run mode\. 231 | . 232 | .fi 233 | . 234 | .IP "" 0 235 | . 236 | .P 237 | Dry run mode automatically enables verbose mode so there is no need to include the \fB\-v\fR option with \fB\-n\fR\. 238 | . 239 | .P 240 | \fBHandle spaces adjacent to dashes:\fR 241 | . 242 | .P 243 | In this example, without \fB\-a\fR the dashes end up surrounded by underscores\. 244 | . 245 | .IP "" 4 246 | . 247 | .nf 248 | 249 | $ slugify \-v "The Beatles \- Yellow Submarine\.mp3" 250 | rename: The Beatles \- Yellow Submarine\.mp3 \-> the_beatles_\-_yellow_submarine\.mp3 251 | . 252 | .fi 253 | . 254 | .IP "" 0 255 | . 256 | .P 257 | But with \fB\-a\fR the adjacent spaces are removed\. 258 | . 259 | .IP "" 4 260 | . 261 | .nf 262 | 263 | $ slugify \-va "The Beatles \- Yellow Submarine\.mp3" 264 | rename: The Beatles \- Yellow Submarine\.mp3 \-> the_beatles\-yellow_submarine\.mp3 265 | . 266 | .fi 267 | . 268 | .IP "" 0 269 | . 270 | .P 271 | The \fB\-a\fR 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 \fB\-a\fR)\. 272 | . 273 | .IP "" 4 274 | . 275 | .nf 276 | 277 | $ slugify \-va "The Beatles \- Yellow Submarine\.mp3" 278 | rename: The Beatles \- Yellow Submarine\.mp3 \-> the_beatles__\-__yellow_submarine\.mp3 279 | . 280 | .fi 281 | . 282 | .IP "" 0 283 | . 284 | .P 285 | But \fB\-c\fR consolidates spaces into a single space and then \-a will remove the left over adjacent single spaces\. 286 | . 287 | .IP "" 4 288 | . 289 | .nf 290 | 291 | $ slugify \-vac "The Beatles \- Yellow Submarine\.mp3" 292 | rename: The Beatles \- Yellow Submarine\.mp3 \-> the_beatles\-yellow_submarine\.mp3 293 | . 294 | .fi 295 | . 296 | .IP "" 0 297 | . 298 | .P 299 | \fBConvert existing underscores into dashes\fR 300 | . 301 | .P 302 | The \fB\-u\fR treats underscores as spaces and \fB\-d\fR converts spaces into dashes\. 303 | . 304 | .IP "" 4 305 | . 306 | .nf 307 | 308 | $ slugify \-vud "Spaces Dashes\-And_Underscores\.txt" 309 | rename: Spaces Dashes\-And_Underscores\.txt \-> spaces\-dashes\-and\-underscores\.txt 310 | . 311 | .fi 312 | . 313 | .IP "" 0 314 | . 315 | .P 316 | \fBConvert existing dashes into underscores\fR 317 | . 318 | .IP "" 4 319 | . 320 | .nf 321 | 322 | $ slugify \-vt "Spaces Dashes\-And_Underscores\.txt" 323 | rename: Spaces Dashes\-And_Underscores\.txt \-> spaces_dashes_and_underscores\.txt 324 | . 325 | .fi 326 | . 327 | .IP "" 0 328 | . 329 | .SH "BUGS" 330 | \fIhttps://github\.com/benlinton/slugify/issues\fR 331 | . 332 | .SH "COPYRIGHT" 333 | Slugify is Copyright (C) 2012 Ben Linton \fIhttps://github\.com/benlinton/slugify\fR 334 | . 335 | .SH "SEE ALSO" 336 | man(1), manpages(5) 337 | -------------------------------------------------------------------------------- /slugify.1.ronn: -------------------------------------------------------------------------------- 1 | slugify(1) -- convert filenames and directories to a web friendly format 2 | ======================================================================== 3 | 4 | ## SYNOPSIS 5 | 6 | `slugify` [-acdhintuv] ... 7 | 8 | ## DESCRIPTION 9 | 10 | **Slugify** converts filenames and directories to a web friendly format. 11 | Before running any destructive commands, consider a dry run `-n` before hand. 12 | 13 | For additional details, please see the repository: 14 | 15 | 16 | Options include: 17 | 18 | * `-a`: 19 | Remove spaces immediately adjacent to dashes. 20 | 21 | * `-c`: 22 | Consolidate consecutive spaces into single space. 23 | 24 | * `-d`: 25 | Replace spaces with dashes (instead of default underscores). 26 | 27 | * `-h`: 28 | Display help. 29 | 30 | * `-i`: 31 | Ignore case. 32 | 33 | * `-n`: 34 | Dry run. 35 | 36 | * `-t`: 37 | Treat existing dashes as spaces. 38 | 39 | * `-u`: 40 | Treat existing underscores as spaces (useful with `-a`, `-c`, or `-d`). 41 | 42 | * `-v`: 43 | Verbose mode. 44 | 45 | ## EXAMPLES 46 | 47 | Note, most examples below are run in verbose mode (`-v`) to help illustrate the 48 | results. Verbose mode is unnecessary in real world scenarios. 49 | 50 | __Provide escaped filenames:__ 51 | 52 | $ slugify -v My\ \ file.txt 53 | rename: My file.txt -> my__file.txt 54 | 55 | __Alternatively provide unescaped filenames inside quotes:__ 56 | 57 | $ slugify -v "My file.txt" 58 | rename: My file.txt -> my__file.txt 59 | 60 | __Globs (like * and ?) work as well:__ 61 | 62 | $ slugify -v *.txt 63 | rename: My file.txt -> my_file.txt 64 | ignore: my_web_friendly_filename.txt 65 | 66 | __Provide an unlimited number of arguments:__ 67 | 68 | $ slugify -v "My first file.txt" "My second file.txt" 69 | rename: My first file.txt -> my_first_file.txt 70 | rename: My second file.txt -> my_second_file.txt 71 | 72 | __Directories are also supported:__ 73 | 74 | $ slugify -v "My Directory" 75 | rename: My Directory -> my_directory 76 | 77 | __Consolidate consecutive spaces into single spaces:__ 78 | 79 | $ slugify -vc "My consolidated file.txt" 80 | rename: My consolidated file.txt -> my_consolidated_file.txt 81 | 82 | __Replace spaces with dashes:__ 83 | 84 | $ slugify -vd "My dashed file.txt" 85 | rename: My dashed file.txt -> my-dashed-file.txt 86 | 87 | The -d option replaces each space with a dash. 88 | 89 | $ slugify -vd "My dashed file.txt" 90 | rename: My dashed file.txt -> my--dashed--file.txt 91 | 92 | Combine `-d` with `-c` (consolidate spaces) for a single dash between each word. 93 | 94 | $ slugify -vdc "My dashed file.txt" 95 | rename: My dashed file.txt -> my-dashed-file.txt 96 | 97 | __Ignore case:__ 98 | 99 | $ slugify -vi "UPPER CASE FILE.txt" 100 | rename: UPPER CASE FILE.txt -> UPPER_CASE_FILE.txt 101 | 102 | __Play it safe with a dry run:__ 103 | 104 | Dry run mode does not alter the filesystem in any way. 105 | 106 | $ slugify -n * 107 | --- Begin dry run mode. 108 | rename: My file.txt -> my_file.txt 109 | ignore: web_friendly_filename.txt 110 | --- End dry run mode. 111 | 112 | Dry run mode also allows you to test filenames that don't exist. Great for 113 | testing! 114 | 115 | $ slugify -n "Ghost File.txt" 116 | --- Begin dry run mode. 117 | not found: Ghost File.txt 118 | rename: Ghost File.txt -> ghost_file.txt 119 | --- End dry run mode. 120 | 121 | Dry run mode automatically enables verbose mode so there is no need to include 122 | the `-v` option with `-n`. 123 | 124 | __Handle spaces adjacent to dashes:__ 125 | 126 | In this example, without `-a` the dashes end up surrounded by underscores. 127 | 128 | $ slugify -v "The Beatles - Yellow Submarine.mp3" 129 | rename: The Beatles - Yellow Submarine.mp3 -> the_beatles_-_yellow_submarine.mp3 130 | 131 | But with `-a` the adjacent spaces are removed. 132 | 133 | $ slugify -va "The Beatles - Yellow Submarine.mp3" 134 | rename: The Beatles - Yellow Submarine.mp3 -> the_beatles-yellow_submarine.mp3 135 | 136 | The `-a` only removes spaces immediately adjacent to a dash, which may not be 137 | the desired effect (below three spaces on either side of the dash get converted 138 | into two underscores because of `-a`). 139 | 140 | $ slugify -va "The Beatles - Yellow Submarine.mp3" 141 | rename: The Beatles - Yellow Submarine.mp3 -> the_beatles__-__yellow_submarine.mp3 142 | 143 | But `-c` consolidates spaces into a single space and then -a will remove the 144 | left over adjacent single spaces. 145 | 146 | $ slugify -vac "The Beatles - Yellow Submarine.mp3" 147 | rename: The Beatles - Yellow Submarine.mp3 -> the_beatles-yellow_submarine.mp3 148 | 149 | __Convert existing underscores into dashes__ 150 | 151 | The `-u` treats underscores as spaces and `-d` converts spaces into dashes. 152 | 153 | $ slugify -vud "Spaces Dashes-And_Underscores.txt" 154 | rename: Spaces Dashes-And_Underscores.txt -> spaces-dashes-and-underscores.txt 155 | 156 | __Convert existing dashes into underscores__ 157 | 158 | $ slugify -vt "Spaces Dashes-And_Underscores.txt" 159 | rename: Spaces Dashes-And_Underscores.txt -> spaces_dashes_and_underscores.txt 160 | 161 | ## BUGS 162 | 163 | 164 | 165 | ## COPYRIGHT 166 | 167 | Slugify is Copyright (C) 2012 Ben Linton 168 | 169 | 170 | ## SEE ALSO 171 | 172 | man(1), manpages(5) 173 | --------------------------------------------------------------------------------