I have a text file with many lines of text. In Linux, how to sort the lines by their length? You can make use of awk together with the sort program like awk ‘{ print length(), $0 | “sort -n” }’ /path/to/text/file Here, we use awk to calculate the text length and sort -n to
Read more
Tag: Linux
How to make journalctl faster?
Posted onOn Linux, I check system logs by journalctl. But it is soooo slow. How to make it faster. By default without any options, the journalctl is indeed very slow. But usually, we are only interested in some logs and can use options to specify the logs that we are interested to check. This will make
Read more
How to play video in full screen and keep it looping with MPlayer?
Posted onI would like to play a video file in full screen and automatically make it keep playing during an exhibition. On Linux, how to do it with MPlayer? You can use this command mplayer -fs -loop 0 video.mp4 Here, -fs make it fullscreen and -loop 0 makes it loop infinitely. For more usage of mplayer,
Read more
How to do “contains string” test in Bash?
Posted onHow to test whether a string $str contains another string $needle in Bash? You can use this piece of Bash script: [[ “$str” == *”$needle”* ]] A usage example: $ str=”abcde hello” $ needle1=”deh” $ needle2=”de hello” $ [[ “$str” == *”$needle1″* ]] && echo “matched” $ [[ “$str” == *”$needle2″* ]] && echo “matched”
Read more
‘dd’ command cannot support calculation for its parameters
Posted on$ dd if=/dev/zero of=./4Ktest100M bs=4KB count=25000*9 dd: invalid number `25000*9′ I think ‘dd’ should support calculation for its parameters like ‘bs’, ‘count’ and so on. You can get the effect by using some other tools/commands, like dd if=/dev/zero of=./4Ktest100M bs=4KB count=$((25000*9)) or dd if=/dev/zero of=./4Ktest100M bs=4KB count=$(bc <<< 25000*9) I think it makes sense. Thank
Read more
How to remove trailing / in path in Bash?
Posted onI am writing a Bash script accepting input of a path from the user. I want to make the path have no trailing ‘/’, such as ‘/path/to/dir’ instead of ‘/path/to/dir/’. However, as the input is from the user, the input could be ‘/path/to/dir’. My script is intended to handle this. The question is: how to
Read more
How to attach and mount Xen DomU’s disk partitions on Linux without Xen?
Posted onHow to attach and mount Xen DomU’s disk partitions on Linux without Xen? You can use kpartx to create devices from the VM disk image (say, ./vmdisk0). To activate all the partitions in a raw VM disk image: # kpartx -av ./vmdisk0 This will output lines such as: add map loop1p1 (253:8): 0 497664 linear
Read more
How to get a script’s directory reliably in Bash on Linux?
Posted onHow to get a script’s directory reliably in Bash on Linux? For example, to get the directory of the executing script $0. dirname can give you the directory name from the absolute path. You can get the absolute path of the script by readlink -f to handle symbolic links (consider a symbolic link ./run.sh linked
Read more
How to use the xargs argument twice in the command on Linux?
Posted on`xargs` passes the argument once to the utility command specified. For example, xargs cat will cat every line passed to xargs. But how to use the xargs argument twice in the command on Linux? For example, to rename file to file.bak where file is from the stdin. One solution is to write a small script like
Read more
Why does ; after & lead to unexpected token error in bash on Linux?
Posted onThe command using ‘;’ after ‘&’ like ssh host1 hostname &; ssh host2 hostname & Leads to error like bash: syntax error near unexpected token `;’ Why does ; after & lead to unexpected token error in bash on Linux? And what’s the solution? The fix The quick fix is to change your command to
Read more
How to add Google as the search engine in Firefox on Linux Mint?
Posted onIn Firefox on Linux Mint, Google is not in the list of search engines supported. How to add Google in Firefox on Linux Mint? Check this tutorials to add Google in Firefox on Linux Mint: How to Add Google to Firefox in Linux Mint as Default Search Engine
How to manually kill HDFS DataNodes?
Posted onstop-dfs.sh report that there are no datanodes running on some nodes like hdfs-node-000208: no datanode to stop However, there are DataNode process running there. How to clean these processes on many (100s) of nodes? You may use this piece of bash script: for i in `cat hadoop/etc/hadoop/slaves`; do echo $i; ssh $i ‘jps | grep
Read more
How to extract images from PDF on Linux?
Posted onI have a PDF file that has some images in it. How to extract the images out (not snapshot/screenshot of the page areas) from PDF on Linux? 2 tools that I usually use for extracting and saving images from PDF files: pdfedit and libreoffice. You can open the PDF file by the tools, right click
Read more
Firefox: how to sync bookmarks saved on iOS devices to Firefox on PC?
Posted onFirefox on iOS can view bookmarks from PC. But how to sync the bookmarks made on iOS to PC? It seems the bookmarks made in Firefox on iPhone is local only. No, you can’t yet. Bookmarks saved on your iOS devices will not sync to your PC at current (by Nov. 25, 2015) version of
Read more
Maximum number of mmap()’ed ranges and how to set it on Linux?
Posted onWhat’s the maximum number of mmap()‘ed ranges that a process can makes and how to set the limits on Linux? I have a program that mmap()s and mprotect()s lots ranges. After allocating many ranges, mprotect() starts to fail with ENOMEM error number. From the man page, ENOMEM means 2 possible problems: ENOMEM Internal kernel structures
Read more
How to disable laptop’s internal keyboard on Linux?
Posted onI connect a USB keyboard to my laptop so the internal keyboard is not used anymore. But I may press some keys by accident. How to disable the laptop internal keyboard? I am working on Linux. This post introduces how to disable laptop’s internal keyboard on Linux: How to Disable and Enable Laptop Keyboard in
Read more
How to watch a directory size in Linux Shell
Posted onHow (only) to show the size of the directory. LS command may show more trivial infos I don’t need. DU command can do it. DU is “du – estimate file space usage”. Users could use ‘–max-depth=N’ parameter to print the level of information. One example is as follows. $ du -h –max-depth=0 hummer-svn 11G hummer-svn
How to merge sam files on Linux?
Posted onThe samtools merge can merge bam files while it can not work for sam files. How to merge sam files on Linux? According to the sam format specification, header lines start with @, while alignment lines do not. So you can use grep to merge sam files as follows efficiently. Assume the header is from
Read more
How to configure ifcfg-eth0 network scripts on Fedora/RHEL Linux?
Posted onHow to configure the ifcfg-* network script files under /etc/sysconfig/network-script/ on Fedora/RHEL Linux? Following is a very typical ifcfg-eth0 file setting static IP/gateway/network mask: DEVICE=eth0 BOOTPROTO=none ONBOOT=yes NETMASK=255.255.0.0 IPADDR=192.168.1.151 GATEWAY=192.168.1.10 USERCTL=no More options are supported, here you can find a reference: Interface Configuration Files (a easier to read PDF from F15 doc).
How to convert tiff images from RGB color to CMYK color on Linux?
Posted onI have a poster to be submitted to the publisher which requires image files in CMYK color. The file I already have is in RGB color. How to convert it on Linux? The convert tool from ImageMagick with option -colorspace to alternate image colorspace of images. The plain output with colorspace is quite large. So
Read more