Are you command-line savvy?
The command line. That small place, where a lot can happen. And more so if you are a Linux user... How to maximize it? Where to harness its power?
I discovered commandlinefu.com a few years ago, while looking for a way to do... something. I don't even enter that often, although it is a brilliant place to discover how to do X in Linux/UNIX.
Among its all time greats I found some gems, and some others I discovered elsewhere, or even I made up.
Command line gems (and rocks?)
Run the last command as root: sudo !! as inapt-get install somethingThe double exclamation mark recovers the last command in the history.
-> error, you have to be root
sudo !!
Use the last argument for the last command: !$ as in
mkdir /path/to/somewhereChange to the previous directory: cd - as in (after the previous example to get back to where you started)
cd !$
cd -Backward history incremental search: C-r in the terminal prompt, start writing the command you want to backward-search. Press C-r to keep on searching for that term.
Correct typo in previous command: ^typo^corrected as in
sudo apt-get isntall somepackage
^isnt^inst
Write command in editor: C-x C-e will fire the editor in $EDITOR to write the following command.
Open the last modified file of a type: I use this to open the most recent ppm file in a directory with Eye Of Gnome: eog "$(ls -rt *.ppm | tail -n 1)"
Use locate to search for a specific PDF and open it with evince: evince "$(locate *partofname*.pdf)" This will only work if it results in only one instance. If you want to open only the first occurrence evince "$(locate *partofname*.pdf | head -n 1)"
The same with find: evince "$(find -name 'NameOfPdf.pdf')"
Mighty heads and tails: head -n N file, tail -n N file will return the first (resp. last) N lines of file.
Get Pid of a process by name: ps ax | grep "firefox"
Follow changes on a incremental file: tail -f filename as in following nohup.out from a running process.
Get all lines containing a string in a file: grep -e "string" file > outputfile Useful as intermediate step for plotting specific data lines from a nohup.out file. With -r, change the string for a regexp (consider buying this book if you will use them more than once, I recommend it: Mastering Regular Expressions
).
I think I have a few more, but I just don't remember them.
What are your special command line tricks? Are you a command-line jedi or more like a command line young one?
Related posts:
Control your computer by whistling (Linux&Mac)
My first port to the Ben NanoNote: gnugo
sed (stream editor) to colorise script output in Linux
Linux screenshots made easy: Shutter & Screenie
Gcal: the ultra-powerful command line GNU calendar
I think I have a few more, but I just don't remember them.
What are your special command line tricks? Are you a command-line jedi or more like a command line young one?
Related posts:



5 Awesome Comments. Add Yours!:
If you want get pid of process you can also write:
pgrep firefox
Thanks for posting these.. I'm a newbie to Linux and slowly moving up the hierarchy from white belt to black belt ;)
Sometimes I use $-modifiers. An example from zsh (my shell of choice, it's second best after tcsh) would be:
for PHOTO in `ls DSCF*.JPG`; do
convert $PHOTO -scale '50%' ${PHOTO:l}
done
This command scales pictures from a Cannon camera and turns file-names to down-case.
man who uses head -N 10 doesn't know about sed 10q
:)
Indeed, I didn't know that about sed. Just started to hack some sed /s expressions last week, to add color to my gcal script. Maybe I'll have to read all options :)
Post a Comment