Resizing images on Linux with gThumb is easy. However, I have a batch of images inside a folder. Manually resizing them will consume too much time. How to automatically resize them on Linux with a script? You can use convert from ImageMagick together with bash script to resize images inside a directory. mkdir resize; IFS=$(echo
Read more
Tag: Bash
How to detect whether an image is almost blank on Linux?
Posted onHow to detect whether a .jpg image is almost blank/black on Linux? Here is a bash function that I used to detect whether an image is black on Linux: function isBlank () { mean=`convert $1 -format “%[mean]” info:` echo “$mean” } It uses ImageMagic to generate formatted image characteristics (the mean). If the mean is
Read more
How to run gitbook on a headless server (make Calibre run in headless server)?
Posted onWhen use gitbook to generate ebook, Calibre reports this: RuntimeError: X server required. If you are running on a headless machine, use xvfb After xvfb is installed, it does not work either. How to make gitbook/Calibre work on a headless server? You need to wrap the command ebook-convert with xvfb-run. However, in gitbook (lib/generate/ebook/index.js), ebook-convert
Read more
How to change number of replications of certain files in HDFS?
Posted onThe HDFS has a configuration in hdfs-site.xml to set the global replication number of blocks with the “dfs.replication” property. However, there are some “hot” files that are access by many nodes. How to increase the number of blocks for these certain files in HDFS? You can the replication number of certain file to 10: hdfs
Read more
Filter away non-printable ASCII characters on Linux?
Posted onSometimes the commands’ output have some non-printable ASCII characters. How to filter away these non-printable ASCII characters on Linux? You can consider the problem as “how to leave only printable ascii characters?”. The printable characters in ASCII table are: Octal 011: Tab Octal 012: Line Feed Octal 015: Carriage Return Octal 040 to 176: printable
Read more
How to calculate the average of value in lines of a text file on Linux by a script?
Posted onHow to calculate the average of value in lines of a text file on Linux by a script? The file.txt is like: Run 0: Time used: 22.235331711 seconds. Run 1: Time used: 20.784491219 seconds. Run 2: Time used: 21.851638876 seconds. What I want it to calculate the average time. awk is handy for this purpose:
Read more
Bash comparison operators
Posted onWhat are all the bash supported comparison operators? The man 1 test contains all the operators supported in bash. An omitted EXPRESSION defaults to false. Otherwise, EXPRESSION is true or false and sets exit status. It is one of: ( EXPRESSION ) EXPRESSION is true ! EXPRESSION EXPRESSION is false EXPRESSION1 -a EXPRESSION2 both EXPRESSION1
Read more
How to set up the Java environment in Linux?
Posted onI am using Fedora 20. I installed the rpm from Oracle for the Oracle JDK. How to set up the environment so that the Java environment is Oracle JDK instead of the OpenJDK? The Oracle JDK is install to /usr/java/ directory. On my Fedora 20, it looks like this: $ ls /usr/java -l total 4
Read more
How to make Emacs run in command line by default?
Posted onHow to make Emacs run in command line by default? I do not want to open the X window. You have at least 2 choices. First choice: use emacs-nox and make it the default: # yum install emacs-nox # alternatives –config emacs and choose emacs-nox. Second choice: use emacs but with -nw to disable the
Read more
Get the number of files in a directory
Posted onHow to the number of files in a directory ls | wc -l works well for me. http://stackoverflow.com/questions/3702104/find-the-number-of-files-in-a-directory
How to print out text with line numbers in Linux
Posted onHow to print out a plain text file with line numbers in Linux? Use the nl command: nl text-file Find options in the nl manual.
Alternatives to goto in bash
Posted onAs we know: There is no goto statement support in bash. goto is useful for certain situations, especially for debugging. So, is there any alternative mechanisms to goto in bash? Robert Copeland gave an interesting hacking to this problem: http://bobcopeland.com/blog/2012/10/goto-in-bash/ The idea is like the self-modifying code. But the difference is to generate a piece
Read more
How to `cut` a String Using a String as the Delimiter in Bash?
Posted onIs is possible to cut in Linux using a string as the delimiter? cut –delimiter=”delim” -f 1,3 cut reports: cut: the delimiter must be a single character The most closest solution that I find is using awk/gawk: awk -F ‘delim’ ‘{print $1; print $3}’ From the manual: -F fs –field-separator fs Use fs for the
Read more
How to get a Makefile’s directory for including other Makefiles
Posted onI experience this problem: I have a file common.mk that includes release.mk which in the same directory. common.mk is included by the top-level Makefile. common.mk and release.mk are shared by multiple projects and the top-level Makefile may stay in different directories with {common,release}.mk. Now, include in make use the pwd as base directory. So, simply
Read more
Remove trailing spaces at the end of each line in a file
Posted onHow to remove trailing spaces at the end of each line in a file on Linux? Use this script with sed: cat your_file | sed ‘s/[[:blank:]]+$//g’
How to padding a integer with 0s in bash?
Posted onHow to convert an integer into a string of many characters padded with 0s in bash. To do this in C: sprintf(buffer, “%08d”, n); which convert integer n to a 8-character string padded with leading 0s. How to padding a integer with 0s in bash? In bash, you can also use printf For the previous
Read more
How to tell whether the shell is run in screen
Posted onHow to tell whether the shell is run in screen? For example, I run screen and it starts a bash environment. How to tell in the bash, whether it is run by screen or it is a normal bash environment? It can be identified by checking the TERM environment variable: In a bash in screen:
Read more
How to set an environment variable in bash?
Posted onHow to set an environment variable in bash? The syntax in bash for setting an environment variable is as follows. export VARIABLE=value Note that there is no space among the variable, the equals sign (“=”) and the value. If the value has spaces, the value should be put in quotes. To check it: echo $VARIABLE
Read more
How to set an environment variable in csh?
Posted onHow to set an environment variable in csh? To set an environment variable in csh: setenv VARIABLE value Most commands used to display/manipulate the environment variables for bash are also available for csh: https://www.systutorials.com/qa/476/how-to-set-an-environment-variable-in-bash
How to pipe the stderr to less
Posted onFor example, try to less the error messages generated by the compiler with make | less Use this command: make 2>&1 | less or, if you want stderr only: make 2>&1 >/dev/null | less