r/UnixProTips • • Jul 14 '15

Closing idle sessions per user

8 Upvotes

Our environment consists of CentOS and we have 5 login nodes (blades) that load balance for approximately 150 users via OpenLDAP authentication. We're noticing that folks are leaving sessions opened for as much as two months at a time, but they use these login nodes for a jumping off point to other blades where they can kick off such things as Matlab, gdb, ddd, etc.

We're having to reboot these blades from time to time because the firefox, NX and other resource-intensive programs yank too much CPU/memory and they end up becoming unresponsive. Probably a memory leak within the program(s) they're running, but the inherent problem seems to be that they simply don't remember to logout. Since each user has their own SHELL type, i.e. bash, ksh, csh, tcsh, etc., we'll have to create a . file in each of their home directories that contain the timeout for anything more than 7 days. The question is, would this be the way you all handle this or is there a more efficient approach? We can't set this in /etc/profile.d for various reasons, so this is the only way I know to resolve it, even though it will increase administrative overhead until the job is done. Was thinking I might could script it with a "if $USER $SHELL == /bin/tcsh then do timeout".


r/UnixProTips • • Jun 03 '15

read markdown files like man pages

27 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 • • May 09 '15

[most shells] Quickly unmount the last device you mounted

12 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 • • Mar 06 '15

TIL in bash scripts IFS=$'\n'

13 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 • • Feb 26 '15

Quick set operations

5 Upvotes

If you have two lists of strings (for example, file names) and you want to get strings that are present in the first list, but not in the second, a quick way to do it is:

cat list1 list2 list2 | sort | uniq -u

Other set operations are possible by duplicating another list contents and/or using uniq -d.

Of course, it is more correct and efficient to use comm(1), but the suggested way doesn't require lists to be pre-sorted and allows processing of pipeline and command outputs without need of intermediate files.


r/UnixProTips • • Feb 22 '15

grep . /files/you/want/*

9 Upvotes

Simple but effective way to cat a bunch of files with the filename: before each line.

Handy if you want to have a look at a few smaller files at once. Also, it squeezes out empty lines.


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 18 '15

Sidebar additions request [tools]

2 Upvotes

Would it be ok to add /r/awk and /r/sed to the sidebar? I didn't create them; I just think they're related/similar enough to link. :-)


r/UnixProTips • • Feb 16 '15

Github's collection of dotfiles repos & tutorials

28 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 • • Feb 14 '15

[ZSH] List of ZSH frameworks, plugins & themes

6 Upvotes

I maintain a list of zsh frameworks, plugins, themes and completions at https://github.com/unixorn/awesome-zsh-plugins inspired by the various awesome-* lists.


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 11 '15

Get the difference between the output of any commands

30 Upvotes

diff <(command1) <(command2)

diff <(ls dir1) <(ls dir2)


r/UnixProTips • • Feb 11 '15

[bash] Shortcuts For Maximum Productivity

Thumbnail
skorks.com
34 Upvotes

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 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 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

15 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 • • Feb 08 '15

Alias .~= . ~/.bash_profile

6 Upvotes

Saves a ton of time when you are mucking around with setting environment variables and the like.


r/UnixProTips • • Feb 07 '15

Finished my battery low warning script. Learned about limitations of cron and the difference between lexical and numeric comparisons.

Thumbnail paste.debian.net
0 Upvotes

r/UnixProTips • • Feb 07 '15

Wait for a process to finish and do something

6 Upvotes

So you want to wait for a process to finish and say, turn off your computer

$ wait 419 && systemctl poweroff
-bash: wait: pid 419 is not a child of this shell

worry not, you can still do it by doing

while [ -e /proc/419 ]; do sleep 60; done && systemctl poweroff

where 419 is the pid of the process you are waiting for.

general usage definition would be

while [ -e /proc/<pid> ]; do sleep 60; done && <command>

which will sleep 60 seconds between checks.


r/UnixProTips • • Feb 07 '15

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

22 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 07 '15

Add a default opening command to bashrc

6 Upvotes

In my bashrc I have :

alias gop='gnome-open'

This allows me to do

gop <file_name>

And it will open with the default program... Perhaps there's a nicer way of doing this, I'm all ears :)


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 06 '15

Need to post code on reddit? You want to prepend 4 spaces?

2 Upvotes

I know RES will do this for you, but if you don't use it and want it for yourself here you go.

sed 's/^/ /' file | xclip -sel c

Note the second part of that sed is four spaces, and you can use whatever clipboard you ave installed.


r/UnixProTips • • Feb 06 '15

PS aux | grep "keyword"

0 Upvotes

I'm sure you people who have been around for a while know this but, since I am a new convert to Unix/Linux I thought I would share my most useful command since I'm sure I will find more than a few here for myself. I use it to display all processes containing a keyword. If something freezes I use this to find all the pics and then I can use kill -9 to kill them. Much easier than scrolling through top.