How to test whether PATH has a specific directory in it in Bash on Linux? For example, how to test whether /usr/local/mytool/bin is in $PATH? Directories in the $PATH variable are separated by :. So you can use a regular expression to test whether the $PATH contains a directory. The code for your specific example
Read more
Author: Eric Ma
Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.How to get another user’s PATH in Bash on Linux?
Posted onHow to get the $PATH environment variables of another user in Bash on Linux? The current user running the Bash script is root. If the scripts is run by root, this piece of code will store the PATH from the environment of the user user1: user1path=$(su – $user1 -c env | grep ^PATH= | cut
Read more
How to test a file or directory exists in Python?
Posted onIn Bash, the [ -f ] and [ -d ] tests can test whether a file or a directory exist. What are the corresponding python method for these tests? For -f: **os.path.isfile(path)** Return True if path is an existing regular file. This follows symbolic links. For -d: **os.path.isdir(path)** Return True if path is an existing
Read more
How to get file extension in JavaScript?
Posted onHow to get file extension from a file name in JavaScript? For examples, For “file.txt”, I want to get “txt”. For “file2.multi.ext.fileext”, I want to get “fileext”. This JavaScript function works well for me. function getExt(filename) { var idx = filename.lastIndexOf(‘.’); // handle cases like, .htaccess, filename return (idx < 1) ? “” : filename.substr(idx
Read more
How to scp multiple files from remote to local on Linux?
Posted onWith scp, copying several files such as file1 file2 to remote site can be passed by command line like $ scp file1 file2 user@remote: But how to scp multiple files from remote to local on Linux? You can do similar things by at least 2 method with scp: $ scp user@remote:file{1,2} ./ $ scp user@remote:”file1
Read more
How to move cursor to a specific byte in Vim?
Posted onIn Vim, ’20G’ moves the cursor to line 20 in command mode. But how to move the cursor to a specific byte in Vim? In normal mode, 20go will move the cursor to byte 20. Or in command line, :goto 20 or :go 20 From vimdoc: :[range]go[to] [count] [count]go Go to {count} byte in the
Read more
How to test whether a user already exist on Linux?
Posted onHow to test whether a user account, say linuxuser, already exist on Linux? You may make use of id which tries to get user IDs. The Bash code snippet is as follows. user=hello id -u $user >/dev/null 2>&1 if [ “$?” == “0” ]; then echo “user $user already exist.” else echo “user $user doesn’t
Read more
How to get the script’s own path in sourced Bash script?
Posted onIn normal Bash script, $0 is the path to the script. However, when a script is sourced, such as . a.sh a.sh’s $0 does not give a.sh. How to get the a.sh inside a.sh? Short answer: use ${BASH_SOURCE[0]}. You can check this post for details and explanations: How to Get Bash Script’s Own Path.
How to get a user’s home directory from the username in a variables in Bash?
Posted onIn Bash, ~username gives the username user’s home. However, this does not work for situations where username is in a variable such as ~$user How to get the home directory from the user name in a variable in Bash? You can use this Bash script snippet: userhome=$(eval echo ~$user) Here, $user get evaluated to its
Read more
How to enable natual scroll of touchpad in MATE on Linux?
Posted onThere are options for Gnome 3 and Cinnamon to make touch pad “natual scroll”. But in MATE, there is no such options in its “Mouse” control options. How to enable natual scroll of touchpad in MATE on Linux? You may manually set the change of your ‘scrolls’ from the touch pad to negative values using
Read more
How to install MATE on Linux Mint 17 Qiana?
Posted onI am using Linux Ming 17 Qiana. How to install MATE on Linux Mint 17? In Linux Mint, the package that installs MATE is ‘mint-meta-mate’: $ sudo aptitude install mint-meta-mate
How to generate a CSR from a private SSL key?
Posted onI am renewing my SSL key for my websites. The CA requests a CSR. I have my private key. How to generate a CSR (Certificate Signing Request) from a private SSL key? You can generate a CSR from your private key with openssl: openssl req -new -key ssl.key -out req.pem Here, ssl.key is your private
Read more
In Vim, how to search and replace in visual selection?
Posted onSearch and replace in the whole text in Vim can be done by :%s/from/to/gc. But how to search and replace in visual selection only? The %V atom in Vim restricts a pattern so that it matches only inside the visual selection. So, your command for searching and replacing only in the visual selection can be
Read more
How do I force Linux to unmount a filesystem?
Posted onSome time, Linux fails to unmount a filesystem reporting “device is busy”. I understand that this helps avoid data lost by disallowing unmouting a filesystem when it is being used. But for some situations, I am sure there is something wrong happened or I care not data lost, such as a NFS mounting while the
Read more
How to manually set Vim’s filetype?
Posted onVim can detects the file types from quite well. But under certain situations, such as creating a new file, Vim does not automatically set the file type. How to manually set it? In side Vim, to set a filetype, set value to the filetype variable. For example, to set the filetype to php: :set filetype=php
Linux boots failed with “sulogin: can not open password database” while the /etc/passwd and /etc/shadow files look fine
Posted onLinux boots failed with “sulogin: can not open password database” while the /etc/passwd and /etc/shadow files look fine. How to fix this? The OS is Fedora 22. You may try this trick Step 1: boot Linux with rw init=/bin/bash following this tutorial. Step 2: after Linux booted, disable SELinux following this tutorial. Reboot. If it
Read more
How to get the top committers in git?
Posted onHow to get the top committers in git? Like in github: But plain text is fine showing number of commits and author names/emails sorted by the number of commits. You may use this command: git log | grep Author | sort | uniq -c | sort -n -r Here, count the number of commits by
Read more
How to debug media print view of Web page in Firefox?
Posted onHow to debug the media print view set by @media print {} in CSS of Web pages in Firefox? In firefox, after opening the Web page, First, hit “Shift + F2” to open the “Developer Toolbar” at the bottom. Second, in the “Developer Toobar”, input media emulate print and Firefox will show the print view
Read more
How to get the latest git commit SHA-1 in a repository?
Posted onHow to get the latest git commit SHA-1 id in a repository? And how to get the first 8 digits of the SHA-1? Instead of the method introduced here, you may use $ git rev-parse HEAD to get the commit SHA-1 hash ID. If you want to get the first 8 digits only, use $
Read more
How to transfer text messages from android to computer?
Posted onHow to transfer text messages from android phone to computer? One solution is to use the SMS Backup & Restore app. The app will backup your messages to a microSD card and then you can copy it out to a PC by connecting your Android phone to your PC with the USB cable.