9 ms·
What's the one Linux command you wish you knew years ago?
- chimeracoder 15y agoI'm not sure why, but it was a while before I ran across du -h. Not sure how I ever did without it! Oh, and "sudo !!". Definitely key, along with "& disown".
- re_todd 15y ago"!ssh" to run the most recent ssh command without having to type out the long list of arguments I use and the host name.
- steve-howard 15y agoIt helps to put your most frequent entries in ~/.ssh/config Host somewhere HostName somewhere.example.com User username IdentityFile ~/.ssh/somewhere_key.pub
- bnegreve 15y agoyes and do you know about proxycommand ? let you set up hops when you don't have direct access to a server. e.g. if you don't have direct access to serverB but have direct access to a serverA that has direct access to serverB put this in your ~/.ssh/config file host serverA hostname serverA.domainname.com host serverB hostname serverB.domainname.com ProxyCommand ssh serverA "nc %h %p" then typing ssh serverB will log you on serverB
- jberryman 15y agoI also use !$ all the time. It is replaced with the last argument to the previous command line
- mmaunder 15y ago100% agree with CTRL-R. It changed my life. Others that I love: screen, pdsh, perl -pi -e 's/find/replace/g' *.txt redis-cli's new ability to have piped in data be the last argument of a command. vim -o file1.txt file2.txt file3.txt (opening multiple files with vim) Using vim instead of less as a pager: cat file.txt | vim -
- steve-howard 15y agosed -i does the same thing as perl -pi. Though I've not encountered a system that doesn't have both.
- Someone 15y agosed's regular expressions are different from those perl uses (even when using the (¿gnu sed?) flag that tells sed to use extended regular expressions). Especially if you "live in perl", that can be a problem.
- michaelcampbell 15y agoDoes sed allow in-place file replacement, or do you need to redirect to a different file then cp/mv it over the original? I haven't used it in many years, but sed didn't used to allow this.
- derwiki 15y agosed -i 's/old/new/g' file
- 1010010101 15y agoNote: "cmds" is one or more s commands. For other commands, if it's a big file, I will just pipe through less (still no temp file) and then save the buffer (to overwrite the original file), instead of using this hack. A hack for in-place editing, for old school sed (no -i): sedi(){ case $# in 0) echo usage: sedi cmds file;; 2) sed -an ' '"$1"'; H; $!d; g; w '"$2"'' $2;; esac; }
- sixtofour 15y ago
- tikhonj 15y agoFor me it was definitely find. I went through a ton of contortions to replicate its behavior several times before somebody knowledgeable clued me in. Also, `` and $(). Find combined with one of these is great. One of my favorite one-liners (I even figured it out myself :)) is: cat `find . --name *.java` | wc -l
- sbov 15y agoNever thought to use ``. I always used xargs: find . -name *.java | xargs cat | wc -l
- cperciva 15y agoYour approach works better than backtick expansion, since it won't run into 'command line too long' issues; but it will still break on "weird" file names (e.g., file names with spaces in them). The correct way to do this is find . -name *.java -print0 | xargs -0 cat | wc -l
- ars 15y agoBTW, recent versions of linux don't have a fixed limit on command line length. The command line can be a large as available memory (for a single processes). (Although the ratio between command line length and allocated memory is not obvious since it depends on the number of words in the command line.)
- ralph 15y agoThat glob could cause trouble. find . -name '*.java' -print0 | xargs -0 cat | wc -l
- pyre 15y agoxargs can still run you into the 'too many args' issue. You'll want to use this: --max-args=max-args -n max-args Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.
- gregable 15y agoAwk. Actually a full programming language but i usually use it for quick one liners: http://gregable.com/2010/09/why-you-should-know-just-little-awk.html http://gregable.com/2010/09/why-you-should-know-just-little-...
- nikcub 15y agoevery developer should know sed and awk. it is amazing how many devs write cumbersome scripts or little programs to handle tasks that are built for sed and awk. my ~/bin folder has ~50 different little scripts that handle all sorts of small tasks.
- cop359 15y agoI don't really know a lot about this stuff, but my understanding is that Perl was more or less replaced sed and awk.
- caw 15y agoI code in Perl at my day job, and I still use awk and grep. Sometimes it's quicker to do it in awk than perl, especially when you're doing it in command line and have to figure out quotes. awk -F, '{print $3}' foo | xargs -I{} echo 'command -x -y -z {}' perl -e 'open F, "<foo"; while(<F>) { my($a,$b,$c,@rest) =split /,/; system("command -x -y -z $c"); }' I think I could do the split into an array and then take the third element, but I'm just trying to do an elementary example. When you want to do more things to this argument you necessarily have to grow this, and at one point I got to the point where I started having to escape quotes. That's rather difficult to read when you're just trying to do something quick in the command line and you mismatch a pair.
- ajays 15y agoInstead of opening the file "foo" by hand, you can use Perl's "awk mode" thusly: perl -lane 'system("command -x -y -z $F[2]")' foo
- 15y ago
- ethank 15y agolsof comes in handy to match pid to socket and thus things like apache proc to running MySQL process.
- rhizome31 15y agoMost of those commands are not specific to GNU/Linux and will work with *BSD, OS X, etc.
- sjs 15y ago"Hey guys check out these sweet Ubuntu commands!" ;-)
- gbog 15y agouptime. I had a phone screening itw with Google and it was the answer to one question. I answered sar, which is much more powerful, and top, but caller wasn't tech and I probably missed a checkbox.
- wazoox 15y agoI always use "w" instead. It give the same information as uptime plus some more (basically "who"), for much less typing.
- pvg 15y agoslabtop
- gnosis 15y agoThis looks interesting. But would it be useful to anyone who's not a kernel developer?
- cema 15y agodd
- Jim_Neath 15y agomytop
- deleted 15y ago[deleted]
- diiq 15y agoThat reddit thread is delightful, because it is full of wonderful commands that I didn't know, or rarely use, or had forgotten. But it's also terrifying, because it exposes how many people will post questions before using man.
- akavi 15y agoIf that is surprising to you, then much of human behaviour must utterly baffle you.
- p9idf 15y agoMany comments seem to be shell- and bash-related. Perhaps the thread exposes how difficult to read the bash manpage is. On my system, the bash 4.1 manpage is 41026 words long, or about 80 pages. Manpages are most useful to me when they are short and to-the-point so I can find what I am looking for before I lose interest in skimming through an 80 page book.
- pyre 15y agoHow would you make the bash manpage short and to the point, considering the complexity that it implements?
- p9idf 15y agoBy reducing the needless complexity that it implements. The purpose of a Unix shell is to interpret a simple language wrapping syscalls so the user can easily give instructions to the kernel. There are several such shells whose source code is smaller than the bash manpage. 13 pages of the 80 page manual are devoted to readline and programmable completion. Readline and programmable completion could be removed from the shell, and their functional equivalents moved to a more logical place in the terminal emulator. Plan 9 does this, and it works well, and people seem to like it and the flexibility it gives. 24 pages are devoted to builtin commands, many of which are unnecessary duplicates of regular commands, and several unnecessary altogether. 3 pages are about weird parameter expansions, all of which can be done more intuitively with sed, except for the array one, which can be done with awk. And there are many smaller things as well, like biffing or arithmetic evaluation, which can be done with dc or whatever. You could try moving history out of the shell and into the terminal emulator as well.
- samuel 15y agoset -o vi
- geekytenny 15y agoThis is useful for finding and killing a process. ps -A | grep some text in program name upon getting the pid kill -9 pid
- bnegreve 15y agoalso check killall programname
- owenmarshall 15y agoBe careful with killall -- BSD and Linux killall does what you say, but SysV killall kills every program on the machine. Most of us probably won't be executing killall on any HPUX or IRIX boxes, but it's still good to know the difference ;)
- aw3c2 15y agopkill pidof
- cpuddle 15y agoInstead of the first, try "pgrep".
- telemachos 15y agoInstead of kill -9 (a nuke), you can often get results with just kill, kill -INT or kill -HUP. (The advantage is that the "weaker" signals allow your runaway program to do some cleanup, but kill -9 doesn't.) There's a nice write-up and a function automating attempts to run increasingly strong signals on this site: http://web.archive.org/web/20080610070315/http://sial.org/howto/shell/kill-9/ http://web.archive.org/web/20080610070315/http://sial.org/ho... (Thank god for the wayback machine. That site was full of good Unix tips and tutorials, but it's gone now.)
- DaveChild 15y agoIt's still there, just without a domain: http://72.14.189.113/ http://72.14.189.113/
- sirn 15y agoFor me, netcat. I mostly use it for * Copying files across machines (`cat file| nc -l port` and `nc host port`) * Remote pasteboard (on OSX w/ pbcopy, `while true; do nc -l port| pbcopy; done`) Not a life changing, but I wish I know it earlier. Only if there's a way to make "remote $EDITOR" with Netcat...
- mmaunder 15y agoActually the new top comment blew me away. I'm in the 20 year veteran category and was also oblivious. Quoted: disown - bash built in. You know how you always start that mega long rsync in an ssh session from your laptop and then realize you have to go offline halfway through? You forgot to start it in screen or nohup, didn't you? You can pause it (ctrl+z), background it (bg), and then disown it so it is protected from SIGHUP when you quit your ssh session. Bash (and, to be fair, zsh and others have copied much of this) has some wonderful job control features most people are completely oblivious of - even veterans of 20 years.
- sgt 15y agodisown is great. However, I don't know if it's possible to start up a new shell, then bring back that process to the foreground. 'jobs' will return no entries, and fg doesn't take a PID parameter. Any ideas? It would be brilliant if that were possible with bash alone.
- pja 15y agoreptyr: https://github.com/nelhage/reptyr https://github.com/nelhage/reptyr is Linux specific & involves a fairly gruesome abuse of ptrace() to attach a running process to your current pty. It won't put the process in the shell jobs list though. There's also retty: http://pasky.or.cz/dev/retty/ http://pasky.or.cz/dev/retty/ but again it's not a complete solution.
- xyzzyb 15y agodisown -h will (reportedly) keep it under job control.
- pja 15y agoTrue, but doesn't help if you've quit the shell & want to re-attach to the process after logging back in again :(
- tel 15y agoScreen is the only real answer, I've ever found.
- helper 15y ago#proxy from port 80 to port 3001 socat TCP-LISTEN:80,fork TCP:localhost:3001
- spudlyo 15y agoGreat program, socat is like netcat, but with a lot more features.
- sarnowski 15y agoctrl+d was an amazing improvement when my boss showed it to me years ago. Now I discover that a lot of people don't know it.
- roadnottaken 15y agoto logout or to delete characters?
- 1010010101 15y agosed -n '/./{ /beg/,/end/p;}'
- PascalW 15y agoNot really a command but this: mv /path/to/some/file.{jpg,png} Has been a huge timesaver. It's the same as mv /path/to/some/file.jpg /path/to/some/file.png But saves you the typing/copy pasting the path for the second argument. Can also be used with cp etc.
- deleted 15y ago[deleted]
- bgaluszka 15y agoEither that or rename jpg png /path/to/some/file.jpg
- robertskmiles 15y agoI didn't know you could do that with rename. I would have done rename 's/jpg/png/g' /path/to/some/file.jpg
- tweakism 15y agoThese are actually two different "rename" commands. bgaluszka's version is usually found on Fedora/RHEL/etc. systems coming from the util-linux package, while your version is usually found on Debian/Ubuntu/etc. systems coming from the Perl package. I prefer the regex one, of course, especially since it takes proper Perl regexes :) You have to be careful of this though and probably avoid using this command in shell scripts. edit: You can just stick Debian's rename at $HOME/bin/rename.pl and call it a day; this command is useful in the terminal since it's less typing than a for loop.
- sjs 15y agoBrace expansion is bad ass. http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion http://www.gnu.org/software/bash/manual/bashref.html#Brace-E... And it's even better in zsh. http://zsh.sourceforge.net/Doc/Release/Expansion.html#Brace-Expansion http://zsh.sourceforge.net/Doc/Release/Expansion.html#Brace-...
- 15y ago
- dkubb 15y agopv - very useful to put in the middle of a series of piped commands to provide feedback on what, if anything, is happening. I could've used this many times to catch cases where the amount of work being performed didn't match my expectations; usually an indication of an incorrect assumption or mistake I had made. Instead I sat there waiting for something way longer than I should've, only because I had a bad feedback loop.
- mrb 15y agoNobody knows this: the bash extension <(cmd) to pass the output of a command, as if it was a file, to another command. Example to diff the list of files in 2 directories (a and b) without using temporary files: $ diff -u <(cd a && find . | sort) <(cd b && find . | sort) Internally, bash creates a pipe and passes /dev/fd/XXX to the main command.
- sixtofour 15y agoJust diff the two directories. $ ls a b a: 1 2 3 4 b: 1 2 3 # Yours: $ diff -u <(cd a && find . | sort) <(cd b && find . | sort) --- /dev/fd/63 2011-11-20 02:50:21.919072726 -0700 +++ /dev/fd/62 2011-11-20 02:50:21.919072726 -0700 @@ -2,4 +2,3 @@ ./1 ./2 ./3 -./4 # Mine: $ diff -u a b diff -u a/1 b/1 --- a/1 2011-11-20 02:43:57.073130007 -0700 +++ b/1 2011-11-20 02:45:14.535912768 -0700 @@ -1 +1 @@ -1 +11 Only in a: 4 Maybe I'm missing something?
- jav 15y agoThe OP's example is diffing listings of the two directories whereas your command is asking diff to directly work on the directories themselves.
- robertskmiles 15y agoWhich means for example you could get that listing in any format, so you could for example spot changes in permissions or ownership if you included it in the listing.
- sixtofour 15y agoI stand enlightened.
- super_mario 15y agoYes, you are because the example was contrived. The point is that process substitution (as it is called) is available on systems that support named pipes. You could think of better uses for it.
- ryan-allen 15y agostrace - for looking a bit deeper into what's going on with live processes when things go awry.
- t_hozumi 15y agodu -sh ./D* 872K ./Desktop 78M ./Documents 3.2G ./Downloads
- scrrr 15y agoMy method to learn Linux/OSX command line. Step 1: Think "I wish I could do X." Step 2: Google/duckduckgo "How to do X in bash" Step 3: Delight that a simple solution already exists. This method is successful in 95% of all cases.
- dhughes 15y agoThis post and probably your comment will be in the results , gotta love that.
- Maven911 15y agowatch for not having to repeatdly type a comnand, usually for an sw app stat for checking when a files been accessed tee piping to a log file with tee, while still seeing normal stdout on screen
- philjackson 15y agoThe "^[." (esc-.) keybinding. In zsh it maps to insert-last-word which allows you to do this: echo hi > /tmp/blah.txt ls -l <esc-.> There's a bunch of complementary commands to go with it too.
- sjs 15y agoIs this only in zsh? I've been recommending this and not marking it explicitly as a zsh thing. alt-. works too. In the shell ESC <foo> is the same as alt-<foo>. The difference being that with alt-<foo> you hold alt and press <foo>, then release them both. with ESC <foo> you press escape, release, and then press and release <foo>.
- philjackson 15y agoIt's bash too.
- buster 15y agoQuitting SSH connections that hang: ~. If you frequently connect through VPNs to other hosts, often enough the SSH connection times out and just siits there, taking no commands. Hitting ~ and . will kill the session (it's an openssh feature). Other frequently used tools: awk, sort, uniq, tail, find, grep.. they alone make the shell a really powerful tool
- shabble 15y agoit's important to note that this needs to be preceeded by a newline to work. so: <ret>~? (list possible escapes) <ret>~. (disconnect session) If you're dumping binary data through ssh as a pipe, this can sometimes bite you if these sequences appear in your data. ssh -oEscapeChar=none ... handles that situation. (for the gory details, man ssh_config)
- matheusalmeida 15y agoIt's not a command but it is still useful. When you are typing a long command and you realize that you need to do other command or set of commands first, usually you type ctrl + c to cancel it. By default it does not save in the history. If you put # (comment the command) in front of the command (you can do it quickly with Ctrl + a #) and then you can use Ctrl + r to get it back. I'm sure there are other ways to save the command in the history but it is a simple way to have the same effect.
- telemachos 15y agoWhat I usually do in that situation is Ctrl + u (delete all the way back to the start of the line), then type the intermediate command, then Ctrl + y (place the deleted partial line back on the prompt, with my cursor at the end where it was). This assume Emacs-style bindings for the shell.
- reinhardt 15y agoI just put a random character instead of # to get "command not found".
- sjs 15y ago# seems safer... what if you're typing a mv command on a BSD machine that also has GNU binutils and the random character you hit is a "g"? Bad news.
- sjs 15y agoFor those using zsh hit alt-q to stash the current command. After you run the next command your stashed line will be brought back for you. alt-h behaves similarly but runs `man <command you were typing>`, so if you have this: $ rsync - and need to check the options, hit alt-h and the man page for rsync comes up. When you exit you'll be back at: $ rsync -
- joshcorbin 15y agoEven better: just type \ (line continuation) at point that you realize that you need to do something else first. Then hit Ctrl-c to cancel once you get to the new line.
- antimora 15y agothese are great ones! But here is mine that I recently learned: sort -h compare human readable numbers (e.g., 2K 1G)
- joelthelion 15y agoCanonical example: du -h | sort -h
- caw 15y agoSort to the same output file: sort -o foo.txt foo.txt You can't do sort foo > foo because you'll be left with a blank file.
- Karellen 15y agosponge(1) from "moreutils" Although sort(1) handles this for you as you mention, the general problem in the shell of wanting to overwrite your input file with the output file, but not being able to redirect to it for the reason you mention, is solved with sponge. Rather than: $ grep "foo" bar.txt >baz.txt; mv baz.txt bar.txt instead: $ grep "foo" bar.txt | sponge bar.txt
- jasiek 15y agopushd / popd
- josch 15y agoalso cd - which goes back to the last directory you cd'd from.
- Pelayo 15y agoscreen Saved my work on remote servers too many times to count. :)
- ludwigvan 15y agoemacs --daemon (or after you start emacs, M-x server-start). This starts up the emacs server, so that it preserves your open files, and you can start new emacs instances immediately with emacsclient. (I actually symlink emacsclient to vi since it loads as fast as vi, which was one of the weakest points of emacs for me.) Very useful if you are using a remote box (like a vps) as your dev environment.
- pja 15y agoIn my .bashrc I have: export ALTERNATE_EDITOR='' alias e='emacsclient -t' Now $ e file.txt will fire up emacsclient to connect to a running emacs daemon. If there is no emacs daemon running it will start one for you and then connect to it. You can export VISUAL="emacsclient -t" export EDITOR='emacsclient -t' for good measure if you like.
- super_mario 15y agoAnd if you are VIM user, you can do the same :D: $ vim -h | grep -i server --remote <files> Edit <files> in a Vim server if possible --remote-silent <files> Same, don't complain if there is no server --remote-wait-silent <files> Same, don't complain if there is no server --remote-send <keys> Send <keys> to a Vim server and exit --remote-expr <expr> Evaluate <expr> in a Vim server and print result --serverlist List available Vim server names and exit --servername <name> Send to/become the Vim server <name> $
- djKianoosh 15y agozless, zgrep, zcat... Only recently found out about these and now I love them
- brown9-2 15y agoGNU Parallel: http://www.gnu.org/s/parallel/ http://www.gnu.org/s/parallel/
- veyron 15y agoThe way uniq should have been implemented: awk '!x[$1]++'
- phzbOx 15y agoFor me, it was using 'cd' without argument. (It takes you back to your ~). Also, learning the various options of grep (-v, -i, -A, -B) made its use really more powerful.
- redler 15y agoA useful one I somehow missed early on is cd - to change back to whichever directory you were just in. Every once in a while most of us find ourselves facing someone's "how could you not know that?" look.
- gnosis 15y agoAlso along these lines are the directory stack manipulation commands: "pushd", "popd", and "dirs" -- along with some related environment variables.
- Aissen 15y ago!$ (short for !!:$) Passes last argument on the command line. In general, the whole paragraph on HISTORY EXPANSION in the bash manual is a must read. A few ones from the moreutils package: - vipe : manually edit the data in the middle of a pipe. - vidir : treat a directory and its files as a text file. Does renames and deletions. - sponge : when you're doing modification on a file in a pipe, and want the output to be to this file (file will be scrambled without sponge). It has others, very useful tools, but I won't spoil them all.
- sjs 15y agoalt-. or (ESC .) does something similar, but can be pressed repeatedly to keep going back further in history in case the argument you want isn't on the most recent command. !! is short for the previous command, but you can do !-2$ to get the last argument from the 2nd last command, or !cp$ to get the last argument from the most recent cp command. Or !cp:2 to get the 2nd argument to the most recent cp command. !# refers to the current command being entered (in zsh, don't know about bash). Basically I second Aissen's recommendation to read up on history expansion. Super useful.
- MPSimmons 15y agoI wish I would have known about dmidecode long before I found out about it.
- gnosis 15y agoAlong these lines there are also: lspci, lshw, and procinfo.
- 1point2 15y agoThe one that starts the system up - still forget what it is - but I know it lives in /boot.
- yewtree 15y agotree
- wackfordjf3 15y agoIn bash: cp /dir/dir/dir/dir/filename !#:^:h/newfilename (Applies to more than cp) Strips cp & repeats previous dir tree minus /filename, plus /newfilename. This can be mimicked by brace expansion: cp /dir/dir/dir/dir/{filename,newfilename}
- Tichy 15y agoI wonder, how do you remember all the command line options? Recently I had this thought: maybe those people who can remember the options actually are autistic (don't know the proper word, that thing many geeks are deemed to be), and I am not. Remembering command line options might be like knowing large prime numbers within seconds. But I am also interested in memory techniques like the Loki method. Perhaps some good system for memorizing command line arguments could be devised...
- ilikejam 15y agoUmm, we just use the commands a lot. Doing something 12 hours a day for 10 years tends to leave some imprints on your memory.
- jasiek 15y agopython -m json.tool < unformatted.json > formatted.json edit: requires simplejson