Login without PWD is fast and efficient. Running commands in server from local machine also have these benefits. login without PWD: add PC A’s public key to PC B’s authorized keys. > a@A:~> cat .ssh/id_rsa.pub | ssh b@B ‘cat >> .ssh/authorized_keys’ > b@B’s password: Run command remotely ssh root@MachineB “ls” NOTE: running commands remotely is
Read more
Tag: Bash
how to set linux date and time in commands
Posted onhow to set linux date and time in commands For example, to change date to 14 Nov 2017 11:57:00, the command would be, $ sudo date –set “14 Nov 2017 11:57:00” Tue Nov 14 11:57:00 HKT 2017
How to print all fields after a certain field with awk on Linux?
Posted onHow to print all fields after a certain field with awk on Linux? Say, I want to print out all fields after field $3: a b c d e f a b b b a a c d should be transformed to d e f b d You may use a for loop in awk
Read more
How to add a prefix string at the beginning of each line in Bash shell script on Linux?
Posted onHow to add a prefix string at the beginning of each line in Bash shell script on Linux? For example, assume we have a file a.txt: line 1 line 2 I want to have, pre line 1 pre line 2 You may use sed, the stream editor for filtering and transforming text: sed -e ‘s/^/pre
Read more
How to detect whether a file is being written by any other process in Linux?
Posted onHow to detect whether a file is being written by any other process in Linux? Before a program open a file to processes it, it wants to ensure no other processes are writing to it. Here, we are sure after the files are written and closed, they will not be written any more. Hence, one-time
Read more
How to get the highest temperature from all sensors in a server on Linux?
Posted onIt is useful to monitor a server node’s temporary. Among all the sensors’ temperatures, the higher one may be a very important one. How to get the highest temperature from all sensors in a server on Linux? You can use this command to get the the highest temperature from all sensors in a server on
Read more
How to split a gzip file to several small ones on Linux?
Posted onI have a very large (e.g. 100GB) .gz file and would like to split it into smaller files like 8GB/each for storage/copying. How to split a gzip file to several small ones on Linux? You may use the tool split to split a file by sizes. An example to split a large.tgz to at most
Read more
Killing Running Bash Script Process Itself and All Child Processes In Linux
Posted onIn Linux, how to kill a process and all its child processes? For example, a Bash script A starts B, B starts C and C calls rsync. I would like to kill A and all its child processes all together. How to do this? There are possibly many answers to this question. One of the
Read more
How to get the mtime of a file on Linux?
Posted onHow to get the mtime of a file on Linux from the file’s path? You can use stat to get the file status including the mtime: %y time of last modification, human-readable %Y time of last modification, seconds since Epoch As an example, $ stat -c %y ./file 2017-06-26 13:33:06.764042064 +0800 $ stat -c %Y
Read more
How to print a line to STDERR and STDOUT in Bash?
Posted onIn Bash, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In Bash, you can simply use the echo command: echo “your message here” or echo your message here Examples: $ echo the message here the
Read more
How to get the running process’ parent process’ ID in C / C++?
Posted onHow to get the running process’ parent process’ ID in C / C++? In C and C++, you can call the getppid() library function which is a function from the POSIX library. #include <sys/types.h> #include <unistd.h> pid_t getppid(void); getppid() returns the process ID of the parent of the calling process. Example usage: getppid.c #include <stdio.h>
Read more
How to get the running process’ parent process’ ID in Bash?
Posted onHow to get the running process’ parent process’ ID in Bash? In Bash, you can get the parent process’s pid from the variable PPID. Note that in a (…) subshell, the $$ stores the subshell’s parent shell pid actually.
How to get the running process’ pid in Bash?
Posted onHow to get the running process’ pid in Bash? In Bash, you can get the process ID from the variable $$. Note that in a subshell invoked by ‘(…)‘, $$ is actually the parent process’ pid. In Bash 4, you can also use $BASHPID to get the process pid.
How to Debug a Bash script?
Posted onHow to debug a Bash script if it has some bugs? Common techniques like printing varibles out for checking apply for bash too. For bash, I also use 2 bash-specific techniques. Use errexit option Run the script with -e option like bash -e your-script.sh or add set -o errexit to the beginning of the script.
Read more
How to find the disk where root / is on in Bash on Linux?
Posted onQuestion: how to find the disk where the Linux’s root(/) is on in Bash? The root may be on a LVM volume or on a raw disk. 2 cases: One example: # df -hT | grep /$ /dev/sda4 ext4 48G 32G 14G 71% / For another example: # df -hT | grep /$ /dev/mapper/fedora-root ext4
Read more
How to get all the keys in an associative array in Bash?
Posted onHow to get all the keys of an associative array in Bash? There are at least 2 ways to get the keys from an associative array of Bash. Let’s start with an example associative array: $ declare -A aa $ aa[“foo”]=bar $ aa[“a b”]=c We can use the @ special index to get all the
Read more
How to get the one character’s next character in ASCII table in Bash?
Posted onHow to get the one character’s next character in ASCII table in Bash? For example, if I have ‘a’ in variable i, how to get ‘b’? First, we need to get the integer value for the character. char=’b’ charint=$(printf “%d” “‘$char'”) Then, we increase the integer by one let charint=$charint+1 Last, we can get the
Read more
How to delete or get the number in the tail from a string in shell script?
Posted onHow to delete or get the number in the tail from a string in shell script (bash script)? Okay to call other tools. For example, from “/dev/sda8”, I want to get “/dev/sda” and “8” separately. This can be achieved by using sed as follows. To get the string after deleting the tailing numbers, we can
Read more
How to split a string by string in Bash?
Posted onHow to split a string by string in Bash? For example, “a string separated by space” => [“a”, “string”, “separated”, “by”, “space”] and “a,string,separated,by,comma” => [“a”, “string”, “separated”, “by”, “comma”] You can convert a string to an array using the grammar like inarr=(${a}) If the delimiter is not space and delimiter is a single character
Read more
How to convert epoch timestamp to human readable date format in Bash?
Posted onIn Bash, how to convert an epoch timestamp to a human readable date format? In Bash, you can use the date command’s -d option: date -d @<your epoch> Here @ specifies the epoch timestamp. One example: $ date -d @1490157520.05 Wed Mar 22 12:38:40 HKT 2017