![]() |
Share you Bash Command Line Tips & Tricks
I've been a linux user for a fair amount of time now, but it's just now that I'm getting really used to the command line interface.
I've picked up a few cool tidbits here and there, and I'm sure a lot of you have too, so let's hear 'em. ----- Here's a flexible sequntial download (think ezpics) Code:
for NUM in `seq -f %02g 00 30`; do URL="http://www.coasar.com/newgalleries/pbc/ahv/03/images/fullimage/0$moo.jpg"; wget $NUM 2>/dev/null && echo Downloaded $URL || echo " Failed $URL" ; done ----- This one I picked up on the net somewhere. I use firefox almost exclusively, so I set up aMSN to open up links with firefox. The major problem with that though is that if I'm already "surfin' the web" firefox will ask me to launch with a different profile. :( Here's a fix that will make it so that if firefox is already open it'll open the link in a new tab. Open up your favorite text editor *cough*VI*cough* and paste this: Code:
#!/bin/bash Now you can do stuff like this (i called mine firefox_newtab) I'm pretty sure that if you use mozilla you can just replace every instance of "firefox" with "mozilla". ----- This is likely common knowledge but I'm going to write in anyway, just incase someone finds it usefull. In my tips i used && (and) and || (or). If two commands are seperated by && then the the second will only be executed if the first one is sucessfull. For example: Code:
./configure && make If two commands are sperated by || (two pipes, shift + \ ) the second command will only execute if the first fails. E.g.:If you follow a command with >/dev/null it won't print it's standard output to the screen. Compares these: Code:
echo Beyond here lie dragons |
Probably not what you're looking for, but, some jokers made a pizza-ordering command line utility. You sign up for an account on Domino's website and the utility does everything else! I've never tried it, but it looks like it works.
http://www.beigerecords.com/cory/pizza_party/ |
a lil simple bash script that will create an individual gzip tar of each sub-dir in the dir that it executed in. each tar has the name of the dir that it has compressed.
Code:
find . -type d -mindepth 1 -maxdepth 1 -exec tar zcf {}.tar.gz {} \; |
Search command history
Ctrl+R will search backwards through your previous commands, just press Ctrl+R, then start typing part of the command you want to recall (any substring, it doesn't have to be from the beginning).
Ctrl+A will go to the start of the line. Ctrl+E will go to the end Ctrl+U will nuke from the cursor to the start of the line Ctrl+W will nuke the word to the left of the cursor |
Here are some simple things that are really invaluable - I use them several times per day :)
'cd -' will go 'back' a directory (eg. say you were in /usr/src/linux, then went to ~/kernelconfigs ... you can quickly get back to /usr/src/linux by typing cd -) when you're typing in a file name, you can enter the first few letters and then <TAB>. It'll either automatically be completed for you or it'll show you a list of potential matches. and if you want to perform batch operations on files in a directory, you can try this: Code:
for i in *; do operation_1_on $i; operation_2_on $i; done Code:
for i in *; do mpg123 -w "${i/.mp3}.wav" "$i"; toolame -v 5 "${i/.mp3}.wav" "${i/.mp3}.mp2"; done |
set permissions on files only:
Code:
find . -type f -exec chmod 0644 {} \; Code:
find . -type d -exec chmod 0755 {} \; recursively set permissions on something depending on if it's a file or a directory (note: chmod -R 0755 would make EVERYTHING (files AND directories) 755.. I don't want that). Of course that can be changed to chown or pretty much anything you want hehe.. I have some other scripts, but they're only relevant for work.. |
One I use at work often is to kill multiple processes at once.
Code:
kill `ps -eaf | grep ^username | cut -c10-14` |
Quote:
Code:
pkill -u username By default, pkill kills by process name. |
Type
! followed by the beginning of a command you've already used...and hit enter. it will auto exec that command in full...you ran last. obvoiusly be careful with somethings. i also presume everyone knows ~userid prefers to userid's home directory. also if on the command line and have vi editor mode activated ( bash -o vi ( works with ksh too ) ) then go for a ESC V and that will bring the command line straight into a full vi session |
Quote:
|
Dont know... I just wrote a couple of shell scripts to backup stuff (one backs up MySQL, another backs up my website, another backs up stuff edited in the past day/week/month) and FTP them to backup. Kinda long to be posting on here though.
|
here's a few random ones i have:
Create your own short user name: Code:
#!/usr/bin/env python Code:
#!/bin/bash Code:
for dirs in `ls /` |
You can use PHP on the CLI which I love. I use that now as a way to list usage on my game hosting server so I know where I am on my usage. I've also set it up so that it'll kill certain processes or start them up.
Those are for linux I've made scripting tips in Windows(I know, not bash, but still useful batch files that I wanna brag about) that'll list all installed applications on a computer and another one that can set the ip address. If anyone would like any of them, let me know and I'll post them. |
I have written some serious one-liner doozies in the past...usually when trying to do something somewhat complicated with a bunch of files. For example, one time I did a one-liner that recursively went through a directory tree and renamed files with certain names to match the directory name, or something like that.
Other one-liners I've used for doing recursive media conversion (WMA->MP3 for example), and of course for downloading Pr0n from various places with nice sequential filenames. Probably my most frequently used BASH construct is the "while read" one: Code:
cat /etc/passwd | awk -F: '{ print $1 }' | while read line; do echo $line; done; |
Kind of like the cd - trick above, but pushd and popd are invaluable. You can create a whole stack of directories to switch from. The command dirs will give you a list of whats on the current stack. Very useful if you keep going back and forth, just pushd dir then popd and then to keep swapping !push and !pop to get back.
Bash is great. |
All times are GMT -8. The time now is 11:02 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project