r/UnixProTips • • Feb 05 '15

Fast switching between two locations: "cd -"

48 Upvotes

"cd -" allows you to go to the previous location you were. For instance:

cd ~/Desktop
cd ~/long/complicated/different/path
cd -  #you are back in ~/Destkop
cd - #you are back in ~/long/complicated/different/path

r/UnixProTips • • Feb 04 '15

alias fuck="sudo !!"

42 Upvotes

r/UnixProTips • • Feb 11 '15

[bash] Shortcuts For Maximum Productivity

Thumbnail
skorks.com
32 Upvotes

r/UnixProTips • • Feb 11 '15

Get the difference between the output of any commands

30 Upvotes

diff <(command1) <(command2)

diff <(ls dir1) <(ls dir2)


r/UnixProTips • • Feb 05 '15

Check the top 10 commands you use

30 Upvotes

history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10


r/UnixProTips • • Feb 16 '15

Github's collection of dotfiles repos & tutorials

29 Upvotes

I'm one of the maintainers, so I'm a bit biased, but I think that the collection of dotfiles repositories & tutorials at https://dotfiles.github.io/ is a pretty good starting point.


r/UnixProTips • • Jun 03 '15

read markdown files like man pages

28 Upvotes

add this in your shell aliases:

# read markdown files like manpages
mdman() {
    grep -v "\-\-\-\-\-" "$*" | pandoc -s -f markdown -t man | groff -T utf8 -man | less
}

then use it like:

mdman ~/file.md

r/UnixProTips • • Feb 05 '15

Add space before the command and it won't be saved to your history.

26 Upvotes

r/UnixProTips • • Feb 04 '15

Use Control-t in bash to fix a typo

26 Upvotes

It's a simple but I find often unused bash shortcut. It swaps the two characters to the left of the cursor. For example, if your fingers get crossed typing "sudo"

foo@bar:~$ suod

Just mash Control-t and it's magically

foo@bar:~$ sudo

For bonus points, use Alt-t to do this with the last two words too


r/UnixProTips • • Feb 08 '15

Use ShellCheck to check shell scripts!

24 Upvotes

ShellCheck is a static analysis tool for sh/bash scripts. It detects a wide range of common errors and pitfalls. It is written in Haskell and you can install a local version (it is packaged in Debian/Ubuntu as shellcheck), it should work well with Emacs' flyspell or vim's syntastic.

See also the list of Bash pitfalls: http://mywiki.wooledge.org/BashPitfalls


r/UnixProTips • • Feb 07 '15

Use the command "set -o vi" to for vi keybindings on the commandline

23 Upvotes

When typing a command, you can hit "escape" and then use hjkl and vi keys to make edits. Super useful for editing long commands.

Edit: This doesn't work on every shell, but it does work with ash/bash.


r/UnixProTips • • Feb 11 '15

Over 1,000 readers! :-)

19 Upvotes

Wow. Thank you all, I never thought it would reach 500 let alone 1000! Keep posting your tips! Thanks again


r/UnixProTips • • Feb 05 '15

Ctrl + z = SIGTSTP

18 Upvotes

This is the ultimate trick you need for your everyday use of terminals.

Pressing Ctrl+z sends SIGTSTP to the current foreground job, and this causes the process to suspend in the background without losing any data, kicking you back to the shell. This allows you to, for example, suspend Vim for a while to check some other stuffs in the shell while remaining on the same terminal.

To bring suspended jobs back to the foreground, type fg in the shell. More details can be found in "Job Control" or "Jobs" section in the manpage of your shell.


r/UnixProTips • • Feb 08 '15

Use <<< to treat strings as files Ex: less <<< "Hello Unix!"

Thumbnail linux.die.net
14 Upvotes

r/UnixProTips • • Feb 08 '15

ls: display file size with thousands separator

14 Upvotes

The file or block size in ls and df, du, ... can become quite unreadable:

$ ls -l dat*
-rw-r--r-- 1 fritz fritz 747571797 Jan 23 03:13 dat1
-rw-r--r-- 1 fritz fritz 769838509 Jan 23 20:57 dat2
-rw-r--r-- 1 fritz fritz 736028643 Jan 23 21:34 dat3
-rw-r--r-- 1 fritz fritz 733700320 Jan 23 21:39 dat4
-rw-r--r-- 1 fritz fritz 710093303 Jan 23 21:56 dat5
-rw-r--r-- 1 fritz fritz 752512339 Jan 23 23:15 dat6

The GNU coreutils support showing thousands separator by adding a ' to the block size.

$ ls -l --block-size=\'1 dat*
-rw-r--r-- 1 fritz fritz 747,571,797 Jan 23 03:13 dat1
-rw-r--r-- 1 fritz fritz 769,838,509 Jan 23 20:57 dat2
-rw-r--r-- 1 fritz fritz 736,028,643 Jan 23 21:34 dat3
-rw-r--r-- 1 fritz fritz 733,700,320 Jan 23 21:39 dat4
-rw-r--r-- 1 fritz fritz 710,093,303 Jan 23 21:56 dat5
-rw-r--r-- 1 fritz fritz 752,512,339 Jan 23 23:15 dat6

The actual separator depends on the LC_NUMERIC locale. The block size can also be specified by setting either the tool specific LS_BLOCK_SIZE or the general BLOCK_SIZE environment variables. Alternatively an alias can be used:

alias ls="ls --block-size=\'1 --color=auto"

(edit: With --color=auto the output will use colors on terminal that support it. Thanks to /u/pie-n)

See the (coreutils) Block size info page for more information.


I'd also like to take the opportunity to point people interested in Linux programming to /r/linux_programming


r/UnixProTips • • Jan 13 '21

Shell UPT of the Day: Modify your HISTCONTROL to have "ignorespace" to ignore all commands that start with a space in your history.

12 Upvotes

I find this super useful if you're entering a password or key in some command. Furthermore, I've heard that a lot of containerized package managers still give read access to your home directory - so in theory, all these applications have access to your whole history.

Bob.


r/UnixProTips • • Mar 06 '15

TIL in bash scripts IFS=$'\n'

12 Upvotes

Without:

for x in $(echo "/tmp/wee1 2.csv"); do
    echo $x
done

/tmp/wee1

2.csv

With:

IFS=$'\n'; for x in $(echo "/tmp/wee1 2.csv"); do
    echo $x
done

/tmp/wee1 2.csv

From the docs:

$IFS

internal field separator

This variable determines how Bash recognizes fields, or word boundaries, when it interprets character strings.

$IFS defaults to whitespace (space, tab, and newline), but may be changed, for example, to parse a comma-separated data file. Note that $* uses the first character held in $IFS.


r/UnixProTips • • May 09 '15

[most shells] Quickly unmount the last device you mounted

11 Upvotes

I usually save keystrokes by typing:

u!mo

In the rare case you used another command starting with 'mo' after the mount, use progressively longer fractions of 'mount' after the ! to disambiguate. Works in [t]csh and [ba]sh.


r/UnixProTips • • Feb 18 '15

'free' and Memory Utilization - a short article I wrote about Unix's free command

Thumbnail blog.chewbonga.com
11 Upvotes

r/UnixProTips • • Feb 12 '15

[bash] Extended Globs: Advanced pattern matching.

11 Upvotes

Bash supports extended globs, for more convenient pattern expansion. You have to enable them using

shopt -s extglob

Then you can do fancy things like get all files that do not match a specific pattern. E.g., all files that do not end in .bu:

ls !(*.bu)

The following additional patterns are available

  • ?(PATTERN-LIST) Matches zero or one occurrence of the given patterns.

  • *(PATTERN-LIST) Matches zero or more occurrences of the given patterns.

  • +(PATTERN-LIST) Matches one or more occurrences of the given patterns.

  • @(PATTERN-LIST) Matches one of the given patterns.

  • !(PATTERN-LIST) Matches anything except one of the given patterns.

and they can of course be nested.

See

for more information. To disable them again use shopt -u extglob.


r/UnixProTips • • Feb 07 '15

Please read before posting.

11 Upvotes

This subreddit is an open place for users of all levels but we need to be mindful of some things that are posted. I'll do my best to illustrate a few guidelines below.

 

  • What is a Unix pro tip? I'm trying to be pretty easy going about this, but it's essentially any command or combination of commands that make using your preferred Unix-like OS easier, interesting, or fun. It's important that you have put some thought into it and it should not run the risk of damaging a users system. Malicious or ill-informed posts will be removed. Again, this is a place for learning and I don't want to be too tough about this. Be mindful of what you post.

 

  • What is not a Unix pro tip? A Unix pro tip is not just simply a command to be run in the shell. Some examples of what is not a Unix pro tip include:

 

Run this command to see processes on your system

ps aux

or

Run this command to get root

su

 

 

Posts with incorrect commands in the title will be removed unless there is sufficient clarification of the correct syntax, in the comments, for it to warrant its presence on the sub. Be thoughtful of other people and the fact that some people may simply run the commands without thinking about them. If you are not sure of the command(s) you are posting then do not post them.

 

Use reddit formatting for your posts, in particular the 4 spaces formatting tip(which can be found by clicking on the 'formatting help' below the submission entry box). It helps to make your posts clear and easier to understand. It's important for subreddits of this nature. Please use it.

 

If your post is specific to a certain Linux distribution, desktop environment , shell etc. please put a tag in the header of your post. Something such as [Debian], [Gnome] or [bash] will do.

 

This post may be updated from time to time so keep an eye on it if you are unsure about the rules of this subreddit. Thanks!


r/UnixProTips • • Feb 03 '15

Hello!

11 Upvotes

Well hopefully this place gets some subscribers and I'm not doing all this for nothing. :-)

The idea is simple. Reddit has Unix pro tips everywhere! Instead of constantly saving threads or using the search function to try to find that one super awesome tip you once found.. we have a place for them now. Here! So post your own ones or other ones you have found and hopefully this place will grow and serve a purpose.

I am open to suggestions, criticism and help. Thanks. :-)

edit: Might be a good idea to also include a header of some sort if your tip is specifically related to Linux/BSD or one of the sub flavours of either.


r/UnixProTips • • Feb 10 '23

Plan 9 Desktop Guide

Thumbnail pspodcasting.net
10 Upvotes

r/UnixProTips • • Aug 11 '15

Using for at the command line to ping multiple adresses.

10 Upvotes

for x in 1 2 3 4 5 6 7 8 9 10; do ping 192.168.1."$x"; done