17 ms·
Unix tricks
- carlesfe 14y agoSo happy to see this on HN! Actually I took most of the tips from unix threads here and r/commandline, I highly recommend that subreddit if you like this kind of tricks! Edit: also, @climagic: https://twitter.com/climagic https://twitter.com/climagic Second edit: I'm enhancing the file with these comments, so if there are any inconsistencies between the txt and a commenter, it's my fault.
- adlpz 14y agoHostiàs! Topopardo! I used to follow your podcasts long ago :D. Anyway, nice list of tips.
- ciupicri 14y ago> SMB is better than NFS. More details would be nice.
- carlesfe 14y agoNFS basically breaks the client computer until the host responds, while SMB detects the I/O error earlier and, at least, doesn't hang the terminal. Other than that, SMB allows for permissions and a bunch of other features. It is, basically, a more modern and robust protocol. Does NFS shine for some use cases? Indeed. Would it be the first choice for most ones? Nope. Disclaimer: my sysadmin skills are not so great, I'm talking as a user
- EwanToo 14y agoNFS has 2 mount types, soft and hard. http://tldp.org/HOWTO/NFS-HOWTO/client.html http://tldp.org/HOWTO/NFS-HOWTO/client.html Soft mounts report errors immediately, hard mounts hang. And for permissions, NFS provides everything under the sun that you could possibly need via ACLs. NFSv4 is a very modern protocol, much as SMBv2 is (not SMB though, it's awful). Linux has had NFSv4 for what, 6 years now, at least, and even v2 and v3 had some limited ACL support? http://wiki.linux-nfs.org/wiki/index.php/ACLs http://wiki.linux-nfs.org/wiki/index.php/ACLs
- carlesfe 14y agoThat's nice to know, it seems that we have the "hard" configuration at the lab and it's a real pain. Backwards compatibility, you know. For my mini-cluster we use SMB and couldn't be happier.
- EwanToo 14y agoNot sure what your lab's trying to be backwards compatible with (NFS has had those mount options since at least 1989), but whatever works for you :) It's a mount option on the client, not the server, so maybe you can change it yourself. http://www.ietf.org/rfc/rfc1094.txt http://www.ietf.org/rfc/rfc1094.txt
- ominous_prime 14y agoIn a lot of cases, hard is what you want. Some systems can't deal with fs errors very well, and it's better to wait than to risk data inconsistencies.
- deleted 14y ago[deleted]
- easytiger 14y agoi cannot possibly disagree more. Are you using NFS from 1999?
- jameswyse 14y agoI've found NFS to use much less cpu than SMB (on raspberry pis running XBMC at least)
- shrikant 14y agoI third this. I was attempting to set up my Raspberry Pi for sharing a big HDD that was plugged into it on my home network, and the NFS mounts appear to weigh lighter on my vanilla Raspbian setup. And fwiw, if there's some sort of error with the mount, the terminal lets me know right away.
- cowmix 14y agoI second this.. If you use any NAS device with a weak CPU and you have a choice of CIFS or NFS, the NFS transfer rates are normally 25% faster and the load is much less on the server.
- lobster_johnson 14y agoI believe NFS is a less chatty protocol with fewer roundtrips. SMB is not a particularly well-designed protocol overall. My only evidence is anecdotal. Back in ~1998 we had Samba running on our small Linux file server using Windows NT desktops via 10mbit Ethernet. It was dog slow, not just for browsing but on sequential things like file transfer. We installed NFS mounts on the same box, and it turned out to be lightning fast. I don't remember just how fast, but it made everyone go "wow". Nowadays, with NFS over fast 100mbps or gigabit Ethernet the latency difference probably is not significant enough to make a difference. I prefer NFS just because its more Unixy.
- gnu8 14y ago1) The caveats of NFS can be crippling if you are a rubbish sysadmin. NFS requires a more thorough understanding than what you can get from a tip sheet. 2) Samba is single threaded, performance will suffer serving SMB from a Linux machine. For this reason it would be better to serve SMB from a Windows machine. 3) Using a foreign protocol between homogenous computers when the native protocol will do is non-ideal. NFS is the right thing when sharing filesystems from Linux to Linux.
- duggieawesome 14y ago:set spell is pretty neat. Will use not when coding, but when writing blog posts.
- nathan_long 14y agoIf you put that in, for example, `~/.vim/ftplugin/markdown.vim`, it will be used on all markdown files. (`../ftdetect/markdown.vim` controls how Vim determines that a file is markdown.)
- jamesmiller5 14y agoI turn that on when editing Git commits. autocmd FileType gitcommit setlocal spell
- niggler 14y ago1) `pgrep` is a standard utility that does what his `psgrep` does and much much more. 2) htop is a cpu and memory hog -- every time I've used it I noticed it takes 6+% CPU time 3) there's an awk trick to do the `sort | uniq` recommendation that works on 10+GB files (single pass): awk '!x[$0]++' 4) Passwordless keys are dangerous -- use ssh-agent to save the password of the keys
- vvpan 14y agoOn my computer htop consumes about the same amount of resources as top, just tested.
- carlesfe 14y agoNot trying to contradict you, just some explanations 1. I prefer 'psgrep' because it covers 99% of my use cases for pgrep (ps axuf | grep $NAME) 2. htop is very nice, come on! Would not let it running on the background for hours, but it's nicer than top 3. Note taken, thanks! 4. Is ssh-agent really safer than using passwordless keys? Just asking, I'm curious
- niggler 14y ago1. `man pgrep` is your friend (you save two greps, and TBH the `grep -v grep` should be a hint that there's a better way) 2. In my experience on Debian (granted this was in 2010), there is a noticeable performance difference between `htop` and `top`. 4. ssh-agent stores the password in memory and is erased on reboot. OTOH If you use a passwordless key file, anyone can use it if they have the key.
- daGrevis 14y agoOnly one Vim tip? I'm disappointed. Check out this resource! http://www.rayninfo.co.uk/vimtips.html http://www.rayninfo.co.uk/vimtips.html
- carlesfe 14y agoGreat resource! Thanks for the link. Most of my vim tips are on my .vimrc (my dotfiles here https://github.com/carlesfe/dotfiles/blob/master/.vimrc https://github.com/carlesfe/dotfiles/blob/master/.vimrc) and on the links on my homepage: http://mmb.pcb.ub.es/~carlesfe/#programming_h3 http://mmb.pcb.ub.es/~carlesfe/#programming_h3. The .txt that op linked feels a bit out of context :)
- DrinkWater 14y agoGood lord, thats a big list! Thanks!
- bramswenson 14y agoSMB is better than NFS in Unix Tricks? Shame Shame Shame. Possibly just wrong. Like the rest though. Thanks!
- carlesfe 14y agoI know, that's controversial... but I've had zero problems with SMB and many ones with NFS. Just my two cents.
- appplemac 14y agotar czf - . | ssh destination "tar xz" To pipe all the contents of your current directory (including dotfiles) to the destination machine. Greetings from LSI-UPC!
- carlesfe 14y agoI use that everyday, but didn't think about adding it to the list. It's on now, thanks! PS: hey, we're neighbors! :)
- deafbybeheading 14y agoWhy not rsync?
- inyourtenement 14y agoThey're both useful. tar over ssh is very fast for large amounts of data. rsync is only fast if most of the data already exists.
- cowmix 14y agoI would change this to: tar czf - . | ssh destination "cd /remote/dir; tar xz"
- appplemac 14y agoThere is also tar -C /local/dir -czf - . | ssh dest "tar -C /remote/dir -xz"
- zengargoyle 14y agoYou will one day punch yourself for not being safe. tar czf - | ssh destination 'cd /remote/dir && tar xzf -' Test that `cd` or one day you'll end up extracting in the wrong place.
- kbenson 14y agoI used to use this, because it's awesome that it got all the file types correct (sockets, for example). Then I learned to love rsync -av. The benefits are many-fold.
- nathanstitt 14y agoWhut? ctrl-r? How have I missed that? No more 'history|grep foo' for me!
- btipling 14y agoAlso ag is faster and better than grep: https://github.com/ggreer/the_silver_searcher https://github.com/ggreer/the_silver_searcher
- pavel_lishin 14y agoTry this in your .inputrc: # Bind the up arrow to history search, instead of history step "\e[A": history-search-backward "\e[B": history-search-forward No more "^rls" to search for ls in your bash history; just type "ls" and start hitting the up arrow.
- dunk010 14y agoThis is the best tip ever.
- nathanstitt 14y agoThanks for the suggestion. I used to have a very customized .bashrc with nice little things like that, but have decided to stick with standard stuff for things that work off of muscle memory. I got tired of sshing into a new box and half the things I'd type wouldn't work properly until I remembered to copy my settings files over, which seemed more trouble than it was worth for a short-lived s3 instance. That's why I was excited to discover ctrl-r. It's a built-in method of searching history that I can remember and it'll work everywhere.
- pavel_lishin 14y ago> I got tired of sshing into a new box ... I've got a public repo of my dotfiles, so the first thing I typically do is "git clone git@github.com:pavellishin/dotfiles.git && cd dotfiles && ./install.sh" After that, I launch tmux, and it's all hunky dory.
- setrofim_ 14y ago> Compile your own version of 'screen' from the git sources. Most versions have a slow scrolling on a vertical split or even no vertical split at all or just use tmux
- tomsthumb 14y agoJust to note, splits in tmux and screen behave differently. iirc, in screen you have a set of splits and fill them in with different ports (kind of how vim thinks of viewports). so technically you can have the same viewport open twice on your monitor and the other will mirror the workings of the one that your are working in right now. in tmux each 'tab'/window is a set of splits, which act more like sub-windows, and don't really share across multiple spaces.
- crusso 14y agoThe special bash command I'm most often asked about by shoulder surfers is !$. It substitutes the last argument in the previous command into the current one. For example: $ ls /some/long/path/somewhere/looking/around/ <output> $ cd !$ cd /some/long/path/somewhere/looking/around/
- yan 14y agoI find ESC, . (esc, then press period) to be more intuitive. (alt+period works as well)
- kps 14y agoAn arguably better (and slightly more portable) way to accomplish this is the special variable $_ , which expands to the last argument of the previous command. Since $_ is a variable rather than a history substitution, it still works when there is no command history (e.g. in scripts) and allows all the usual variable expansion forms, for example ${_##*/} to extract the last path component.
- crusso 14y agoThat's good to know, although !$ is easier to type since you only need to depress the right shift key, whereas yours requires a quick shift on the opposite side. Makes a big difference when you're trying to quickly type "rm -rf !$" as root. :)
- garethadams 14y agoI don't think I'd type `rm -rf [anything]` quickly as root
- crusso 14y agoHence the smiley. Was a joke.
- seanp2k2 14y agoAs a righty, I rarely ever use the right shift key. Also, check it: $ cd /happily/tabbing/out/some/really/deep/path/ooh/dear/maybe/its/java/WAIT_A.file bash:> 'WAIT-A.file' is not a directory $ nano $_ (opens file)
- globalpanic 14y agofind . -name "file-wildcard" -exec "string" {} ";" -print is something I use a lot - plus xargs sometimes
- martinced 14y agoI use variations on "find" so often that I've created several little commands "fij" (find in any java source), "fit" (find in any text / org file), "fix" (find in any XML file) etc. which I use all the time
- prakashk 14y agoYou might want try `ack` then. (http://betterthangrep.com/ http://betterthangrep.com/). On Ubuntu/Debian systems, it is packaged as `ack-grep`.
- Symmetry 14y agoLots of good tricks, though I replaced a lot of them with "Use fish instead of bash" http://ridiculousfish.com/shell/ http://ridiculousfish.com/shell/
- deleted 14y ago[deleted]
- deleted 14y ago[deleted]
- bradbeattie 14y agoOf similar note, http://www.commandlinefu.com/commands/browse/sort-by-votes http://www.commandlinefu.com/commands/browse/sort-by-votes indexes a good number of these tricks.
- firebones 14y agoWanted: a lint for your history that analyzes your commandline usage, suggesting these types of tips based on your historical use. history | commandlint
- implicit_cast 14y agoMore: Bash and zsh support a surprising amount of emacs editing functionality. Cursor navigation (M-b, M-f, C-a, C-e), text selection (C-space), copy/paste (C-w, M-w, C-y, M-y), and undo (C-_).
- deleted 14y ago[deleted]
- jofel 14y agoInstead of find . -type d -exec chmod g+x {} \;' you can usually use chmod -R g+X . which gives additionally the group execute permission to files which have already user/everyone execute permission.
- jcr 14y agoThis is bad juju. 'find . -type d -exec chmod g+x {} \;' If you happen to have a malicious directory named (without double quotes): ".;sudo rm -rf /" You'd be stuffed. It's better practice to use the '-print0' flag of find(1) and pipe the result into xargs(1) with the '-0' flag set. For safety, it's best to also use '-r #' for expected number of arguments, '-n' to stop xargs from running without arguments, and "-J %" so you can use quoting. find . -type d -print0 | xargs -0 -n -r 1 -J % chmod g+x "%" I believe some on implementations '-n' is unnecessary when '-r #' is specified, but on other implementations it's is the maximum. EDIT: thanks for the vim tip on for finding spelling mistakes!
- neoteric 14y agoThis is simply not true: $ mkdir '; echo woops' $ find . -type d -exec echo {} ';' . ./; echo woops As you can see 'woops' is never echoed. EDIT: The reason being the shell is never involved in this process, and the shell is what is responsible for splitting commands on semicolons/newlines.
- jcr 14y agoYou are assuming the exact versions of the shell and find programs that you use are the only ones that exist. It may not be a problem on your exact system, but it can be a problem elsewhere.
- neoteric 14y agoI am assuming a POSIX-compliant implementation of `find`. The shell is not involved. FWIW, your `find -print0`/`xargs -0` is not POSIX.
- jcr 14y agoSorry, I didn't see your 'EDIT' caveat when I responded --now that will teach me to reply too soon. ;-) Also, it seems I failed to be clear; I'm probably too tired I suppose. My point was there is plenty of ancient and buggy code out there. It could be "most", or even "many" modern unix variants have fixed a lot of the old bugs in find(1), but if you don't have the luxury of working on a current system, and your not allowed to upgrade it, then plenty bad things can happen due to invoking a shell, handling space, quote, and delimiter characters, and so forth. reference: $ uname -a OpenBSD alien.foo.test 5.1 GENERIC.MP#207 amd64 setup: $ mkdir test $ cd test $ touch file1 $ touch file2 $ touch file3 $ mkdir ';ls' bad: $ find . -type d -exec sh -c {} \; sh: ./: cannot execute - Is a directory ;ls file1 file2 file3 test.sh better: $ find . -type d -exec sh -ec {} \; sh: ./: cannot execute - Is a directory also bad: $ find . -type d -print0 | xargs -0 -r -n 1 -J % sh -c "%" sh: ./: cannot execute - Is a directory ;ls file1 file2 file3 test.sh better: $ find . -type d -print0 | xargs -0 -r -n 1 -x -J % sh -ec "%" sh: ./: cannot execute - Is a directory better; $ find . -type d -print0 | xargs -0 -r -J % sh -c "%" best: $ find . -type d -print0 | xargs -0 -r -J % sh -ec "%" POSIX is all great and wonderful in theory, but in practice it's no different than the bogus Java "write once, run anywhere" claim. If a system or utility claims to be POSIX compliant, then you're probably close, but you'll still need to do testing and debugging. At least some of the issues with find/xargs are mentioned in the following wikipedia article. It's probably more clear than I am right now. http://en.wikipedia.org/wiki/Xargs http://en.wikipedia.org/wiki/Xargs
- mixedbit 14y agoack-grep is great for searching source code
- sepeth 14y agoYesterday, I discovered the_silver_searcher: https://github.com/ggreer/the_silver_searcher https://github.com/ggreer/the_silver_searcher I think you might like it.
- kibwen 14y ago- 'cd -' change to the previous directory you were working on To my surprise, this also works with git: git checkout - # to checkout the previous branch
- VeXocide 14y agoInstead of ProxyCommand ssh -T host1 'nc %h %p' you can use ProxyCommand ssh -W %h:%p host1 which uses ssh itself and therefor also works on machines where netcat isn't installed.
- carlesfe 14y agoHi, I tried that but my ssh version doesn't have the -W flag. What would you suggest?
- argarg 14y agoUnfortunately OSX uses a badly outdated openssh version just like many other unix tools and it doesn't support the -W option. You could try upgrading openssh using homebrew if you want.
- Ensorceled 14y agoWeird, I've been using vi/elvis/vim/MacVim as my primary editor since 1984 and I hate vi key bindings for shell; I always use the emacs bindings.
- AaronBBrown 14y agoI'm the opposite. Every time I log into a server that doesn't have my bashrc, I immediately have to `set -o vi` or else I'm useless.
- Ensorceled 14y agoThat's why I love *nix ... options!
- growt 14y agoIf I may add a trick: ctrl-z - stops a program bg - sends the stopped program to the background fg - gets the program back to the foreground (interactive mode) very useful in editor sessions or when you want to get rid of the endless download/scp that is blocking your terminal
- sp332 14y agoYou can also use "jobs" to see background and stopped tasks. If there are multiple jobs, you can use "fg 2" to foreground the second job on the list, etc.
- niggler 14y agoNote that each job gets an identifier (which you can see by running `jobs`). Other commands like `kill` can work with the id number by using %[id]. For example: $ some_command ^Z (hit control z) $ some_command_2 ^Z (hit control z) $ jobs [1]- Stopped some_command [2]+ Stopped some_command_2 $ kill %1 $ jobs [1]- Terminated: 15 some_command [2]+ Stopped some_command_2
- dminor 14y agoWay back in my college days, one of the student admins took down the CS department's server by forgetting the % and killing process 1 by mistake.
- ottbot 14y agoAnother nice job management tool is disown, which lets you log out of your session without killing the job (similar to starting the command with nohup)
- mpyne 14y agoAnd also, you can pass the -h flag to disown to leave the job in the job control table (i.e. you can still bring it to the foreground/suspend it/etc.), but still skip sending the SIGHUP on logout.
- etrain 14y agoHis last trick - compressed fie transfer without intermediate state: 'tar cz folder/ | ssh server "tar xz"' Can be pulled off with two flags to scp - and you get to see progress as a benefit! scp -Cr folder server:dest/
- kbenson 14y agorsync -av folder $USER@$SERVER:/destination/path. scp won't copy certain file types correctly. Rsync really is better here, and not just because of that.
- stevvooe 14y agoYou need a -z to activate compression. But, nonetheless, please always use rsync when copying host to host.
- inyourtenement 14y agorsync is slow if the data is not already on the destination. tar over ssh is fast, and tar over socketpipe is even faster but not encrypted. I'm not aware of any attributes that tar doesn't preserve.
- ce4 14y agoThere's cryptcat if you need encryption. "bar" is also a nice little program if you like to see an ETA (c.f. http://clpbar.sourceforge.net/ http://clpbar.sourceforge.net/ )
- kbenson 14y agoHow so? Also, I find that if I'm going to copy the data once, I'm often going to copy it twice, or which to get a more up to date version of it at a later time. Rsync clearly wins in these cases. Finally, from the compress flag on rsync: Note that this option typically achieves better compression ratios than can be achieved by using a compressing remote shell or a compressing transport because it takes advantage of the implicit information in the matching data blocks that are not explicitly sent over the connection.
- 65a 14y agoHere's one it took me a while to figure out... Hung SSH session (such as wifi out of range)? Type Return-Tilde-Period
- philsnow 14y agoAlso, if you've done this: you@somehost:~$ ssh otherhost you@otherhost:~$ some_command If you hit ^Z right now, it will tell the shell on otherhost to stop some_command and give you the shell prompt on otherhost back. If instead you wanted to stop the ssh process and get the shell prompt on somehost, hit Tilde ^Z (you don't have to hit a new Return, but ssh only notices these escape sequences after a Return). Also if you use ControlMaster and have a few xterms open all with sshs to otherhost, and then you exit the first ssh you happened to have open, it will seem to hang and not give you your prompt back. What's happening is that that ssh process is the "control master" and it's still open because you've got other sshs to the same host open. Hit Tilde & to background the ssh process and get your terminal back. Yes, you could also hit Tilde ^Z and then 'bg' the ssh process.
- init0 14y agoMd version for readabilty https://gist.github.com/hemanth/5109020 https://gist.github.com/hemanth/5109020
- mikegioia 14y ago'ssh -R 12345:localhost:22 server.com "sleep 1000; exit"' forwards server.com's port 12345 to your local ssh port, even if you machine is not externally visible on the net. This one blew my mind
- malloc2x 14y agoBetter to use, e.g.: ssh -fN -o ServerAliveInterval="240" -R 2222:localhost:22 example.com (And on example.com ssh -p 2222 localhost) This lets you easily keep the tunnel open long-term. Why? -f Background the ssh process (don't need nohup) -N Don't run any command -o... Make ssh do the work of keeping the session alive forever
- jackalope 14y agoThe ServerAliveInterval option might also help with another issue mentioned in the post: 'sshfs_mount' is not really stable, any network failure will be troublesome' To avoid trouble with remote backups and other long running processes, I always add the following to the end of ~/.ssh/config or /etc/ssh_config: Host * ServerAliveInterval 300
- carlesfe 14y agoBut the remote host can limit your ServerAliveInterval, however most hosts don't close your session if there is something running. On our clusters this is the only working solution, and I tried both, trust me.
- malloc2x 14y agoIf that's an issue (I've never met a server like that, not to mention one like that and I couldn't change the setting) then wouldn't you rather use this? while [ true ]; do sleep 1000; done Just using: sleep 1000; exit means you can't make new connections after 17 minutes.
- carlesfe 14y ago
- gcv 14y agoLearn to use your shell's globbing features instead of overusing find. In zsh, you can do 'print -l /*.(c|cc|h|hh)' for example (I'm sure bash has an equivalent).
- oftenwrong 14y agoFor me, the glob gets a lot of use.
- oftenwrong 14y ago** glob
- aw3c2 14y agodoes that work for say 2143789 files?
- klibertp 14y agoI actually hit a limit quite a few times already - I don't remember if it was the shell that complained or the command (mv, cp, ...) itself, but I know I couldn't execute the command. find with xargs or -exec worked, however.
- aw3c2 14y agoIt's what happens if one uses * or similar globbing. The shell will try to pass all the files as arguments to the command and if it is too many, it will fail. That is why using find is often needed.
- unfletch 14y ago'!!:n' selects the nth argument of the last command, and '!$' the last arg A lot of people know about "!$" (which is shorthand for !!:$), but that's just the tip of Bash's history expansion. I use these things all the time. One of my favorite keystroke savers is adding :h, the head modifier, to !$. For example: $ cp file.txt /some/annoyingly/deep/target/directory/other.txt $ cd !$:h $ pwd # => /some/annoyingly/deep/target/directory Once you understand how each component works it's easier to put them together into new (to you) combinations. For example, once you know that !$ is shorthand for !!:$, it's not a huge leap to reason out that you can use !-2:$ to get the last argument to the 2nd-to-last command. Or !ls:$ for the last arg to the most recent `ls` command. I also prefer to do substitution with the :s modifier rather than ^ as suggested at the link, for consistency's sake: $ echo "foo bar" foo bar $ echo !!:s/bar/baz foo baz $ echo !?bar?:s/foo/qux qux bar Relevant Bash manual pages: http://www.gnu.org/software/bash/manual/html_node/Event-Designators.html http://www.gnu.org/software/bash/manual/html_node/Event-Desi... http://www.gnu.org/software/bash/manual/html_node/Word-Designators.html http://www.gnu.org/software/bash/manual/html_node/Word-Desig... http://www.gnu.org/software/bash/manual/html_node/Modifiers.html http://www.gnu.org/software/bash/manual/html_node/Modifiers....
- jschmitz28 14y agoThat first example is great, thanks. I run into that all the time. Up until now I've been doing this: $ cp file.txt /some/annoyingly/deep/target/directory/other.txt $ cd !$ (No such file or directory) (up arrow and then backspace to directory)
- cschol 14y agoAlt + . – use the last word of the previous command. $ cp file.txt /some/annoyingly/deep/target/directory/other.txt $ cd then press Alt + . $ pwd # => /some/annoyingly/deep/target/directory
- ralph 14y agoNo, that gives the last word, as you say, we want the dirname of the last word. $ echo foo/bar foo/bar $ echo !$ !$:h foo/bar foo
- DanHulton 14y agosudo !! Perform the previous command as root. Great if you continually forget which of your scripts need to be run as root and which don't.
- rburhum 14y agoI freaked out when I found out about the "screen" command several years back. "screen" starts a virtual screen that you can detach from with "ctrl-a d" and you can log out, login from a different machine/session and reattach with "screen -r". it has history so you can run long running commands and reattach 3 days later to continue from where you left off as if you had been logged in the whole time.
- dirkk0 14y agoand you can do things like "screen -X -S minecraftserver -p 0 -X stuff "say test $(printf '\r')"
- base698 14y agoTmux is the new screen. tmux allows for emacs or vim key bindings to run around the buffer and search, splits horizontal and vertical and easier configuration: http://tmux.sourceforge.net/ http://tmux.sourceforge.net/
- revscat 14y agoYup! You might want to try tmux, though. I used screen for many years and switched over to tmux about 2 years ago, and have been really happy with it. It (tmux) is also maintained more regularly now; screen development seems to have stagnated. Either way, enjoy!
- rburhum 14y agoI will checkout tmux. Thank you!
- re_todd 14y agoTmux is similar, here is a cheat sheet that helped me move from screen to tmux: http://www.dayid.org/os/notes/tm.html http://www.dayid.org/os/notes/tm.html
- artursapek 14y agoRegarding set -o vi, which I love, is there a way to have it load my vimrc as well? I have custom bindings I would love to have.
- McUsr 14y agoThis was a nice very timely page sitting with my private repo in mercurial, and the other one at github… I discovered help <builtin> some months ago, and that was a great boon really. Like help "test" so I didn't have to go the rather large bash man page. Here is a little shellscript for displaying a man page on Mac Os X (gman). (If you then click on on of the links on the man-page, it may pop up in your default browser). #!/bin/bash if [ $# -lt 1 ] ; then echo "gman takes a man page, if found and formats it into html." echo "Usage: gman [manfile]" exit 2 fi a=`man -aw $* |head -1` if test x$a = x ; then echo "Can't find $1" exit 1 fi # Figures out if it is a normal man page or something else (gz). b=`man -aw $* |head -1 |grep "gz"` echo $b if test x$b = x ; then groff -man $a -Thtml >|/tmp/tmp.html else gzcat $b |groff -man -Thtml >|/tmp/tmp.html fi qlmanage -p /tmp/tmp.html >/dev/null 2&>1
- D9u 14y ago- Use 'apt-file' to see which package provides that file you're missing Ummm, "apt-" isn't a "Unix trick..." It's specific to linux distros which use the "Aptitude" package manager. Linux != Unix
- iso-8859-1 14y agoAptitude is just a front-end to APT. And APT (package tool) is just an interface to dpkg or RPM (package managers).
- siavosh 14y agoIs there a command/short-cut to storing the path(s) returned from a find command? Currently I do something like: find . -name somefile.txt -print Then copy and paste the results manually into a cd command for example. I feel like there's a much better way somewhere.
- ralph 14y agocd `find -name foo`
- Tyr42 14y agoxargs? Here is an example from the man page: >For example, the following command will copy the list of >files and directories which start with > an uppercase letter in the current directory >to destdir: /bin/ls -1d [A-Z]* | xargs -J % cp -rp % destdir You can use this with find really nicely. find . -name somefile.txt -print0 | xargs -0 -J % cp -rp % destdir Note the -print0 and -0 flags (zero). This will use a null byte as a separator instead of spaces to avoid failing on files with spaces in their names.
- FiloSottile 14y ago* Read on 'ssh-keygen' to avoid typing passwords every time you ssh He meant ssh-agent?
- jabo 14y agoNo he did mean ssh-keygen which generates a public and private key pair for you. Run ssh-keygen on your local machine and copy the public key to ~/.ssh/authorized_keys and you'll be able to login to the server without a password using ssh -i /path/to/private/key user@host
- LiveTheDream 14y agoThat approach is insecure, however, because anyone with the private key now has access. When running ssh-keygen, you should add a passphrase to the key, then add the key to ssh-agent so you don't need a password for the account, nor do you need to type the key's passphrase constantly.
- emidln 14y agoAnyone with private key access has control over my user account and has better access than what my private key + passphrase would provide. I understand layers (probably moreso than most), but this is something that always bothers me a lot from a practicality perspective. My passwords are encrypted at rest via encrypted filesystems. If you are running things on my personal machine as my user account, I'm already being keylogged and/or am executing arbitrary code for you. If I'm logged into somewhere via ssh (hint: I am whenever I have a network connection), you can just scan my ssh config and use my ssh key anyway. From there, you can probably do a lot of other nasty stuff. ssh-agent won't really prevent this. It will prevent the malware from working again when I reboot until I log into another remote host (which I've established I do a lot) where the keylogger now gets me. It's possible, but extremely unlikely that I have might have completely read-only media. I could be using my TPM device to protect from from booting and executing modified system states. Some of this might prevent you from easily persisting the keylogger threat across reboots. I might also have a module or something that calculate checksums on startup of critical things, have ridiculous anti-exfil outgoing connection policies, etc that prevent all but the most targeted attacks. I don't have all of that in place. (In particular, to anyone generating a profile for me, I don't build detailed outgoing packet filter rules (you are welcome).) But what I do have in place will probably prevent me from getting my initial passphase keylogged if I used ssh-agent since it's likely (although this isn't strictly necessary) that I'm going to get attacked again after I start logging into remote hosts. So they can't steal my password, but they do have unrestricted access to my user account and the remote users I can log into. That complicates things, but is still a major security failure, to the point where them having the passphrase to my key isn't super important. I mean, in this scenario, they already have the absolute best input vector (a history of me logging in so they can execute attacks at the times I'm supposed to be logging in, as well as direct access to the systems from my ip addresses) to the point where using my ssh key from elsewhere is probably a worse a idea.
- woodchuck64 14y agozsh: setopt extendedglob allows you to use numeric ranges on globbing with < >: mv p1080<100-300>.jpg folderx/ to move p1080100.jpg through p1080300.jpg to a new folder. Still not available on bash or more common shells?
- LukeShu 14y agoBash: mv p1080{100..300}.jpg folderx/
- 1amzave 14y agoThough that's not strictly a glob, but brace expansion (it will expand to all the numbers in that range regardless of whether or not files with those names exist). bash does have 'shopt -s extglob', which enables a number of useful globbing extensions, though I don't believe there are any numeric ones among them.
- woodchuck64 14y agoTIL, thank you. Here it is from the manpage: A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or single characters, and incr, an optional increment, is an integer. When integers are supplied, the expression expands to each number between x and y, inclusive. Supplied integers may be pre‐fixed with 0 to force each term to have the same width. When either x or y begins with a zero, the shell attempts to force all generated terms to contain the same number of digits, zero-padding where necessary. When characters are supplied, the expression expands to each character lexicographically between x and y, inclusive. Note that both x and y must be of the same type. When the increment is supplied, it is used as the difference between each term. The default increment is 1 or -1 as appropriate.
- dunk010 14y agoHere's a really great presentation I found (probably on HN) some years ago. http://www.ukuug.org/events/linux2003/papers/bash_tips http://www.ukuug.org/events/linux2003/papers/bash_tips All extremely useful, my favourite being the .inputrc rebindings of up and down to search history. Takes a little getting used to but is great once you are (good luck to anyone else trying to use your terminal though ;))
- artagnon 14y agoFeels very outdated. 1. Use zsh, not bash. AUTO_PUSHD, CORRECT_ALL and tons of other options make some tricks redundant. Also, the zle M-n and M-p are more useful than C-r imo. 2. Use tmux, not screen. 3. Use z (https://github.com/rupa/z https://github.com/rupa/z), not j.py. 4. Use cron, not at. Or even systemd timer units, if you're so inclined. 5. Use public-key authentication and keychain, not password-based SSH. 6. Don't send emails from command-line naively; you have no control over the headers. Use git-send-email or similar. 7. Consider using something slightly more sophisticated than Python's SimpleHTTPServer to share files/ folders. One example: woof (http://www.home.unix-ag.org/simon/woof.html http://www.home.unix-ag.org/simon/woof.html)
- laichzeit0 14y agoI take it you don't work on many disparate unix systems on a daily basis :) I find most of the time I'm lucky if there's even bash installed on the remote system, it's usually ksh. tmux? way too new. Python? Nope. Perl is the only scripting language I'd wage my balls on beyond awk if I hope to reuse the script again. Sad, but I find this is generally the case in extremely large enterprises where there is a mix of AIX, HPUX, Linux and Solaris being used due to years of weird procurement decisions. Sigh.
- carlesfe 14y agoYes, that's exactly why those tricks "feel outdated". It's because they're made to run on systems from the early 2000s
- rcfox 14y agoIsn't at just a cron helper for one-off jobs?
- emidln 14y agoSadly this isn't usually the case
- philsnow 14y agoI've given tmux two separate tries and both times it had screen corruption issues with curses apps. screen is tried (and tried, and tried) and true. I've also invested enough time in learning bash over the years that zsh is not a net-win for me. I've switched to using it on some systems but I seldom use more than what's available in bash. I would switch back to bash on these systems for consistency, but I feel like I've already sunk too much time in to this experiment of using zsh. don't be a zsh/tmux hipster; there's nothing wrong with "good enough". this lesson has played out several times as businesses / software with arguably better execution / implementation loses out to existing players that have been around a while. Edit: though, if you're relatively new and haven't been using bash/screen/whatever for decades, I don't think anybody would call you a hipster for using zsh/tmux instead of bash/screen. The marginal utility of learning tmux or zsh is much higher for somebody who hasn't already used other stuff forever.
- xiaoma 14y agoOne of my new favorites-- use ctrl-r autocompletion to grab a command close to what I want from history, tack on a character that breaks it if necessary and run that. Then use fc to get that command back in a vi editing context, change what I want and :wq The resulting command is immediately executed. This process can be really fast compared to manually constructing long piped commands.
- networked 14y agoRelated earlier discussions: https://news.ycombinator.com/item?id=5022457 https://news.ycombinator.com/item?id=5022457 https://news.ycombinator.com/item?id=4481234 https://news.ycombinator.com/item?id=4481234 A pretty good thread on Reddit: http://www.reddit.com/r/linux/comments/mi80x/give_me_that_one_command_you_wish_you_knew_years/ http://www.reddit.com/r/linux/comments/mi80x/give_me_that_on... Edit: see https://news.ycombinator.com/item?id=3257393 https://news.ycombinator.com/item?id=3257393 for a discussion of the above thread. While we're at it, consider using weborf [1] as an alternative to Python's SimpleHTTPServer for simple file sharing. I found it able to saturate a gigabit ethernet connection when hosted on a Core 2 Duo ULV laptop with an SSD. [1] See http://galileo.dmi.unict.it/wiki/weborf/doku.php?id=start http://galileo.dmi.unict.it/wiki/weborf/doku.php?id=start. It's available from the official repos in Debian and Ubuntu with sudo apt-get install weborf Invoking it is dead simple: weborf -b ~/dir-to-share
- dfc 14y agoRE: weborf I will have to look at weborf. I have always wished debian packaged publicfile, similar to djbdns or even dbndns. As it is I am still looking for a "djbdns-like http server" that is apt-get installable and actively maintained. I will never understand why gnome-user-share depends on apache...
- keenerd 14y agoConsider webfs too. It is even simpler than weborf. http://packages.debian.org/search?keywords=webfs http://packages.debian.org/search?keywords=webfs
- deleted 14y ago[deleted]
- hackerpolicy 14y agoIf you work with something like this /this/is/some/very/nested/directory/ and you need to move to almost the same structure /this/be/some/very/nested/directory/ then you might benefit from function bcd { cd ${PWD/$1/$2} }
- jbp 14y agoAs an emacs user I map "C-p" "C-n" for history search in bash In .inputrc "\C-p": history-search-backward "\C-n": history-search-forward Also I found the following settings to be very useful: # List the possible completions when Tab is pressed set show-all-if-ambiguous on #tab complete without needing to get the case right set completion-ignore-case on
- marekrud 14y agoGNU parallel: easily substitute of file extensions + parallel execution, e.g. ls *.png | parallel convert {} {.}.jpg
- emmelaich 14y agoI see people often have aliases for ls -ltr | tail but ls -lt | head might be a lot faster with a lot of files.
- runejuhl 14y agoWhen creating excessively long oneliners (you know, the kind that should actually be a script, because you know you're going to find a use for it in a few weeks), the following key combo is golden: ^x ^e It opens up the exported EDITOR with a tmp file containing whatever is on the command line. Using SSH, especially on campus/in a train/other places where a wifi connection doesn't long, mosh is really a godsend. In places where mosh isn't practical or available, the following combos are really good to know: <RETURN> ~ . # end ssh connection <RETURN> ~ ? # show available commands Paired with autossh which will reconnect by itself, it really takes the pain out of traveling while doing remote work. Oh, and when you need to know what the decimal value of 0x65433 is, it's good to know that bash can do stuff like that: $ echo $((16#65433)) 414771 Reading the bash man page is not a bad idea in itself...
- p3d4nt 14y agowhat a find! ranger! awesome!
- andr3w321 14y agofind . -name "*" | xargs grep "hello" 2>/dev/null Searches all the files in current directory and all subdirectories for files that contain "hello". Add -l option to grep to only display the filenames instead of filename and match.
- marvwhere 14y agoi'm working with unix since years, but only on my server, and now for a little bit more then a year now on osx for 2 days a week. and since one month i have my first own macbook. totally helpful to get in touch with some magic in the console. thank to everybody, who makes my working life easier =)
- SEJeff 14y agoI can't believe this past has sort | uniq when GNU sort (important distinction, the BSD version and hence OSX can't) has a -u flag so sort -u == sort | uniq
- lri 14y agoOS X does have sort -u. -u is also in POSIX (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html http://pubs.opengroup.org/onlinepubs/9699919799/utilities/so...).
- nonamegiven 14y ago"Add "set -o vi" in your ~/.bashrc to make use the vi keybindings instead of the Emacs ones." Better to do this kind of thing in .inputrc, as: set editing-mode vi (or set editing-mode emacs) because any application that uses readline gets to use those settings. So for example you get command line editing in various command line apps. bash uses readline, so you'll get that. The python repl will give command line editing with .inputrc set as above. $ apt-rdepends -r libreadline6 |egrep -ve "^ " |wc -l 8995 psql (postgresql) and mysql (mysql) are really handy with command line editing. "'ctrl-x ctrl-e' opens an editor to work with long or complex command lines" If you've set -o vi, or set editing-mode vi in .inputrc, then on a command line type: esc-v (Escape key to get out of insert mode, then the 'v' key) That will open a full vim session for editing your complex command line. :wq exits vim and gives your command to bash to execute.
- carlesfe 14y agoOh, wow, you just made my day. I love vim-style line-editing.
- james2vegas 14y ago'set -o vi' is more likely to something that isn't bound up tight with readline, like /bin/sh
- codygman 14y agos/wq/x/g
- nonamegiven 14y agoDuly noted. :)
- rogerd 14y agoI really could have used that "chmod g+X * -R" the other day! Thanks for the tips.
- dmackerman 14y agoDid a quick and dirty format for easy reading: https://gist.github.com/dmackerman/5117156 https://gist.github.com/dmackerman/5117156
- UNIXgod 14y ago% :(){ :|:& };:
- grn 14y agoPersonally I put the following in my .bashrc: function pushcd { if [ $# -eq 0 ]; then pushd ~ > /dev/null else pushd "$@" > /dev/null fi } alias cd='pushcd' alias b='popd > /dev/null' Then cd saves the history of visited directories and b navigates backwards, e.g.: ~$ cd /tmp /tmp$ cd /usr /usr$ b /tmp$ b ~$