9 ms·
Awesome but commonly unknown Linux Commands
- helwr 15y agorelated: http://www.quora.com/What-are-some-lesser-known-but-useful-Unix-commands http://www.quora.com/What-are-some-lesser-known-but-useful-U...
- helwr 15y agorelated: http://www.quora.com/What-are-some-lesser-known-but-useful-Unix-commands http://www.quora.com/What-are-some-lesser-known-but-useful-U... http://www.quora.com/Linux/What-are-some-time-saving-tips-that-every-Linux-user-should-know http://www.quora.com/Linux/What-are-some-time-saving-tips-th... http://www.quora.com/What-are-the-most-useful-swiss-army-knife-one-liners-on-Unix http://www.quora.com/What-are-the-most-useful-swiss-army-kni...
- Symmetry 15y agopstree prints out a tree of all the running processes, arranged by who spawned who.
- danieldk 15y agoOn Linux I very regularly use 'strace', to see what a program is currently doing (in terms of syscalls), what files were opened/read/written/closed, etc.
- thomasknowles 15y agostrace is the Daddy of debugging especially when you can attach it to an already existing program using it's PID. I have solved so many permissions errors problems with strace where the application itself just failed without logging anything.
- zobzu 15y agotry austrace. also gdb of course.
- nikcub 15y agoand on OS X you have DTrace (from Solaris), which can be called using dtruss, which is a shell script wrapper around DTrace also, in developer > applications there is an app called 'Instruments' which is an excellent GUI front-end to DTrace
- Meai 15y agoIf the application fails, wouldn't it just shut down and the process ends? Meaning that I can't call strace anymore? Do you have suggestions for that usecase? How would I know what the process id is going to be before I start the program?
- tedunangst 15y ago$ strace program args
- nz 15y agoSimilar experience here, except that I've solved similar issues (and far more complicated issues) with DTrace instead of strace. The range of information that DTrace can retrieve about a running application surpasses anything that strace can retrieve. DTrace can retrieve information on a systemic scale, whereas each strace instance operates on a single process. And the overhead is significantly lower with DTrace. For many of the systems applications I design for Illumos, I've used DTrace probes as a way of logging very frequent events, on demand (to avoid frequent IO). All of the events that _must_ be logged for the application to function properly, are logged and fsync'd. I think that in most systems dynamic tracing will eventually replace a significant portion of the logging functionality that people code into their applications. Either way, if you think strace is sweet, give DTrace a spin. Some helpful links: [0] A video demostration of DTrace, by the creator of DTrace. [0] http://www.youtube.com/watch?v=6chLw2aodYQ http://www.youtube.com/watch?v=6chLw2aodYQ [1] A post I wrote, demonstrating DTrace. Similar posts can be found on the blog's dtrace-addict page. [1] http://nickziv.wordpress.com/2011/04/08/adventures-of-a-dtrace-addict-part-0/ http://nickziv.wordpress.com/2011/04/08/adventures-of-a-dtra... [2] A wiki that contains DTrace examples for various languages and system facilities. Some examples may be Illumos-centric. [2] http://www.solarisinternals.com/wiki/index.php/DTrace_Topics http://www.solarisinternals.com/wiki/index.php/DTrace_Topics [3] DTrace's home. Contains blogs by the engineers behind DTrace. [3] http://www.dtrace.org http://www.dtrace.org UPDATE: Meant to reply to parent's parent.
- tangothedog 15y agosystemtap is another utility in this vein that's worth checking out http://sourceware.org/systemtap/ http://sourceware.org/systemtap/
- nz 15y agoI already checked it out and the timer probes don't work at all. On one system, I couldn't even invoke kernel functions. On another system, the kernel panicked as soon as I executed `stap -e ...`. Basically unusable, at this point. I hope it improves because developers on linux could really benefit from dynamic tracing.
- falcolas 15y agostrace, combined with lsof and netstat can help you with so many things it's not even funny. I've debugged stalled processes across 3 separate servers with these three tools alone.
- user24 15y agoOne I use all the time is this: $ pwd /Users/me/Sites/foo $ pushd . $ cd / $ pwd / $ popd $ pwd /Users/me/Sites/foo Or in English: If you're in a directory that you need to leave but you know you'll go back there in a minute, use "pushd ." to save that directory. Then go off and do whatever you need to, and when you need to return to the saved directory, use "popd" to take you straight back there.
- nfm 15y agoYou can also use `cd -` to return to the previous directory.
- footle 15y agogit "borrowed" this idea too. You can use `git checkout -` to checkout the last thing you checked out; very handy when merging.
- nosignal 15y agoPython sort of did too, you can use _ to access the last-returned variable in the interpreter. [testfunc(x) for x in testlist] <successful result> success = _ print _ <successful result> It's useful when using the interpreter as a calculator, or when you're bashing out some calculation you can never remember how to do properly.
- brettnak 15y agoThe same thing also works in irb. >> "foo" => "foo" >> bar = _ => "foo" >> bar => "foo"
- LiveTheDream 15y agoin bash you can use $_ to access the last argument of the previous command. It lets you do things like this: $ pwd /home/foo $ mkdir -p bar/baz/qux && cd $_ $ pwd /home/foo/bar/baz/qux
- 15y ago
- dpapathanasiou 15y agolsof is also great for monitoring network activity: you can have it tell you about all the tcp/udp connections, as well as who's attached to which port.
- mmphosis 15y agolsof -i
- zobzu 15y agoim surprised when i see all this as if any of those super basic commands or programs were rare ;-)
- joelthelion 15y agoNo autojump? ;-)
- burgerbrain 15y agoMeh, I was hoping for at least semi obscure things like the 'moreutils' utils. Speaking of which, many people don't know about the 'moreutils' utils. Check them out ;)
- pbhjpbhj 15y agohttp://blog.techfun.org/2009/08/moreutils-package-a-must/ http://blog.techfun.org/2009/08/moreutils-package-a-must/ vidir sounds very useful to me.
- kachnuv_ocasek 15y agoI see sponge is somehow useless, how is `| sponge /etc/passwd` different from `> /etc/passwd`.
- ominous_prime 15y agoThe redirect happens before the command is executed, so if you're trying to read from /etc/passwd, it's already been overwritten. Sponge will buffer the output and write the file after the first command has executed.
- kachnuv_ocasek 15y agoAh, excuse my ignorance.
- dedward 15y agoHe explained it on the site - if you are trying to use grep to trim contents from a file and output them to the same file, you will end up with an empty file. Seriously though - everyone knows this. Just use a temp file. He says sponge "keeps the results in memory" - that's a problem if the file is huge. Plus there are other tools to do in-place replacement.
- michaelcampbell 15y agoxargs. Too few people know xargs. Every time I see a "find" with "-exec" in it, my soul cries a little bit. (Yes, sometimes it might be necessary, but in the vast majority of cases not.)
- planckscnst 15y agofind -exec is almost always faster and more secure than xargs.
- e1ven 15y agoWhereas I almost always use for i in `find . -name foo`; do mv $i $i-new; done That's very straightforward, and gets the job done without much weirdness.
- burgerbrain 15y agoIt is my understanding that -exec is almost always slower, since it generally needs to invoke whatever program you are using many many times more.
- fluidcruft 15y agoDoes find have an equivalent for xargs "-P max" flag?
- tange 15y agoYou can emulate it with sem: find . -exec sem --id my_find -j3 sleep 4\; echo {} \; See more about sem: http://www.gnu.org/software/parallel/sem.html http://www.gnu.org/software/parallel/sem.html
- michaelcampbell 15y agoI'd like to hear some justification, cites, or examples for either of these assertions.
- planckscnst 15y ago
- uggedal 15y agoqmv from renameutils will bring up $EDITOR with all files in the current directory in two columns. Edit some filenames and save. The files will be renamed accordingly.
- phaker 15y ago'rename' uses arbitrary perl expressions, including regexes. from the manpage: rename 's/\.bak$//' *.bak
- rdtsc 15y agoAll these article links should just end up here: http://www.commandlinefu.com/commands/browse http://www.commandlinefu.com/commands/browse you can sort by date, #(votes) and also search The top all-time popular one is: * run last command as root: $ sudo !!
- nuclear_eclipse 15y agoTo be fair, that's not even a command; that's a shell expansion that automatically substitutes your previous command in place of the !! before executing it.
- lanstein 15y agoThank you for another '...and this is why I love HN' moment.
- Nick_C 15y agoJust be aware it may not execute the command you think. man bash for HISTIGNORE. Personally I never use it with sudo; it's too dangerous.
- nuclear_eclipse 15y agoThis is also why I disabled HISTIGNORE in both bash and zshell. zshell also allows me to press TAB to expand the command in-place before I press enter, so I can still use the shortcut without the danger.
- pkrumins 15y agoCheck this out -- CommandLineFu One-Liners Explained: http://www.catonmat.net/blog/top-ten-one-liners-from-commandlinefu-explained/ http://www.catonmat.net/blog/top-ten-one-liners-from-command...
- thristian 15y agoIf you're running a modern Linux desktop, you should have a "notify-send" command which will cause a message to be displayed by your desktop-environment's pop-up notification system (on Ubuntu, this command is in the libnotify-bin package). I have two scripts I keep around which I call "notify-success" and "notify-failure": notify-success: notify-send --icon=gtk-dialog-info Success! "$*" notify-failure: notify-send --icon=gtk-dialog-warning Failure! "$*" When I want to run a long-running command and don't want to have to keep checking on it, I'll do something like this: make -j3 && notify-success "Build complete" || notify-failure "Build failed"
- sunburnt 15y agoI do just that, but I also add audible notifications using "play" for when I am not looking at the screen. Use "--expire-time" argument to "notify-send" to control how long the message is displayed.
- icebraining 15y agoI just have a script called 'bell': echo -n "\a" My terminal (urxvt) will detect the bell and set the 'urgent' flag on the window, which is detected by Awesome, which sets the appropriate tag* icon to red. * Tag in Awesome is more or less akin to a virtual desktop.
- xtacy 15y agoOn a mac, you could also use growlnotify. growlnotify -m "Hey I am done!" -t "Done!"
- LiveTheDream 15y agoAlso useful is the `-s` option to make the notification sticky growlnotify -s -t 'Done!' -m 'And I'm not going away until you click me'
- sigil 15y agoWith tmux, you can monitor the window for activity (^A M) and do: make -j3 >make.log 2>&1 ; echo $? When make finishes, your prompt is printed again (activity!) and the window's caption is highlighted. I use the same mechanism to watch for new mail and chats in other windows. It's like Growl for your terminal.
- losvedir 15y agoiftraf sounds interesting. I was looking for just such a program the other day. I was sitting there and noticed with my iStat monitor that I was uploading something at 250 KB/sec. I closed Chrome and eventually all running programs in the doc, yet it still continued. I tried to find out WHAT was uploading that, but to no avail. Any suggestions for tools? I ended up trying iftop, lsof -i, and netstat to get a glimpse, but it stopped before I could get to the bottom of it.
- Game_Ender 15y agoTry "nethogs" found it at: http://www.commandlinefu.com/commands/view/2849/triple-monitoring-in-screen#comment http://www.commandlinefu.com/commands/view/2849/triple-monit...
- sciurus 15y agoNethogs is great when you need to determine what processes are transferring data or how much they're transferring. I got curious how it works, so I ran it through strace and looked through the source. /proc/net/tcp lists all established TCP connections. It includes local and remote addresses and ports and the inode for the socket. Nethogs sniffs traffic and associates it with its entry in /proc/net/tcp. It takes the inode from there and scans through /proc//fd/ looking for the file descripter that has that inode to determine which process has the socket open. Once it finds the process it adds it to a table of inode to process id mappings so it doesn't have to scan through /proc again the second time a packet for that connection comes through.
- jerf 15y agoWireshark or some other packet sniffer can be used to sniff the packets. From the packets you'll get a pretty big clue what's going on. If it's a connection-oriented protocol, you'll be able to trace back to the source ports with lsof, but for this use case often just a glance at what's coming out will be enough to give it away.
- ffffruit 15y agoAlso good - bashmarks - directory bookmarks for the shell. http://www.huyng.com/bashmarks-directory-bookmarks-for-the-shell/ http://www.huyng.com/bashmarks-directory-bookmarks-for-the-s...
- snorkel 15y agowatch is rather handy: # watch packet counters watch ifconfig # watch tcp connections watch netstat -plan # watch the load average watch uptime # watch directory contents watch ls -l
- dicroce 15y agostrace nuff said.
- sciurus 15y agoAnchor's blog is worth subscribing to if you're a sysadmin. The technical content is good, and they can be merciless yet amusing when writing about software that fail to meet their standards of good taste (see http://www.anchor.com.au/blog/tag/fail/ http://www.anchor.com.au/blog/tag/fail/).
- molecule 15y ago> Want to kill all processes being run by a given user? Issue a pkill -U USERNAME; sure beats the hell out of... similarly: killall -u USERNAME OFWGKTA
- moe 15y agoI think there's a critical one missing in this list: https://github.com/busyloop/lolcat https://github.com/busyloop/lolcat
- julius 15y agoA ridiculously unknown tool is "expect". Automates anything. Saved me hours of typing. Wrote about it (use case + example) here: http://www.chocobrain.com/Advanced-terminal-automation-with-expect/4NRKAvM2NWZqai6a3CoDQe http://www.chocobrain.com/Advanced-terminal-automation-with-...
- evmw 15y ago"at" is very handy for scheduling one-time scripts/commands echo my_script.py | at 4am tomorrow then list queued jobs with atq note: its not enabled by default on mac os x, see man atrun
- bdr 15y agoIn the 'top' family, iotop is extremely useful for identifying bottlenecks. It shows you the I/O rate per process, which wasn't even possible until a recent kernel.
- atsaloli 15y agoHere are the tools I find indispensable as a sys admin: - screen for accessing servers - lshw for seeing Linux server hardware config - cfengine for automating my system administration - atop as an advanced top http://www.atoptool.nl/downloadatop.php http://www.atoptool.nl/downloadatop.php atop uses color to show when a subsystem goes over warn/critical threshold. it can be run in present time, or can be used to go back in time and "play back the tape".
- Jach 15y agoWhile we're mentioning tools that break the "prog | xargs .." pattern, I've found ack to be pretty useful. ( http://betterthangrep.com/ http://betterthangrep.com/ )