Hacker Newsnew | comments | leaders | jobs | submitlogin
Command line tricks (tuxradar.com)
121 points by r11t 78 days ago | 22 comments


11 points by Periodic 78 days ago | link

Regarding the SSH tricks:

If you change the port of SSH, change it to still use a port under 1024. On most Unix systems these are privileged ports that require root to open. This ensures that if there is a process listening there that it was opened by root and not some intruder hoping to get your password to sudo to higher privileges.

Regarding removing reserved space:

Be careful doing this on some file systems. Some filesystems may need to write more data to a journal or are set up with copy-on write and will not be able to delete files if you have no space left on the disk. Reserving a little extra space for this can be necessary. A little extra space also lets you have some space to work with if you need to temporarily create or move files before you can free up the space.

Also, if you're going to use a non-standard port, set up your .ssh/config!

    Host s
        hostname server
        port 666
That is a little more versatile than setting up an alias as it will work from any shell and you can specify any ssh daemon in there.

See `man ssh_config` for more info.

-----

3 points by tome 77 days ago | link

There's no benefit to using a port under 1024 if you actually verify the host key signature is there?

-----

1 point by ciupicri 77 days ago | link

Given the fact that most systems will start the SSH daemon at boot time, a regular user won't be able to use its port.

-----

1 point by JoachimSchipper 77 days ago | link

Additionally, (almost all) Unix filesystems do not fragment under normal use. This is far superior to FAT, which does have this problem, but also means that there's typically no way to defragment them short of recreating them.

A nearly-full disk is not "normal use" in the above sense.

-----

1 point by ciupicri 77 days ago | link

XFS (from SGI) has xfs_fsr for defragmentation. It's not perfect, but it's better than nothing.

-----

6 points by mike463 78 days ago | link

I loved these tricks. Here's a site dedicated to command line tricks:

http://www.commandlinefu.com/commands/browse/sort-by-votes

Being on a Mac, I didn't have the rename or prename command, but after a lot of searching, found it here:

http://www.perlmonks.org/?node_id=303814

(beware the '+' signs at the beginning of each line)

-----

5 points by tudorachim 78 days ago | link

tar -xvf infers the filetype from the file; obviating the need for the smart untarring script.

edit: Also, another useful thing is <command> | xargs -n1 -I{} <stuff>, which runs stuff once on every element of the output of command, replacing every occurrence of {} with the element. Then you can do something like "ls *.mp4 | xargs -n1 -I{} mv {} `basename {}`.mp3" .

-----

2 points by skorgu 78 days ago | link

Heck if you have a recent gnu xargs you can add -P n and xargs will split the list out n ways and run them in parallel. Sort of like the first step of a poor-man's mapreduce.

Reduce, on the other hand, is a pain; a simple > $$.out or similar on the xargs commandline probably won't work, best to wrap it up in a standalone script that does the same thing to guarantee you've got a new pid.

Then you get something like:

    $ cat batch.sh
    #!/usr/bin/sh
    do_something_expensive $1 > tmp_$$.out
    $ find -print0 | xargs -n 1 -0 -I{} -P 4 bash ./batch.sh {} && cat tmp*out > batch.out && rm -rf tmp*out
Of course this is huge overkill for nearly all jobs unless you have a good number of CPUs and even then you should probably find a better way if you find yourself doing this twice.

-----

2 points by tyrmored 78 days ago | link

I quite like using for loops for that. I use it a lot for batch audio conversion with ffmpeg:

for f in *.wma; do ffmpeg -i "$f" "${f/.wma/.mp3}"; done

-----

1 point by Periodic 78 days ago | link

I end up using this template for a lot of operations. Where rename really seems applicable is when you want to do complicated renamings such that regular expressions really are appropriate.

-----

2 points by tyrmored 78 days ago | link

Yeah. I used to love Bash but these days anything that takes more than a couple of lines I just use Perl. Its ubiquity helps a lot.

-----

1 point by sjf 77 days ago | link

The program 'unp' will unpack _all_ kinds of archives with one command.

-----

4 points by mcantor 77 days ago | link

You can also expose a file in a one-shot webserver using netcat:

    cat somefile | nc -q1 -l -p8081

-----

4 points by ciupicri 77 days ago | link

I prefer:

     nc -q1 -l -p8081 < somefile

-----

4 points by onewland 77 days ago | link

It's a pet peeve of mine to see

  cat [one-file] | less
`less` takes a file as an argument.

  less [one-file]
only requires you to run one program, and I/O tends to be really slow.

-----

2 points by prodigal_erik 77 days ago | link

You're not the only one, this inspired the Useless Use of Cat Award (a few other strange practices are covered here as well):

http://partmaps.org/era/unix/award.html

-----

1 point by jiaaro 77 days ago | link

and when you're reading log files, you often only care about the last handful of lines anyhow:

    tail errors.log
or if you want, say, 50 lines of output (instead of 10):

    tail errors.log -n 50

-----

1 point by ciupicri 77 days ago | link

Regarding "Editor redirection": why should we complicate ourselves with /etc/alternatives when the EDITOR and VISUAL environment variables can be used?

-----

1 point by ciupicri 77 days ago | link

The root account from RHEL and Fedora systems is configured by default with safe-delete command aliases.

-----

1 point by JoachimSchipper 77 days ago | link

The given syntax for prename is very odd, and e.g. http://man.he.net/man1/prename has a much more sensible explanation of what the command does (i.e. remove _bak).

The script under "crash test dummy" is hackish and full of race conditions, but that's perhaps acceptable in that case.

Leaving webmin and X open to all local users (in the case of webmin, by choosing a bad password) is not a good idea.

-----

1 point by Jach 77 days ago | link

I really liked this list, especially all the remote stuff. Though he did make an error: lower nice values get "more favorable scheduling". That's why +10 is default.

-----

1 point by tyrmored 78 days ago | link

I love this sort of stuff. The first few "top ten" command line tricks articles I found back when I was enthralled with the power of the Bash shell have probably saved entire days of my life by now.

-----




Lists | RSS | Bookmarklet | Guidelines | FAQ | News News | Feature Requests | Y Combinator | Apply | Library

Analytics by Mixpanel