5 ms·
Useful command-line scripts for web developers (or anyone else really)
- antirez 17y agoSome time ago I wrote this little script in order to track, among different virtual hosts, what is consuming too much CPU: #!/bin/sh cd /proc ps auxw | grep apache2 | grep 'Rl' | sed -e 's/ \+/ /g' | \ cut -d' ' -f2 | egrep '[0-9]+' | xargs ls -l | grep cwd Basically this will print a line for every instance of every apache virtual host that is 'running' right now. It's Linux specific.
- emson 17y agoVery good, that looks really interesting.
- sunkencity 17y agoI find it very much easier to just enter irb or run ruby with -e and hack around with backquoted strings instead of using bash to loop through and manipulate filenames. a silly usecase would be reversing all the file names: ruby -e '`ls -1`.each_line { |x| `mv #{x.chomp} #{x.chomp.reverse}` }' pbcopy is pretty cool feature though.
- emson 17y agoYes I like the idea of using Ruby I find it a little more intuitive. Thanks. Also you may want to look at commandlinefu.com: http://www.commandlinefu.com/commands/tagged/34/bash http://www.commandlinefu.com/commands/tagged/34/bash
- Xichekolas 17y agoAlso fun as far as ruby on the command line goes: Rush http://rush.heroku.com/ http://rush.heroku.com/
- jsonscripter 17y agoIs there any way to give grep some context? It's great knowing what file a word came from, but I've never figured out how to get grep to spit out the leading and following lines too.
- rntz 17y agofrom `man grep`: -A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given. -B NUM, --before-context=NUM Print NUM lines of leading context before matching lines. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given. -C NUM, -NUM, --context=NUM Print NUM lines of output context. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
- akirk 17y agoNot that obvious (even from this description): you can simply do a grep -3 to get three lines of context, etc.
- mahmud 17y agoawk and perl will let you do magic, my friend. Advanced "grep" features are only in GNU grep.
- mileszs 17y agoI use a function that looks like this: find $1 -name $2 | xargs egrep -nC3 $3 | less So, say: find ./ -name "*.rb" | xargs egrep -nC3 'gsub' | less You'll get the matching line, three lines before and three lines after, as well as file name and line numbers.
- emson 17y agoI also use this natty little function in my .bash_profile to cd to the last opened finder location. This is a script written by a colleague. http://www.visualcortex.co.uk/ http://www.visualcortex.co.uk/. 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. # function to change directory to the one set in the last opened finder. cdf () { currFolderPath=$( /usr/bin/osascript <<" EOT" tell application "Finder" try set currFolder to (folder of the front window as alias) on error set currFolder to (path to desktop folder as alias) end try POSIX path of currFolder end tell EOT ) echo "cd to \"$currFolderPath\"" cd "$currFolderPath" }
- yan 17y agoConversely, running "open dir/" will open the dir in Finder. I tend to use that a lot.
- nileshk 17y agoI've been using OpenTerminal for this purpose: http://www.macupdate.com/info.php/id/23173 http://www.macupdate.com/info.php/id/23173 It gives you an icon in Finder that changes the directory in your terminal to the one in the Finder window. There are some options, like have it open a new terminal, or change the folder using pushd, so you can popd once your done and return to the folder it originally was in. I like the script you provided, though, as it saves me from having to use the mouse (no button click) and I can easily select which terminal to do it in. Note that I had to remove the quotes around the first EOT to get it to work.
- surki 17y agoI often use this to watch youtube videos mplayer `clive -e http://www.youtube.com/watch?v=SZRyry_B7TY http://www.youtube.com/watch?v=SZRyry_B7TY | awk -F'","' '{print $2}'` You would need 'clive' package.
- emson 17y agoCool I like it.
- DannoHung 17y agoDon't use grep. Use ack.
- deleted 17y ago[deleted]
- aaronblohowiak 17y agoThis comment is not nearly as useful as one that gives examples about your reasoning (or at least a link to such.)
- DannoHung 17y agoSorry, I thought people would be intrigued and google it. http://betterthangrep.com/ http://betterthangrep.com/ A quote: "ack is a tool like grep, aimed at programmers with large trees of heterogeneous source code. ack is written purely in Perl, and takes advantage of the power of Perl's regular expressions."
- blogimus 17y agoI frequently need to create datestamped archives. Here is a one-line example, given two parameters. $1 is the archive name prefix and $2 is the directory to archive. tar -cvzf ${1}-$(date +%Y%m%d-%H%M).tar.gz $2 Note, if $2 contains path information (such as absolute path), that path will be stored in the tar. So it is usually used with the working dir as the parent of $2.
- silentbicycle 17y agoNothing against this post in particular, but it's much more useful in the long term to learn how to write stuff like this yourself. Compare knowing how to cook with just having lots of recipes. While there are certainly some people who will pick apart examples like these and learn new tricks, I've seen enough copy-and-paste programming to know that many won't.
- nuclear_eclipse 17y agoNot to mention that many of the examples given are very inefficient ways to do what's needed. Specifically, looking for files with "5" in the name: $ ls | grep -e .*5.* Would be better as: $ find . -name '*5*' or if you insist on using a regex: $ find . -regex '.*5.*'
- deleted 17y ago[deleted]
- greyboy 17y agoAm I missing something? What's wrong with: $ ls *5* ?? The find command is more powerful (descending into subdirs, etc.), but the original example in the link only specified the current directory.
- nuclear_eclipse 17y agoSorry, I got so caught up the frustration that I forget to post that as my real frustration. :P That was exactly what I first thought when I saw that, and was posting the `find` calls as just a easier / more comprehensive method of looking, esp when you start wanting recursive searches. The fact that you can add tons of different search filters to `find` that you can't have with `ls` really make it the goto way of finding files, rather than piping ls to some form of grep or whatnot.
- philh 17y ago$ ls -d *5* Otherwise you get a listing of any directory with a 5 in the name.
- uggedal 17y ago> Bash script to append a .txt extension to a filename Why?
- silentbicycle 17y agoAnd regardless of whether it's necessary in the first place, why is it a bash script rather than a sh script? Very few of these use bash-specific extensions, and the BSDs don't necessarily have bash installed. Things like that make porting from Linux to BSD and other Unices tons of fun.
- mitechka 17y agoA more general case would be: How to change a pattern in a bunch of file names from X to Y: for i in *X*; do mv $i ${i/X/Y}; done
- ori_b 17y agoOr just use 'rename': $ rename s/foo$/bar/ *foo
- dryicerx 17y agohttp://www.commandlinefu.com http://www.commandlinefu.com CommandLineFu is a much more generalized command-line collection, has a few clever ones. Not just web developers, but anyone who uses *nix in general. Specially the top rated ones http://www.commandlinefu.com/commands/browse/sort-by-votes http://www.commandlinefu.com/commands/browse/sort-by-votes
- spudlyo 17y agoI sometimes in interviews I ask how to find the top 10 IP addresses that have hit a web host in the last thousand hits. tail -1000 access.log | cut -d' ' -f1 | sort | uniq -c | sort -rn | head
- mjgoins 17y agoJust a little warning: The "for i in..." paradigm breaks if the strings you're iterating over have spaces in them. The way around that is to either use find and/or xargs instead of "for i in", or to first set the IFS variable to a value that doesn't contain spaces.