Ben Emson

18 Useful bash scripts for web developers

Using bash scripts to become a more efficient web developer

Here are a few scripts, that I find really useful for speeding up my web development time.

I've been building up this list as I needed to use them - so they maybe a little raw.

For example often clients send me images with filenames that don't match my naming standard, so running the appropriate script really helps keep me focussed on the job in hand and not waste too much time reformatting filenames etc.

Finally if you have any useful little bash scripts why don't you add them to the comments below?

Bash script to append a .txt extension to a filename

Iterate over the current directory, get all files with .log and append .txt to the end of the entire filename: Liquid error: No such file or directory - posix_spawnp

Script to make filenames lowercase

Converts all the file names in a directory and converts them to lowercase.

echo version: Liquid error: No such file or directory - posix_spawnp

real version: Liquid error: No such file or directory - posix_spawnp

Quick note on how to use grep to search for a string in file

-o print Only the matched parts of a matching line, with each part on a separate line. -H print the filename for each match. -n prefix each line of output with the 1 based line number. -R, -r recursively read all files under subdirectories. Liquid error: No such file or directory - posix_spawnp

{txt,log} this globs the file extensions together. 2>/dev/null this passes all errors to a blackhole, so that they won't be displayed. Liquid error: No such file or directory - posix_spawnp

Find all occurrences of 123741 in all .txt and .log files in all subdirectories. Liquid error: No such file or directory - posix_spawnp

List all files that match this regular expression in the current direcory

All files with the number 5 in their name. Liquid error: No such file or directory - posix_spawnp

Bash script to check whether a tag exists in a subversion repository

Liquid error: No such file or directory - posix_spawnp

The [ ] is known as a Test, by default it tests integers Also to end an if statement you use fi Liquid error: No such file or directory - posix_spawnp

Note need the / in the evaluation part of "it-0.9.0.7/", don't really need it in grep it-0.9.0.7/ Liquid error: No such file or directory - posix_spawnp

Quick note on how to increment a variable, in a bash script

Liquid error: No such file or directory - posix_spawnp

incrementing in a loop Liquid error: No such file or directory - posix_spawnp

Using a bash for loop to list all text files and append whoopee

Liquid error: No such file or directory - posix_spawnp

Change all filenames to be a specific incremented name, starting from file 16

Liquid error: No such file or directory - posix_spawnp

Script to loop through all .txt filenames and make them lower case

Liquid error: No such file or directory - posix_spawnp

Get all JPG files and create the appropriate HTML list tags for them and add them to a file

Create an HTML version: Liquid error: No such file or directory - posix_spawnp

Create a HAML version: Liquid error: No such file or directory - posix_spawnp

Batch change a misspelled filename of certain files, using string replacement

Liquid error: No such file or directory - posix_spawnp

Script to batch create files based on filenames in a file

Create a file called something like "my_files.txt" with the following content:

archive_error_112480_0040.txt archive_error_114390_0043.txt etc...

Commands: Liquid error: No such file or directory - posix_spawnp

Using bash Expansions, and allow escapes

-e is an undocumented flag that allows escapes Expansion ${command/parameter/substitution} everything else needs to be escaped. Liquid error: No such file or directory - posix_spawnp

Executing a .bash_script after making changes

Liquid error: No such file or directory - posix_spawnp

NB: you can 'include' other bash .sh scripts in a .bash_profile file by using 'source' or '.'

Quick tip on using curl for downloading files off the web

Use -O to name the remote file, curl will save it locally but remove the rest of the path. Liquid error: No such file or directory - posix_spawnp

Bash command for testing the existence of files and directories

If a directory does NOT exist create it Liquid error: No such file or directory - posix_spawnp

If a file exists echo a message: Liquid error: No such file or directory - posix_spawnp

Bash command to remove spaces from filenames

Liquid error: No such file or directory - posix_spawnp

replace spaces for underscores Liquid error: No such file or directory - posix_spawnp

Vim regex to replace spaces in images file names of ah HTML page Liquid error: No such file or directory - posix_spawnp

Copy text to the clipboard

Liquid error: No such file or directory - posix_spawnp

Conclusion

Some of these are quite basic but useful none-the-less, please add your own to the comments below. Many thanks, and good luck.

Updated...

Script function for .bash_profile to cd to the last opened finder location

This is a script written by a colleague. James Power. Add the following function to your .bash_profile in Apple OSX. Now open a directory in Finder, then open terminal and type cdf, your terminal will now change directory to the same one as the last Finder opened.

Liquid error: No such file or directory - posix_spawnp

Also for a much more comprehensive list of commands check out: http://www.commandlinefu.com/commands/tagged/34/bash

Ruby Rush command-line tool

If you use Ruby maybe check out Rush, it allows you do use Ruby in a bash command-line like way.

blog comments powered by Disqus