bash, bat, whatever...
What is your favourite command line hyperproductivity trick?
bash, bat, whatever...
What is your favourite command line hyperproductivity trick?
Running grep
over directories with code, often with find
(and sometimes xargs
when needed). For me this is typically faster than using the equivalent tools in an IDE, although I guess that mostly shows my age.
I like my bash prompt to be a different colour. In your .bashrc or .profile:
PS1='\033[01;34m\n[\u@\H \W]$\033[00m '
Entering
START .
to open a Windows Explorer window for the current directory.
Pipes in general. Imagine you want to find out how many files that have a particular string in their filename are in a folder. You could do it like this:
ls -l | grep yourstring | wc -l
# Sample output: 52
Favourite command line hyperproductivity trick:
I like the Tab key for path/file completion in bash.
Favourite command line tool:
man [command]
Piping the output of grep (or findstr) to itself to exclude things I don't want. For example:
findstr /spin /c:"foo" *.cc | findstr /v /c:".svn"
Turning on tab completion in a Windows command shell.
set \HKCU\Software\Microsoft\Command Processor\CompletionChar to 9
After that use tab to auto-complete filenames in shells.
I think Vista has this turned on by default but XP doesn't
At the Mac OS X bash prompt issuing
open Filename
starts up the application associated with Filename
and loads the file.
In tcsh, using CTRL-X+?
to expand paths and aliases, CTRL-X+$
to expand variables and CTRL-X+*
to expand globs.
Some examples:
> alias l less
> l
# press CTRL-X+?
> less
> set l less
> $l
# press CTRL-X+$
> less
> ls
a.c b.c c.h
> echo *.c
# press CTRL-X+*
> echo a.c b.c
In bash:
set -o vi
turns on vi mode. Also, adding this to ~/.inputrc will turn on vi mode for anything using readline:
set editing-mode vi
set keymap vi
I am more familiar with the vi commands than I am with the emacs ones, so these changes give me a productivity boost.
Insert preceding line's final parameter (bash)
Alt-. is one of the most useful key combinations; for some reason no one knows about this one. Just try it and see
Press it again and again to select older previous parameters.
Great when you want to do something else to something you used just a moment ago, try it and see.
Update Example
Let's say you have the following files in a directory:
06.Jimmies_Joys-Wolverine_Blues.flac
06.King_Oliver-Jazzin_Babies_Blues.flac
07.Cotton_Pickers-You_Tell_Her,_I_stutter.flac
07.Mammie_Smith-Lady_Luck_Blues.flac
08.Paul_Whiteman-If_I_Cant_Have_The_Sweetie.flac
08.The_Georgians-Loose_Foot.flac
09.Jelly_Roll_Morton-Wolverine_Blues.flac
09.Wades_Moulin_Rouge_Orchestra-Mobile_Blues.flac
Sadly, you didn't notice that the CDDB entry for CD6 was mislabeled CD5, and so all the entries for CD5 and CD6 are now mixed. You take out the liner notes and want to move the true CD6 music up a level and into the appropriate subdirectory because OCD compels you so. Here's what you do:
Using Alt-. will auto-complete ../*CD6*
FTW.
When trying to find if a specific program is running: ps options | grep [p]rogram
That is, turning the program name into a non-self-matching regular expression.
Creating a m3u file from command:
dir /B *.mp3 > playlist.m3u
On Windows:
TaskList /M nameof.dll
Gives you a list of all the running processes that have the DLL loaded. This is useful if you're trying to track down a locking issue.
Here's a way to show any file in Windows Explorer (open window and highlight file; at least, if the file's directory window is not already open):
explorer /select,"c:\windows\notepad.exe"
Or, if you like "explore" (show file tree) better:
explorer /E,/select,"c:\windows\notepad.exe"
(I'm just taking notepad as an example, as that path probably exists on your PC.)
Note that Explorer will crash if the path does not exist.
bash-completion: the greatest things since bash completion
This gives you relevant completion suggestions for what you've currently typed. Extremely useful!
On Windows:
explorer /e,/root, path
Will launch an explorer instance with the root set to path. This is handy when you're browsing source code and just want an explorer view from the root of the code down.
NOTE: The strange commas are correct and there needs to be a space after the last comma and before the path.
I use alt-f/b in bash to jump over words along with ctrl-a/e to navigate to the end/beginning of the line. Ctrl-u deletes everything left from the cursor, ctrl-k everything right from the cursor.
On Windows you can use ctrl-left/right. Home/End, however I haven't found a shortcut to delete everything left/right from the cursor.
In Bash, the ! keyword thingummy.
e.g.
!ssh
runs the latest entry from your command line history that began with 'ssh'.
or
!224
reruns entry #224 from your history.
Useful when you have to run the same command several times, for example, running ssh with loads of command-line options specified.
From a Windows XP command prompt, might be a known feature but I use it all the time. hitting the F7 key will bring up the list of previously issued commands.
in bash, for loops. I particularly like the structure:
for f in *; do cd $f; for g in *; do [STUFF]; done; cd ..; done
in unix/sh, Using find in conjunction with while read in order to perform a common operation on certain files within a directory tree.
Change permissions of directories only:
find ./ -type d |while read x; do chmod go+rx "$x"; done
Remove files not accessed in more than a week:
find ./ -type f -atime +7| while readx; do rm "$x"; done
The find command allows one to ferret out those files with common characteristics, and the while read construct makes makes it simple to perform operations on each.
How to paste on windows console: alt+space+e+p
I know it's huge and complex but it saves me 50 times per day.
% perl -ne "print if /something/" < infile
Prints all of the lines with the regex something in them, from the file infile. "-e" tells perl to interpret the next argument as a script (&& run it), while "-n" tells perl to add a "while (<>) {}
" around the whole script, which is like saying 'execute the script for every line of input'.
Great for parsing log files.
To cut and paste arguments to a command
cmd `cat`
paste followed by ctrl-d
On Windows, when working with code, often I do heavy use of Command Window Here and then findstr
findstr /c:"string_to_be_searched" /s *.cpp
Is pretty useful.
Print Disk Volumes
$fsutil fsinfo drives
would result an output like this Drives: C:\ D:\ E:\ F:\ G:\ I:\ J:\ Piping to more would result the following output $fsutil fsinfo drives | more
Drives: C:\ D:\ E:\ F:\ G:\ I:\ J:\
using for to get volumes would result an output like thi
$for /F "skip=2" %i in ('fsutil fsinfo drives ^| more') do @echo %i
D:\ E:\ F:\ G:\ I:\ J:\
then you can get first volume as follows
$for /F "tokens=2" %i in ('fsutil fsinfo drives') do @echo %i
would result an output like this C:\
any enhancement for this would be cool
Command line calculator to enhance batch arithmetic operations specially divison math written in C#
**class Program
{
static void Syntax()
{
System.Console.Write("Example: calc 10 + 25 or calc 10 / 2 \n");
}
static void Main(string[] args)
{
try
{
if (args.Length == 3 && args.Length != 0 && Microsoft.VisualBasic.Information.IsNumeric(args.GetValue(0)) && Microsoft.VisualBasic.Information.IsNumeric(args.GetValue(2)))
{
//foreach (string var in args)
//{
string op = (string)args.GetValue(1);
double arg1 = System.Convert.ToDouble(args.GetValue(0));
double arg2 = System.Convert.ToDouble(args.GetValue(2));
switch (op)
{
case "+":
System.Console.Write(arg1 + arg2);
break;
case "/":
//double v = Convert.ToDouble(args.GetValue(2));
if (arg2 == 0 || arg2 == 0.0)
System.Console.Write("Cannot divide by zero!.");
else
System.Console.Write(arg1 / arg2);
break;
case "*":
System.Console.Write(arg1 * arg2);
break;
case "-":
System.Console.Write(arg1 - arg2);
break;
default:
Console.WriteLine("An attempt to an illegal operation! please check syntax.\n");
Syntax();
break;
}
//}
}
else
{
System.Console.Write("Missing argument or bad syntax .\n");
Syntax();
}
}
catch (Exception ex)
{
}
}
}**
then you can invoke command line compiler on it as follows %windir%\Microsoft.NET\Framework\v2.0.50727\csc.exe calc.cs
I have too much music I guess; so here is one thing I use that hasn't been listed:
locate -i artist | grep -i mp3$
substitute artist for song name etc. I include the "-i" options to make it case in-sensitive. I had to edit /etc/updatedb.conf to include my mounts in /media
Shell-fu is a place for storing, moderating and propagating command line tips and tricks. A bit like StackOverflow, but solely for shell. You'll find plenty of answers to this question there.
For bash, using !$ to repeat last argument to last command.
ls /var/log/messages
cp !$ !$.270809
And the magic carets, although I rarely use them nowadays:
[root@isis /] usradd -u 666 -g 10 -u guest
bash: usradd: command not found
[root@isis /] ^ra^era^
useradd -u 666 -g 10 -u guest
Last but not least, for infinite loops I use this (not a trick, just a quirk of mine :)
until [6 -eq 9]; echo "Jimi has really gotten into me."; done
In bash I sometimes misuse the fact that I know globbing a pattern will result in exactly two filenames. So if i have two textfiles stuff.txt
and stuff.txt.org
and I want to compare there contents I enter:
diff stuff.txt*
Copy to clipboard (in bat) :
MyCommand | clip
Actually, I just discovered it, but I'll surely use it every day now :)
Grep emacs from the output of ps aux, while filter out the grep emacs command itself
ps aux | grep [e]macs
In Bash, I like to:
for details on the alias definitions, see my blog post. It is easy stuff but very useful.
!!
repeat the last command. It is useful when you type, by example:
apt-get install
and forget the sudo, so:
sudo !!
do it for you.
There's an excellent website for bash command line, CommandLineFu.
My favourite under linux is:
rm -rf *
Seriously though, it has to be Ctrl+A and Ctrl E for editing on the linux command line, (Home/End) respectively. There is another one that I use when messing with bootsectors...
dd if=/dev/hda3 of=/bootsect.bin bs=512 cnt=1
As for Windows, it has to be Winkey + 'R', cmd enter. Much faster than clicking on Start, Run, cmd.
Hope this helps, Best regards, Tom.
One of my faves, in windows, is the tree command
tree <dir>
//e.g.
D:\axis2-1.2\dist>tree
Folder PATH listing
Volume serial number is 0000AC08 EC5E:85B4
D:.
+---axis2-web
¦ +---css
¦ +---Error
¦ +---images
¦ +---include
+---META-INF
+---WEB-INF
+---classes
+---conf
+---lib
+---modules
+---services
tree /f <dir> //lists all files too - very handy
I haven't found anything quite as nice in unix, although this comes close...
//can't remember who i got these ones from, but thanks anyway
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
or
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
Processing a file a line at a time.
cat filename | ( while read LINE ; do ... ; done )
You can also do tricks with IFS like
cat filename | ( while IFS=, read ONE TWO REST ; do ... ; done )