Pages tagged with bash

I'm still in the process of tagging pages that were migrated from the old site, so for now you're best off using the search engine rather than relying on the tags.

  • Bash script to check SSH connection - You can use the following bash script to check whether an SSH connection is working. I run it via a cron job so that I receive an email alert when there’s a problem with a server. #!/bin/bash ssh -q -o BatchMode=yes -o ConnectTimeout=10 example.com e...
  • Check directory is mounted - To check that a directory is mounted in a bash script: echo "Mounting /mnt/share..." sudo mount /mnt/share if ! mount | grep '/mnt/share ' > /dev/null; then echo "/mnt/share not mounted. Exiting." exit fi # Do stuff with /mnt/share echo "Unm...
  • Testing for a non-interactive shell - I had some backup scripts that used ssh and scp but reported the following warning to stderr: stdin: is not a tty If you receive this error then it’s likely that your .bashrc file (or another terminal init file, on the server side) is trying to do ...
  • Redirect stdout and stderr to a file - From Redirect stdout & stderr and append to a file. It is possible to redirect one output channel to another like “2>&1” which means “put the output of channel 2 (stderr) where right now channel 1 (stdout) goes” and let channel 1 point to a...
  • Remove old files by date in filename - I have backup scripts that create files with the backup date in their filename, in ISO 8601 format, as generated by date -I, as follows: mirrors.tgz-2015-12-31 This is year-month-day: mirrors.tgz-Y-m-d Every month I want to delete all old backu...
  • Find target given a symlink - To find the target of a symbolic link i.e. the real file or directory that your symlink points to… realpath [symlink] Or: readlink -f [symlink] This can be useful in shared hosting environments where your home or html directory is a link but so...
  • Bash - Notes about Bash. Run command on each line in a file Run the [command] on each line in a [file]: while read l; do [command] $l & done < [file] This will run [command] [line n] & for every line in [file], where [line n] is the next line...
  • Join text files with filenames - You can use cat to join the contents of files. For example, this will create a file called tmpfile that contains the contents of all .txt files in the current directory: cat *.txt > tmpfile Use >> to append to tmpfile rather than overwriting...
  • How to detatch a program from the terminal - Sometimes, when you start a program from the terminal, closing the terminal will also close the program. Even if you start the program in the background. vlc does this. E.g. Start vlc with vlc &, close the terminal and vlc will also close. To prev...
  • How to use a regex in a bash conditional - Use the double bracket syntax to use regular expressions (aka regex or regexp) in bash conditionals: me@pc ~/tmp/bashregextest $ if [[ "foo/bar" =~ ^foo ]]; then echo 'yes'; else echo 'no'; fi yes me@pc ~/tmp/bashregextest $ if [[ "foo/bar" =~ ^bar ]]...
  • Work with filenames listed in a file - If you have a text file with a number of line-seperated filenames, you can do the following to manipulate them: cat thefile.txt | while read line; do ls "$line"; done That example will simply run ls on each filename in the text file.
  • Bashrc for root - On Gentoo (and perhaps other distributions), root doesn’t have the normal bash startup configuration files. If you want to use a ~/.bashrc file to load e.g. ~/.bash_aliases and ~/.bash_functions, then you’ll need a ~/.bash_profile file to kick everythi...
  • Bash keyboard shortcuts - Some keyboard shortcuts and commands for the Bash. Key sequence Action ctrl-c Kill current process ctrl-z Send current process to background fg return Restore...
  • Hiding stderr or stdout - To hide the standard error (stderr) or standard output (stdout) messages produced by a program, redirect them to /dev/null, a special file that swallows all data written to it. Hide stdout: the_program > /dev/null (Shorthand for the_program 1&g...
  • Working with filenames in bash - Bash has powerful shell parameter expansion capabilities, which we can use along with a couple of functions to deconstruct filenames. To get the directory of a file, use dirname: me@pc ~/tmp $ dirname /home/steph/tmp/test.txt /home/steph/tmp To ge...
  • Terminating or killing a process - Send a TERM signal (SIGTERM) to the process to terminate gracefully, by requesting that it do so. kill -TERM pid Or: kill -15 pid Where pid is the process identifier. Send a KILL signal to force the termination: kill -KILL pid Or: kill -9...

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top