Ahad, 23 Mac 2014

3966. A personal weight management software.

بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ  , الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ , الرَّحْمَنِ الرَّحِيمِ ,  مَالِكِ يَوْمِ الدِّينِ , إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ , اهْدِنَا الصِّرَاطَ المُسْتَقِيمَ  , صِرَاطَ الَّذِينَ أَنْعَمْتَ عَلَيْهِمْ , غَيْرِ المَغْضُوبِ عَلَيْهِمْ وَلاَ الضَّالِّينَ.


Assalamualaikum w.b.t/السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُه

Meja www.peceq.blogspot.com 

Pondus: A personal weight management software


A week after my return from a New England & Canada cruise, I found myself busy searching for a personal weight management software.

Why weight management? See the chocolate dessert buffet pictures taken on board the Holland America Maasdam cruise ship.

I came across pondus, a python program that is free and open-sourced.

Pondus has a rather modest feature set: the ability to enter one's weight over time, and have it plotted in a chart. It does not have too many bells and whistles, but it is very simple to use.

Pondus is packaged with certain Debian releases (namely, Lenny and Sid). So a simple apt-get install pondus will put it on your system. Because I run Debian Etch, I need to install it from source files.

I downloaded the source tar ball as well as the install instructions from here. The instructions are straight-forward, and I installed pondus without a glitch.

To run pondus, just type pondus in the command line. When you run pondus for the first time, it is not too terribly exciting .... because you have not entered any weight data.

Before you enter your weight, you should customize the unit of measure that pondus will use(lbs versus kgs). Click on the Tool icon (the one with the screwdriver and wrench) to bring up Preferences. For me, I prefer pounds (that is the unit my scale uses).

Now, let's enter some weight data, and draw a pretty chart.

To enter a weight, click on the Plus icon.

To plot the data, click on the Chart icon.

Pondus has some additional useful features such as data import and export in CSV format, as well as the ability to save the chart in png or svg (2-dimensional vector graphics) format.

An interesting feature is that you can set time-sensitive goals (aka, Weight Planner). For example, you can set a goal such as "weigh 180 lbs on August 15". This feature is disabled by default. To enable it, bring upPreferences as above, and click Use Weight Planner checkbox to select it, and choose whether to include the weight plan in your plots. Note that this feature is not for everyone (do I really need yet another remainder of my shortcomings?)

Pondus is adequate for tracking my body weight. I'm still searching for software that can also track my blood pressure. Won't it be perfect if a single software can manage both weight and blood pressure?


Sumber: (http://linuxcommando.blogspot.com/2008/07/pondus-personal-weight-management.html)

Identify Linux distribution, code name and version info

Peter Leung at Linux Commando - 2 days ago
How do you identify the Linux distribution, the specific code name and version number that is running on a machine? Below are several ways. 1. lsb_release $ lsb_release -a No LSB modules are available. Distributor ID: Debian Description: Debian GNU/Linux 7.4 (wheezy) Release: 7.4 Codename: wheezy If lsb_release is not installed by default on your machine, install it as follows. - For Fedora, Centos, and Red Hat Enterprise Linux $ sudo yum install redhat-lsb-core - For Debian and its derivatives. ... more »

How to enable mod_rewrite for Apache web server

Peter Leung at Linux Commando - 5 days ago
I've installed two Content Management Systemson my Debian wheezy system - WordPress and Drupal. The web server deployed is the venerable Apache. Common to the 2 deployments is the issue of how to enable the *mod_rewrite*module for the Apache web server. The rewrite module maps obscure URLs - computer gibberish - to 'clean' URLs. The default URLs, for both WordPress and Drupal, are something like *http://localhost/wordpress/?p=248*. You can configure the URLs to be rewritten to resemble the post titles: *http://localhost/wordpress/a-board-game/*. However, the change won't take e... more »

Part 2: Specify default applications for file types - file managers

Peter Leung at Linux Commando - 1 week ago
Part 1introduced the *xdg-mime* command to change the default application for a file type. If you want a GUI method, or more tweaking is required after running the command, this post is for you. File managers can create file associations - the assigning of a default application to open files of a certain extension. Below is an example of changing the PDF default application using the Nautilus file manager for the GNOME desktop. 1. Open Nautilus, and navigate to any PDF file. 2. Right click on the PDF file, and select *Properties*. 3. Click to open the *Open With* ta... more »

How to specify default applications for file types: Part 1 - Command line

Peter Leung at Linux Commando - 2 weeks ago
Numerous apps vie to open your PDF files by default - *evince*, *xpdf*, *Adobe Reader*, *Okular*, etc. If you don't like the default as dictated by your desktop environment, you can change it. The general idea is that default applications for file types, or 'file association', is governed by a set of configuration files. Having said that, I wish an universal method existed to change file association that is independent of the Linux distribution, the desktop environment, the file manager, and the application. In reality, the desktop environment may choose to interpret the same con... more »

How to find the MIME type of a file

Peter Leung at Linux Commando - 2 weeks ago
Some MIME types can be intuitive - *application/pdf* for PDF files. However, not all MIME types are that easy to get exactly right - *application/vnd.ms-excel* for *xls* or Excel files. The following are different ways, using the command line, to identify the MIME type of a file. - xdg-mime $ xdg-mime query filetype somefile.xls application/vnd.ms-excel - file $ file -b --mime-type somefile.xls application/vnd.ms-excel - gvfs-info $ gvfs-info -a standard::content-type somefile.xls attributes: standard::content-ty... more »

wkhtmltopdf converts HTML to PDF

Peter Leung at Linux Commando - 4 weeks ago
Most - if not all - new electronic devices don't come with printed manuals. For the Sony laptop SVF15A1 I bought, I had to go to the Sony web site to access the user guide. The user guide is composed of many individual HTML pages. This posted a problem for me because I wanted to convert some HTML pages to PDF documents for easier off-line access. Modern web browsers, such as Chrome and Firefox, have the built-in print to PDF feature. For Chrome, 1. Navigate to the HTML page, right click, and select *Print*. 2. Select *Save as PDF* to be the *Destination*. 3. Click... more »

Find out when a package was last installed or updated

Peter Leung at Linux Commando - 5 weeks ago
If you administer a Linux computer, you may occasionally ask when a software package was last installed or updated on your system. For a Red-Hat-based operating system - Centos, Fedora, RHEL, etc - getting the answer is a simple task of querying the RPM database. The RPM database stores, among other things, the last install date of rpm packages. To query information about the *curl* package: $ rpm -qi curl Name : curl Version : 7.29.0 Release : 7.fc19 Architecture: i686 Install Date: Sun 11 Aug 2013 03:55:52 PM PDT Group : Applications/Internet Size :... more »

Merging pdf files

Peter Leung at Linux Commando - 1 month ago
This post is about combining pdf files. It complements 2 earlier posts on splitting pdf files - part 1, and 2. I am using a tool named SimpleScan to scan in multiples pages of a document. Each page is scanned into a separate pdf file. I must find a way to stitch the pdf files together into one single pdf. You can use either the *pdftk* command, or the *gs* command to merge pdf files. You may recall that they are the same commands you would use to split pdf files. In the examples below, the input files are *T4a.pdf*, *T4b.pdf*, and *T4c.pdf*; the merged output file is *combined.p... more »

How to split up PDF files - part 2

Peter Leung at Linux Commando - 1 month ago
In an earlier post, I used the *pdftk* tool to extract pages from a pdf file. I had no reason to investigate alternative solutions until I encountered the following problem. I had to extract the first 4 pages of a pdf document. The normally reliable *pdftk* command generated a Java exception. $ pdftk T4.pdf cat 1-4 output outputT4.pdf Unhandled Java Exception: Unhandled Java Exception: java.lang.NullPointerException at gnu.gcj.runtime.NameFinder.lookup(libgcj.so.12) at java.lang.Throwable.getStackTrace(libgcj.so.12) at java.lang.Throwable.stackTraceString(libgcj.so.12) ... more »

Print text files with multiples pages per sheet

Peter Leung at Linux Commando - 2 months ago
*a2ps* is the venerable tool for formatting text files for Postscript printers. This post focuses on how to call *a2ps* to print multiple pages per sheet. To install *a2ps* and *gv* aka ghostview, which *a2ps* uses to preview output files: $ sudo apt-get install a2ps $ sudo apt-get install gv Layout You can layout a sheet of paper into rows and columns of pages. To print the emacs config file in the above format, any of the following will do: $ a2ps ~/.emacs $ a2ps --rows 1 --columns 2 ~/.emacs $ a2ps -2 ~/.emacs Note that with no explicit instruction, the default layout i... more »

pinta: a lightweight paint app that has (requires) no manual

Peter Leung at Linux Commando - 2 months ago
Want a tool for editing image files, but shun GIMP because of the steep learning curve? For me, I needed an app to edit screenshots for my blog posts. *Pinta* turns out to be the perfect tool for that purpose. I use pinta to edit the following image file formats: JPG, PNG, TIFF, BMP, ICO, TGA, ORA. To install *pinta*: $ sudo apt-get install pinta # Debian Wheezy $ sudo yum install pinta # Red Hat The main draw canvas is sandwiched between 2 columns of tool sub-windows. The left column is organized into *Tools* and *Palette*; the right, *Layers*, *Images*, and *Histor... more »

OCR Scanning

Peter Leung at Linux Commando - 2 months ago
This post describes how to scan pages from a printed book and convert the image to text using Optical Character Recognition (OCR) technology. The tools that I use are: 1. SimpleScan 2. tesseract Preparation SimpleScan is a GUI scan application that comes pre-installed in many Linux distributions (including Debian Wheezy). To manually install it on Debian: $ sudo apt-get install simple-scan tesseract is a command-line OCR program. To install: $ sudo apt-get install tesseract-ocr If English is the language used, that is all you need to install. If you require... more »

Install latest WordPress on Debian Linux

Peter Leung at Linux Commando - 3 months ago
This blog is built on the Google Blogger platform. My new blog however will switch to WordPress, and most likely be hosted by a third-party web hosting company. For now, I want to install WordPress on my home computer to be the staging server. This is where I experiment with new ideas before I upload the final contents to the production WordPress server. My home computer runs Debian 7 (aka Wheezy) which includes a WordPress package, albeit a little outdated (3.6.1). The latest WordPress release 3.8introduces a new template and admin dashboard design. This post is an overview of ho... more »

How to install pre-packaged software collections after the initial install

Peter Leung at Linux Commando - 3 months ago
During a fresh Debian install, you can specify the installation of pre-assembled collections of related software packages that together provide a high-level functionality, for example, Web Server. The install screen may look something like this: If you select *Web server*, the install program will take care of installing the right packages for you. You don't need to know the names of the individual web server packages. The install program will install Apache2 automatically. What to do if you have declined to install a collection, only to realize later that you do need it? Aft... more »

Dual booting Debian Wheezy and Windows 7

Peter Leung at Linux Commando - 3 months ago
My ASUS CM6870 desktop computer came pre-installed with Windows 7. The following outlines the steps to set up dual boot of Debian Wheezy and Windows 7. 1. Re-partition your hard drive. Initially, Windows takes up the entire hard drive. Two 'letter' drives were allocated on this disk: C, and D. The C: drive holds the Windows system with plenty of unused space. The D: drive stores user data, and is practically empty. Before installing Linux, you need to re-partition the hard drive to allocate space to Linux. I used the native Windows 7 Disk ... more »

KDE4: how to relocate icons on the Plasma desktop

Peter Leung at Linux Commando - 3 months ago
As a long-time GNOME user, I had to overcome some initial learning curve to the KDE4 desktop environment. My first puzzlement is how to re-position icons on the desktop. I tried to click and drag the icon. However, clicking the icon immediately runs the associated application. That was not my intention. The trick is to click on the side panel which only appears when you mouse over the icon. Drag the side panel to the target location, and release. Be careful to only click the background of the side panel, not any item on the panel. Clicking an item on the panel will execute the ... more »

KDE4: Show the Trash Can and Home icons

Peter Leung at Linux Commando - 3 months ago
My earlier post shows how to display the Trash Can on the GNOME 3 desktop. Not to be out-done, KDE4 also hides the Trash Can by default. This post outlines the steps to enable the display of the Trash Can on KDE4. I'll also show how to set up the Home directory icon on the KDE4 desktop. To display the Trash Can: 1. Click anywhere on the desktop background. A menu is displayed. 2. Click *Add Widgets*. 3. Search for the trashcan widget by entering 'trash' in the search box. 4. Double click the trashcan widget returned by the search. You now have a Trash Can d... more »

GNOME 3: Get your Trash Can and Computer icons back

Peter Leung at Linux Commando - 3 months ago
I recently upgraded from Debian 6 (aka "squeeze") to 7 (aka "wheezy"). Debian wheezy runs a newer version of the desktop environment, GNOME 3 (up from GNOME 2). With GNOME 3, certain desktop actions that users were able to do in GNOME 2 have been disabled by default. Below are some desktop features removed by default that I find particularly annoying: - No Trash Can icon on the desktop. - No Computer or Home icon on the desktop. - Right clicking on the desktop does not bring up a menu. - Files in your Desktop directory (*/home//Desktop*) do not appear on the ... more »

How to connect to Bluetooth headset/soundbar/speaker

Peter Leung at Linux Commando - 3 months ago
Most modern laptops are equipped with the Bluetooth radio. It means that you can use a Bluetooth mouse and keyboard as your input device, and a Bluetooth headset, soundbar, or speaker for your sound output. This article gives an example of how to connect your Linux laptop to a Bluetooth soundbar. Device model My laptop is a DELL Vostro 1015 running Ubuntu 12.04.3 LTS ("precise"). The *bluez* Bluetooth stack is of version 4.98. My soundbar is the Panasonic HTB450. According to the Owner's Manual, this model features Bluetooth V2.1 + EDR. Note that this model is not a Bluetooth Lo... more »

How to modify the user-friendly Bluetooth local name

Peter Leung at Linux Commando - 4 months ago
Each Bluetooth device has a user-friendly local name as well as an unique 48-bit hexadecimal device address (eg, C0:F8:DA:9D:CF:DF). The default local name may be too generic, eg, *ubuntu-0*, to identify you to the rest of the world. This post shows you how to assign a more descriptive local name. To find out the current local name of the Bluetooth device, execute the following command: $ hciconfig -a hci0: Type: BR/EDR Bus: USB BD Address: C0:F8:DA:9D:CF:DF ACL MTU: 1022:8 SCO MTU: 121:3 UP RUNNING PSCAN ISCAN <..sniped> Link policy: RSWITCH HOLD SNIFF Link mode: S... more »

Verify if a Linux computer has the Bluetooth hardware

Peter Leung at Linux Commando - 4 months ago
Many laptops today come with a Bluetooth radio. For desktops however, most likely you need to go buy a Bluetooth USB dongle. If you don't know whether your computer has the Bluetooth hardware, the following command will help you find out. $ lsusb |grep Bluetooth Bus 004 Device 003: ID 0cf3:3005 Atheros Communications, Inc. AR3011 Bluetooth $ If the search returns a Bluetooth device, it indicates strongly that your computer has a Bluetooth chip in it. Note that simply searching for Bluetooth in your kernel ring buffer is NOT conclusive. My Debian desktop computer does not have... more »

Connect Android and Linux using USB

Peter Leung at Linux Commando - 4 months ago
I have a Samsung Galaxy Tab 2 tablet, running Android 4.1.1 (aka Jelly Bean). I'd like to transfer some files from the tablet to my Linux server running Debian 7.2 (aka Wheezy). The Android tablet connects via Wi-Fi to the same LAN as the Linux server. There are many ways to transfer files between the 2 devices. For example, you could install an Android app named AirDroid which lets you manage your Android device from a desktop web browser, including file transfer. In this post, I'll go another route, an arguably more direct and basic one. I'll connect the 2 using a USB cable. ... more »

How to connect to a WPA/WPA2 WiFi network using Linux command line

Peter Leung at Linux Commando - 5 months ago
This is a step-to-step guide for connecting to a WPA/WPA2 WiFi network via the Linux command line interface. The tools are: - *wpa_supplicant* - *iw* - *ip* - *ping* *iw* is the basic tool for WiFi network-related tasks, such as finding the WiFi device name, and scanning access points. *wpa_supplicant* is the wireless tool for connecting to a WPA/WPA2 network. *ip* is used for enabling/disabling devices, and finding out general network interface information. The steps for connecting to a WPA/WPA2 network are: 1. Find out the wireless device name. ... more »

How to use the screen command to run a program unattended

Peter Leung at Linux Commando - 5 months ago
*screen* is a powerful terminal session manager with many use cases. One such use case is to start a long-running Command Line Interface (CLI) program in a terminal session, detach the session while leaving the program running unattended, logout, and return to the same session later from another terminal. Let's examine how you would use *screen* in a real-life situation. Imagine the time is 15 minutes to the end of your work day. But before you can leave, you have to run a program that will run for an hour. You don't want to wait around until it finishes in order to check the ou... more »

Allow root ssh login with public key authentication only

Peter Leung at Linux Commando - 5 months ago
Often, *ssh* is configured to disallow *root* to login directly. To login, *root* first logins as a non-privileged user, and then do a *sudo* to become *root*. There can be many reasons why you don't want *root* to login directly. You may be concerned about security. Brute force attacks by guessing the password are common. In addition to security, you may be concerned about traceability. If there are more than 1 administrator on a system, and they can all login as *root*, then it is impossible to trace who had done what (after all, it is the same *root* account). As an alternat... more »

Extract audio track from a youtube video

Peter Leung at Linux Commando - 5 months ago
Youtube is a great source for watching videos or listening to music on-line via their website. But if you want to download the video or audio to your computer, you need some special tool, and you need to know the URL for the youtube video you wish to download. One such tool is *youtube-dl*, which is a python script. Unfortunately, *youtube-dl* is not pre-packaged with Debian Wheezy. I downloaded the script directly from this web page: $ sudo curl https://yt-dl.org/downloads/2013.09.24.2/youtube-dl -o /usr/local/bin/youtube-dl $ sudo chmod a+x /usr/local/bin/youtube-dl The synt... more »

X11 Forwarding over SSH: run remote graphical app and display locally

Peter Leung at Linux Commando - 10 months ago
In the modern networked environment, we often wish to run an application on a remote host while we are comfortably logged in on our local computer. Assuming both machines are Linux-based, and the application runs on the graphical X desktop, the following approaches come to mind: - VNC - X11 forwarding over SSH This article focuses only on X11 forwarding. X11 forwarding over SSH enables you to run a remote X app and display it locally, with traffic between the 2 hosts encrypted by SSH. For X11 forwarding over SSH to work, both the SSH client and SSH server must be pr... more »

Forcing pseudo terminal on ssh command execution

Peter Leung at Linux Commando - 10 months ago
You already know how to execute a command on a remote computer via *ssh*. The syntax is like this: $ ssh peter@192.168.1.112 This will work if the command is simply piping output, for example, *ls*. If the command is "screen-based" in that it interprets user input, you may get an error. The following shows what happens when you *ssh* to run such programs (e.g., *top*, *emacs*, *screen*). $ ssh peter@192.168.1.112 top peter@192.168.1.112's password: TERM environment variable not set. $ ssh peter@192.168.1.112 emacs peter@192.168.1.112's password: emacs: standard input is ... more »

One-liner to shutdown remote host

Peter Leung at Linux Commando - 10 months ago
To shutdown the local machine immediately, you execute this command as *root* or under *sudo*: $ shutdown -h now If it is a remote server that you want to shutdown, it could be slightly more involved. You need to have *root* privileges to shutdown a machine. However, many systems are configured to block *root* from logging in remotely using*ssh*. So, you need to *ssh* in as a regular, non-root user, and pass the *sudo*command to shutdown host. $ ssh -t peter@192.168.1.112 'sudo shutdown -h now' peter@192.168.1.112's password: [sudo] password for peter: Broadcast message from ... more »

One-liner to copy text to a remote host

Peter Leung at Linux Commando - 11 months ago
Occasionally, I want to copy a short line of text to a remote computer. For instance, I have an URL for some real cool web site which, for whatever reason, I want to send to a remote host. I can always put the text in a file, and transfer it via *scp*. $ cat > coolurl.txt http://really-cool-web-site/ $ scp coolurl.txt peter@192.168.1.112: Or, you can use the following one-liner command: $ echo 'http://really-cool-web-site/'|ssh peter@192.168.1.112 'cat >coolurl.txt' The one-liner uses only simple commands such as *echo*, *ssh* and *cat*. It saves you the step of creating a n... more »

Identify available printer names

Peter Leung at Linux Commando - 1 year ago
Some machines have access to more than one printer. Unless the default printer is the one you want, you need to know the name of the printer to use in the printer-related command. For instance, if you want to know the status of a printer, you execute the *lpq* command: $ lpq ml1640 is ready no entries ml1640 is the default printer for the machine. If you want a printer other than the default, the *lpq* command requires that you specify the printer name. Other commands such as *lpr*, and *lprm* behave the same way. How do you find out the printer name that the Linux printing sys... more »

Deciphering mysql error codes

Peter Leung at Linux Commando - 1 year ago
I was tasked to upgrade the *mysql* server running on the Centos server. Of course, the first thing I should do was to backup my database. I run the *mysqldump* command below to write out the sql statements that will re-create and populate the database tables. # mysqldump -uroot -p -l --opt --all-databases=true > /root/mysqlall.sql mysqldump: Couldn't execute 'show fields from `asset`': Got error 28 from storage engine (1030) Lo and behold, I got an "error 28". What is "error 28"? The error message has no useful details that could help me troubleshoot. The *perror* command came... more »

Auto fill in ssh client parameters

Peter Leung at Linux Commando - 1 year ago
I often ssh to different servers, both at work and at home. Often, the Linux account name is different according to which server I'm logging in. Also, some servers are set up to allow ssh login through a different port than the default port 22. For instance, to login to 123.123.123.123, I need to type all this in: # ssh -p 2222 admin_2@123.123.123.123 All of this can become human unmanageable rather quickly. Luckily, I can set up the ssh client such that it fills in ssh login parameters such as port number and user name. I simply type ssh followed by the IP address or hostnam... more »

Auto fill in ssh client parameters

Peter Leung at Linux Commando - 1 year ago
I often ssh to different servers, both at work and at home. Often, the Linux account name is different according to which server I'm logging in. Also, some servers are set up to allow ssh login through a different port than the default port 22. For instance, to login to 123.123.123.123, I need to type all this in: # ssh -p 2222 admin_2@123.123.123.123 All of this can become human unmanageable rather quickly. Luckily, I can set up the ssh client such that it fills in ssh login parameters such as port number and user name. I simply type ssh followed by the IP address or hostnam... more »

Splitting up is easy for a PDF file

Peter Leung at Linux Commando - 1 year ago
Occasionally, I needed to extract some pages from a multi-page pdf document. Suppose you have a 6-page pdf document named *myoldfile.pdf*. You want to extract into a new pdf file *mynewfile.pdf* containing only pages 1 and 2, 4 and 5 from *myoldfile.pdf*. I did exactly that using *pdktk*, a command-line tool. If *pdftk* is not already installed, install it like this on a Debian or Ubuntu-based computer. $ sudo apt-get update $ sudo apt-get install pdftk Then, to make a new pdf with just pages 1, 2, 4, and 5 from the old pdf, do this: $ pdftk myoldfile.pdf cat 1 2 4 5 outpu... more »

Convert units of measurement

Peter Leung at Linux Commando - 1 year ago
How do you convert from 1 measurement unit to another unit? Say from kilometers to miles. I can always look it up on that fridge magnet. This one local realtor likes to send me freebies like the fridge magnet with the common conversion factors. If you don't have a handy fridge magnet, you can try the units command-line utility program. To install it on a Debian system, enter: # apt-get install units # units 60km miles * 37.282272 / 0.0268224 It tells you that 60 kilometers is equivalent to 37.282272 miles. The second line (beginning with the '/') is somewhat confusing.... more »

How to insert a file at a specific line and column

Peter Leung at Linux Commando - 3 years ago
My objective is to insert the complete contents of a text file at a specific row and column of another text file. If we are merely concerned with inserting after a specific line, it can be readily achieved with a number of Linux tools. For example, to insert file1.txt after the second line of file2.txt, any of the following commands will do: - $ sed -i '2r file1.txt' file2.txt - $ awk '{print} NR==2 {while (getline < "file1.txt") print}' file2.txt Unlike the previous sed command which modifies file2 in-line, the above awk command writes the desired output to standar... more »

Fun with Date Arithmetic

Peter Leung at Linux Commando - 4 years ago
We all know that the date command tells you the current time. Occasionally, you use the same command to set the time. That however becomes rarer these days with the advent of the ntp service that automatically synchronizes your computer's time with a super accurate public time server of your choice. Various implementations of the date command are in use today. This article discusses the date command of the GNU coreutils package. By default, the date command tells you the time now. The date command also lets you do some basic date addition and subtraction. This is achieved by specif... more »

How to move print jobs from one printer queue to another

Peter Leung at Linux Commando - 4 years ago
In the olden days, an entire office department shared one printer. At home, an entire household shared one printer. Nowadays, I have 2 printers at home (1 HP LaserJet and 1 Samsung Monochrome Laser printer). With access to multiple printers come new opportunities and challenges. One of those opportunities is that if one printer goes down for whatever reason, you can route your print jobs to the surviving one. One challenge is that you need to manage multiple printer queues. With more than 1 printer, I often find myself printing to the wrong one. How do you route print jobs from 1 pr... more »

Search and replace line feeds using emacs

Peter Leung at Linux Commando - 4 years ago
As an avid emacs user for a decade plus, I am a little embarrassed to confess that I did not know for the longest time how to search for or replace line feed (newline) characters. If you are only searching for run-of-the-mill ASCII strings such as *abc*, you just type *C-s* (control-S), and then the characters, and hit *return*. Unfortunately, if your search string includes a line feed character, e.g., *abc* followed by a newline, I have some good news and some bad news for you. The good news is that you use the same shortcut (*C-s*) just like you would normally to search for any... more »

loook: A light-weight text search tool for OpenOffice documents

Peter Leung at Linux Commando - 4 years ago
If you are happily running a desktop search tool such as Tracker or Beagle, read no further. You already have the search tool you need to search OpenOffice files and more. These desktop search tools build indexes to improve search performance, and are in general quite scalable. Yet, a desktop search tool may be an overkill if you are just searching for certain text string in a directory containing OpenOffice files. loook is a no-frill, yet very functional text search tool for OpenOffice files. Unlike the aforementioned desktop search engines, it does not build an index of your OpenO... more »

Is PHP supported on my Web Server?

Peter Leung at Linux Commando - 4 years ago
Recently, I had to install a PHP-based web application on my Apache web server. My first question was: "Is PHP enabled on the web server?" You can always ask the systems guy. But if you happen to be the systems guy, and you don't quite remember if you have installed PHP, here is how to find out. 1. Create a new file with a php suffix (say myphp.php) 2. Copy and paste the following code into the php file. (I cannot get blogger.com to display PHP code; use the code in the April 29, 2009 comment) 3. Save the file and upload it to a location on your web server that is... more »

More on Inserting Arguments from Previous Commands

Peter Leung at Linux Commando - 4 years ago
I previously blogged on using the shortcut Alt + dot to insert the last argument from the previous command. Suppose you don't want the last argument. Instead, you want to insert the first, second, or third argument of a previous command. No problem! Alt + 1 + dot inserts the first argument of the last command. To key it in properly, hold the alt key, press the 1 key, and then the dot (".") key. Similarly, alt + 2 + dot inserts the second command argument. For example, you just executed this command. $ ls -l /home/peter/somefile.txt secondfile.txt -rw-r--r-- 1 peter peter 8115 200... more »

Open a file from the command line using its default application

Peter Leung at Linux Commando - 4 years ago
Windows users are familiar with the concept of file association. When you double click a file (say, cisco.doc), Windows examines the file name extension (doc), and opens the file using the default program associated with that extension(Office). Linux users can open files in a similar way in their X Window graphical user interface. But, if you want to open the file from the command-line, you need to type out at least the program name, oowriter, or do you? $ oowriter cisco.doc If GNOME is your window manager, use the gnome-open command as follow: $ gnome-open cisco.doc oowrite... more »

Find all (non-)empty files in a directory

Peter Leung at Linux Commando - 4 years ago
Creating an empty file in Linux is easy. If a-file does not exist, create the file and make it empty by simply: $ touch a-file $ ls -l a-file -rw-r--r-- 1 peter peter 0 2009-05-02 20:15 a-file Finding all empty files in a directory can also be done using a single command. Ditto for non-empty files. Suppose you want to find all empty files in the directory /home/peter. The command is: $ find -L /home/peter -maxdepth 1 -type f -size 0 By default, the find command excludes symbolic files. Use the -L option to include them. The expression -maxdepth 1 specifies that the maximum dep... more »

A surefire shortcut to Insert the Last Argument of the Last Command

Peter Leung at Linux Commando - 4 years ago
Sometimes, in the Linux command-line world, a seemingly trivial technique can turn out to be tremendously useful. Before I discover the Alt-dot(.) shortcut, I type !$ to insert the last argument of the previous command. peter@tiger:~$ ls -l Windows_20081110102654.log -rw-r--r-- 1 peter peter 808 2008-11-10 10:26 Windows_20081110102654.log peter@tiger:~$ cat !$ cat Windows_20081110102654.log ..... ..... peter@tiger:~$ Then, I discovered that typing Alt-dot achieves the same result. That is, press (and hold) the Alt key, then the dot key. There are some advantages of using Alt-dot ove... more »

Auto Start Applications at Login to GNOME Desktop

Peter Leung at Linux Commando - 5 years ago
It may sound trivial, but for the longest time, I, being an avid CLI fan, did not configure GNOME to auto-start certain applications after login to desktop. A couple of times, I actually went looking for some entry similar to Microsoft Windows Startup menu in the GNOME menu structures, but each time, I came up empty and frustrated. Finally, I found out how, and I wrote it down. The following works for the GNOME Desktop 2.14.3 using the Debian Etch Linux distribution. To configure GNOME to auto start an application on login, 1. Mouse to Desktop -> Preferences -> Sessions. ... more »

How to increase number of disk mounts before next fsck at system boot

Peter Leung at Linux Commando - 5 years ago
Many home users often power off their computers when they are not being used. Some do it to be green: turning idle computers off saves electricity and $$. Others do it for the extra security. To those who are hard-core Linux geeks, machine uptime is sacred, and voluntarily rebooting the machine is nothing but sacrilegious. If you do reboot your machine from time to time, you most definitely have encountered a most annoying experience. Once in a while, while the computer is booting up, you see the message /dev/hdaN has reached maximal mount count, check forced. The check seems to tak... more »

How to open and close the CD DVD tray

Peter Leung at Linux Commando - 5 years ago
You can open/close the CD, DVD disk tray from the command line. Many Linux users already know about the eject command for opening the disk tray: $ eject How do you close the tray? Turns out that you can use the same eject command but with an additional -toption to close the tray. $ eject -t Before I found out about the -t option, I always had to reach and press the open/close button on the drive to close the tray. With the -t option, you can now both open and close the tray from the command line. There is another option -T (CAPITAL T, that is) you should know. eject -T basi... more »

Two additional ways to tail a log file

Peter Leung at Linux Commando - 5 years ago
I want to revisit a topic: how to tail a log file. My earlier posts discussed the use of tail and less commands to tail a log file, and multitail if you need to tail multiple files at once. This followup article discussed two other ways to tail a log file: the use of the most command, and to tail within emacs. The most command bills itself as a replacement for less. Like its predecessors less and more, most is a file pager program. To install most on my Debian Etch system: $ apt-get update && apt-get install most To page the log file say /var/log/messages: $ most /var/log/messag... more »

How to disable SSH host key checking

Peter Leung at Linux Commando - 5 years ago
Remote login using the SSH protocol is a frequent activity in today's internet world. With the SSH protocol, the onus is on the SSH client to verify the identity of the host to which it is connecting. The host identify is established by its SSH host key. Typically, the host key is auto-created during initial SSH installation setup. By default, the SSH client verifies the host key against a local file containing known, rustworthy machines. This provides protection against possible Man-In-The-Middle attacks. However, there are situations in which you want to bypass this verification s... more »

Upgrade individual packages for Debian-based systems

Peter Leung at Linux Commando - 5 years ago
If you are using Debian-based distributions (Debian, Ubuntu, etc), you are probably familiar with the apt-get update followed by the apt-get upgrade routine. That is what I regularly use to upgrade ALL packages that have an update available. But what if you only want to upgrade certain individual packages? apt-get upgrade will upgrade ALL or nothing. So, that is out of the question. What you need is apt-get install. apt-get install serves dual purposes: install and upgrade. It installs a package if it is not already installed. If it is already installed, apt-get install will upgrad... more »

How to get the process start date and time

Peter Leung at Linux Commando - 5 years ago
How can we determine when a running process was started? The venerable ps command deserves first consideration. Most Linux command-line users are familiar with either the standard UNIX notation or the BSD notation when it comes to specifying ps options. If ps -ef is what you use, that is the UNIX notation. $ ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 Sep20 ? 00:00:03 init [3] peter 1218 1 0 Sep20 ? 00:21:35 /usr/lib/iceweasel/firefox-bin -a firefox peter 4901 1 1 16:34 ? 00:01:12 /usr/bin/emacs-snapshot-gtk The STIME column displa...more »

How to find and delete all hard links to a file

Peter Leung at Linux Commando - 5 years ago
Deleting a file is deceptively simple. You can simply use the rm command like this. $ rm file1 However, if the file has one or more hard links to it, life gets more interesting. You need to seek and destroy all hard links to the file. A hard link is essentially another name for a file. Hard links to file1 can be created as follows: $ ln file1 file2 $ ln file1 tmp/file3 file2 and file3 become another name of file1. How do I know if some file has a hard link to it? Do a long listing of the file like this. $ ls -l file1 -rw-r--r-- 3 peter peter 0 2008-09-01 16:15 file1 Note ... more »

How I repaired a corrupted grub menu.lst config file

Peter Leung at Linux Commando - 5 years ago
Imagine the shock when I discovered my Debian Etch machine would not boot after I ran a "routine" apt-get upgrade. The upgrade involved quite a number of packages, including the kernel image. GRUB, the default boot loader, came to an abrupt stop with the error message Error 15: File Not found. The culprit was the file /[01;31mvmlinuz-[00m2.6.18-6-k7. The weird-looking file name suggested strongly that the GRUB config file, /boot/grub/menu.lst, got corrupted by the upgrade. At this point, I had the following options: - Use a rescue CD/DVD like Knoppix to boot into the system, corr... more »

How to show apt log history

Peter Leung at Linux Commando - 5 years ago
Users of Debian-based distributions (myself included) often brag about Debian's supposedly superior package management tool-set. This tool-set includes a choice of several excellent package managers such as dpkg, apt, synaptic, and aptitude. The various tools are all first class at what they are designed to do. In my opinion, one major feature gap is a command-line interface for viewing the apt change log. After a recent routine Etch package upgrade, I discovered that the grub menu.lst file got corrupted. So, I wanted to check the recent apt activities to find out which packages wer... more »

Learn more about a command when no man info page is available

Peter Leung at Linux Commando - 5 years ago
To read the documentation about a command, the first thing we do is to read its manual (man) page. $ man cd No manual entry for cd But what if no man page is available for that command? This could be due to a number of different reasons. For example, the Linux system was originally installed without any man pages at all. I worked with a Linux-based mobile gateway device that has 0 man pages pre-installed. This is to save disk space on a 1 GB Solid State Drive (SSD). In most typical desktops or servers, man pages are pre-installed. A man page can be missing for a particular comman... more »

Pondus: A personal weight management software

Peter Leung at Linux Commando - 5 years ago
A week after my return from a New England & Canada cruise, I found myself busy searching for a personal weight management software. Why weight management? See the chocolate dessert buffet pictures taken on board the Holland America Maasdam cruise ship. I came across pondus, a python program that is free and open-sourced. Pondus has a rather modest feature set: the ability to enter one's weight over time, and have it plotted in a chart. It does not have too many bells and whistles, but it is very simple to use. Pondus is packaged with certain Debian releases (namely, Lenny and Sid).... more »

How to do reverse DNS lookup

Peter Leung at Linux Commando - 5 years ago
Most people can better remember domain names, e.g., www.gnu.org, than their corresponding IP addresses, 199.232.41.10. (In this example, www.gnu.org is the home of the Free Software Foundation.) We delegate the responsibility to machines, aka, the DNS servers, to resolve the domain names for us. Sometimes, we do need to manually lookup the IP address of a domain name. You may already be familiar with the nslookup command which is now deprecated. We use the dig command to make DNS queries. $ dig +noall +answer www.gnu.org www.gnu.org. 67 IN CNAME gnu.org. gnu.o... more »

How to count number of files in a directory

Peter Leung at Linux Commando - 5 years ago
Now that I am back from vacation, I had to take care of some chores, like uploading the pictures taken with my digital camera. I stepped out during the long upload process (400+ pictures). When I returned, it was already done. To just make sure all pictures are now on the server, I wanted to count the number of files in the targetdirdirectory. $ ls -1 targetdir | wc -l 454 The above command reads ls dash one piped to wc dash letter l. Note that if you had used ls dash letter l instead, the count is one greater than the actual number of files. This is because ls dash letter loutput... more »

Dual pane Linux file managers: mc and emelfm

Peter Leung at Linux Commando - 5 years ago
I have been computing all my life without using a dual pane file manager until one day I decided that having one will greatly enhance my qualify of life. What I had in mind was something that will make my life easier in the copying or moving of files from 1 directory to another. By dual pane (or twin-pane) file manager, I mean a file manager that displays two directories side by side (one active, one passive). The term is confusing because often a dual pane file manager has a third pane that lets you enter commands to execute on the active directory. Staying true to my preference fo... more »

Run emacs in batch mode to byte-compile elisp files

Peter Leung at Linux Commando - 5 years ago
emacs is my favorite text editor. (No flame please.) Little known and used perhaps is the fact that emacs does run in batch mode. By batch, I mean emacs accepts and executes commands in the command line, without any user interaction. You can emulate a typical emacs text editing session as follow: $ emacs -batch afile.txt -l runme.el -f save-buffer -kill This command opens the afile.txt in batch emacs mode. The -l parameter specifies an elisp file to load and execute. In this example, it loads the elisp file named runme.el which modifies the file buffer. Next, it saves the buffer. ... more »

Create File of a Given Size ... with random contents

Peter Leung at Linux Commando - 5 years ago
A while back, I wrote about how to create a zero-filled file of any arbitrary size. This is part 2 where I share how to create a file of random contents (not just zeroes). Recently, I ran into a situation where a zero-filled file is insufficient. I needed to create a log file of size 2 MB in order to be zipped up and copied to another server. To create the 2MB file (with all zeroes), I run the dd command: $ dd if=/dev/zero of=a.log bs=1M count=2 I quickly realized that the test result would be invalid because zipping a all-zero file dramatically reduced its size. $ gzip a.log $ ls ... more »

Smart case-insensitive, incremental search using vim

Peter Leung at Linux Commando - 5 years ago
My previous article describes my top annoyance with the vim text editor, namely, its syntax highlighting. In this article, I will tell on a close second annoyance, and what to do about it. vim, by default, searches case sensitively. If you search for apple, you will find exactly that, but not Apple or APPLE. In most situations, I want my searches to be case insensitive. To make search case sensitive, set the corresponding vim option by typing :set ignorecase (and press the return key). ignorecase has a shorter alias called ic. You can type :set ic and it will have the same effect. ... more »

How to find a file and cd to its dirname using command substitution

Peter Leung at Linux Commando - 5 years ago
Many times I know the name of a file on my Linux machine, say unknown.txt, but I don't know the directory the file is in. If I want to change directory to the directory folding the file, I used to do a 2-step process: $ find / -name unknownfile.txt 2>/dev/null /home/peter/status/2007/november/unknownfile.txt Note: if you know more about where that file may be, you can always make the starting point of the find more specific (say "/home/peter" instead of just "/"). Then, I manually enter the cd command with the path discovered from the last step. $ cd /home/peter/status/2007/nove... more »

Show progress during dd copy

Peter Leung at Linux Commando - 5 years ago
dd is a popular, generic command-line tool for copying files from 1 location to another. It is often used to copy entire disk images. Like many Linux command line tools, it operates silently unless something unexpected happens. Its lack of visual progress feedback is a nice feature for scripting. However, it can leave you wondering about its progress if you are interactively dd-copying a large disk. To illustrate, you run the following (valid, but perhaps not very useful) ddcopy: $ dd if=/dev/random of=/dev/null bs=1K count=100 It will run for a few minutes as it copies (and imme... more »

How to number each line in a text file on Linux

Peter Leung at Linux Commando - 5 years ago
Some Linux commands support options that will number the input lines as a side effect, e.g., grep, and cat. The nl command is on the other hand dedicated to the task of numbering lines in a text file. If you want maximum flexibility, sed or perl is your best bet. Suppose you want to number the lines in the input.txt file which contains: 123 456 789 abc def ghi Below are some ways to number input.txt: - cat -n $ cat -n input.txt 1 123 2 3 456 4 5 789 6 7 8 abc 9 10 def 11 ... more »

How to disable vim syntax highlighting and coloring

Peter Leung at Linux Commando - 5 years ago
Syntax highlighting is my top annoyance in using vi/vim. Syntax highlighting is just a fancy term meaning that the text editor will auto-color parts of a text file according to some rules that makes sense to it, using some default color scheme. To be precise, only vim, not vi, has syntax highlighting. vi has a 2 color scheme only: background and foreground. Yet, on my Centos 4 system (and many other distros), the vi command is just a soft link to vim. Syntax highlighting is useful, and usually nothing to complain about. However, I find the default vim color scheme to be an eye-kille... more »

Use the OR operator in grep to search for words and phrases

Peter Leung at Linux Commando - 5 years ago
grep is a very powerful command-line search program in the Linux world. In this article, I will cover how to use OR in the grep command to search for words and phrases in a text file. Suppose you want to find all occurrences of the words "apples" and "oranges" in the text file named fruits.txt. $ cat fruits.txt yellow bananas green apples red oranges red apples $ grep 'apples\|oranges' fruits.txt green apples red oranges red apples Note that you must use the backslash \ to escape the OR operator (|). Using the OR operator, you can also search for phrases like "green apples" and... more »

Root edit a file using emacs in the same session

Peter Leung at Linux Commando - 5 years ago
We know that we should always log in using our regular non-root account, and only sudo in when necessary to do things that only root can do. Most of the time, you are logged in as a regular user, and you have your emacseditor open. Now, you realize that you need to edit a file which is only writable by root(say /etc/hosts.allow). What you can always do is to open up another emacs session with the right credential, and edit the file there: $ sudo emacs /etc/hosts.allow This becomes a little tedious, doesn't it? A nifty little trick is to use tramp, an emacs package for transparent ... more »

Delete Windows/DOS carriage return characters from text files

Peter Leung at Linux Commando - 5 years ago
Different operating system may use different characters to indicate the line break. Unix/Linux uses a single Line Feed (LF) character as line break. Windows/DOS uses 2 characters: Carriage Return/Line Feed (CR/LF). MacOS uses CR. Nowadays, it is a reality that we operate on multiple platforms. If you transfer a text file created on a Windows machine to a Linux machine, the file will contain those extra Carriage Return characters. Some Linux programs run just fine with those characters in their input, but some are less forgiving. Below are various ways to remove the Carriage Control... more »

Run ifconfig as non-root user for read-only access to network interfaces

Peter Leung at Linux Commando - 5 years ago
It is a frequent scenario that you are logged in to the console of a Linux system, and you need to know its IP address. If you are the root user, that is easy: $ ifconfig eth0 Link encap:Ethernet HWaddr 00:0B:6B:E1:BC:14 inet addr:192.168.0.103 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20b:6aff:fed0:bb04/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8100 errors:0 dropped:0 overruns:0 frame:0 TX packets:7727 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 ... more »

Ping or nmap to identify machines on the LAN

Peter Leung at Linux Commando - 5 years ago
You can use ping or nmap to find out what machines are currently on the local network. The first method involves pinging the LAN broadcast address. To find out the broadcast address of the local network: $ ifconfig eth0 eth0 Link encap:Ethernet HWaddr 01:1B:6B:D8:B1:26 inet addr:192.168.0.103 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20b:6aff:fed0:bb04/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:70324 errors:0 dropped:0 overruns:0 frame:0 TX packets:69429 errors:0 dropped:0 overruns:...more »

How to indent lines in text files using sed, awk, perl

Peter Leung at Linux Commando - 5 years ago
Suppose I have a text file named input.txt, and I want to indent each line in the file by 5 spaces. $ cat input.txt 12 34 56 78 The following are different ways to do the same thing: - sed $ sed 's/^/ /' input.txt 12 34 56 78 s/^/ / searches for the beginning of a line (^), and "replaces" that with 5 spaces. - awk $ awk '{ print " " $0 }' input.txt 12 34 56 78 - perl $ perl -pe 's/^/ /' input.txt 12 34 56 ... more »

How to convert text files to all upper or lower case

Peter Leung at Linux Commando - 5 years ago
How can you convert a text file to all lower case or all upper case? As usual, in Linux, there are more than 1 way to accomplish a task. To convert a file (input.txt) to all lower case (output.txt), choose any ONE of the following: - dd $ dd if=input.txt of=output.txt conv=lcase - tr $ tr '[:upper:]' '[:lower:]' < input.txt > output.txt - awk $ awk '{ print tolower($0) }' input.txt > output.txt - perl $ perl -pe '$_= lc($_)' input.txt > output.txt - sed $ sed -e 's/\(.*\)/\L\1/' input.txt > output.tx... more »

How to prevent Linux man pages from clearing after you quit reading

Peter Leung at Linux Commando - 5 years ago
Man pages are excellent resources for learning the specifics of a Linux command. After all, who can remember all the nitty gritty of a command? One annoyance of reading man pages on some Linux distributions is that after you quit reading it, the contents are cleared off screen. The manpage contents simply don't stay around after you quit man. If that happens to you, it means that the default pager for viewing manpages is the less command, and that is how less behaves. Wiping man contents out or not is a personal preference. Some may like the man stuff being wiped out because it won'... more »

bash quicksand 2: Quotes needed in string tests

Peter Leung at Linux Commando - 5 years ago
This is part 2 of the bash quicksand series. Part 1 is about whitespaces in variable assignment. When you write bash shell scripts, you want to avoid these innocent-looking mistakes. We all fall into these traps at some point, especially when we first write bash scripts. You define a bash variable, say $somevar. Later, you need to test if it is equal to some string, say "linux". 1 #!/bin/bash ... 5 if [ $somevar == "linux"] 6 then ... 9 fi ... 25 exit 0 Depending on the value of $somevar at the time of the test, you can see some puzzling errors: - If $somevar is n...more »

How to Display Routing Table

Peter Leung at Linux Commando - 5 years ago
To display the kernel routing table, you can use any of the following methods: - route $ sudo route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0 You need to be root to execute route. The -n option means that you want numerical IP addresses displayed, instead of the corresponding host names. - netstat $ netstat -rn Ker... more »

Compare Directories using Diff in Linux

Peter Leung at Linux Commando - 5 years ago
To compare 2 files, we use the diff command. How do we compare 2 directories? Specifically, we want to know what files/subdirectories are common, what are only in 1 directory but not the other. Unix old-timers may remember the dircmp command. Alas, that command is not available in Linux. In Linux, we use the same diff command to compare directories as well as files. $ diff ~peter ~george Only in /home/peter: announce.doc diff /home/peter/.bashrc /home/george/.bashrc 76,83d72 < < # Customization by Peter < export LESS=-m < export GREP_OPTIONS='--color=always' < shopt -s histappend < s... more »

Using awk to extract lines in a text file

Peter Leung at Linux Commando - 5 years ago
awk is not an obvious choice as a tool for strictly extracting rows from a text file. It is better known for its column/field manipulation capabilities in a text file. More obvious choices are sed, and perl. You can see how sed does it in my earlier entry. If you opt for awk, you can use its NR variable which contains the number of input records so far. Suppose the text file is somefile: $ cat > somefile.txt Line 1 Line 2 Line 3 Line 4 To print a single line number, say line 2: $ awk 'NR==52' somefile.txt Line 2 If the text file is huge, you can cheat by exiting the program on ... more »

Fast way to execute sequential commands from the command history

Peter Leung at Linux Commando - 5 years ago
When I had to navigate to a subdirectory in a deep hierarchy, I found myself do a lot of cd's. $ cd /home/peter $ cd level1 $ cd level2 $ cd level3 $ cd level4 $ pwd /home/peter/level1/level2/level3/level4 If I ever need to revisit that level4 subdirectory, re-running those same commands in the same order from the command history can be a real chore. You have to keep finding your way back in history to execute the next command. bash has a short cut, Cntl-O (as in Control-Oh), that is your friend here. Simply go back to the first command in the series (cd /home/peter). Hit Cntrl-O (... more »

Quick hex / decimal conversion using CLI

Peter Leung at Linux Commando - 5 years ago
Once in a while, you need to convert a number from hexadecimal to decimal notation, and vice versa. Say, you want to know the decimal equivalent of the hexadecimal 15A. You can convert in many different ways, all within bash, and relatively easy. To convert a number from hexadecimal to decimal: $ echo $((0x15a)) 346 $ printf '%d\n' 0x15a 346 $ perl -e 'printf ("%d\n", 0x15a)' 346 $ echo 'ibase=16;obase=A;15A' | bc 346 Note that ibase and obase specify the input and the output notation respectively. By default, the notation for both is decimal unless you change it usin... more »

Extracting columns and fields from a text file

Peter Leung at Linux Commando - 5 years ago
I posted about extracting lines from a text file ([1], [2]). Enough about lines for now. Let's turn our attention to extracting columns and delimited fields in a text file. For instance, one task is to extract columns 5 to 7 in a file. Sometimes, the data you want reside in variable-length fields that are delimited by some character, say ",". A sample task is to extract the second field in a comma-delimited file. As usual, there are more than 1 way to accomplish the tasks. The tools that we will use are cut, awk, and perl. The text file is somefile. $ cat > somefile 1234567890 123... more »

Use sed or perl to extract every nth line in a text file

Peter Leung at Linux Commando - 5 years ago
I recently blogged about the use of sed to extract lines in a text file. As examples, I showed some simple cases of using sed to extract a single line, and a block of lines in a file. An annoymous reader asked how one would extract every nth line from a large file. Suppose somefile contains the following lines: $ cat > somefile line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 $ Below, I show 2 ways to extract every 4th line: lines 4 and lines 8 in somefile. 1. sed $ sed -n '0~4p' somefile line 4 line 8 $ 0~4 means select eve... more »

bash quicksand 1: whitespaces in variable assignment

Peter Leung at Linux Commando - 5 years ago
bash is a very powerful scripting language. However, there is a learning curve that frustrates many. In a series of bash quicksand blog entries, I hope to post some warning signs about what to avoid. I want to write them down because after one gets used to bash syntax, one tends to forget about the initial hurdles. Hopefully, no late comers will be trapped in bash quicksand. Let's begin. Say you want to assign the text string myvalue to some variable myvariable.What is more simple than that? $ myvariable = myvalue bash: myvariable: command not found $ Oops. And you vowed that it... more »

Trick grep not to report itself in a process search

Peter Leung at Linux Commando - 5 years ago
How often have you searched for a process, using ps and grep? $ ps -ef |grep emacs peter 7107 1 1 09:10 ? 00:00:08 /usr/bin/emacs-snapshot-gtk peter 7377 7050 0 09:19 pts/0 00:00:00 grep emacs It always reports one more process than you want to see ... namely the grep process itself. In the example above, the process 7377 is the grep process itself. What you really want is the 7107 emacs process. While this is really harmless (albeit annoying), it can be a real pain if you put this in a script. In that case, you have to parse out the grep process itself. ... more »

How to check the exit status code

Peter Leung at Linux Commando - 5 years ago
When a command finishes execution, it returns an exit code. The exit code is not displayed on the screen by default. To examine the exit code, you need to examine a special variable, "$?" Say, you are searching for a string in a text file. $ grep x1y2z3 somefile.txt $ The standard output of the command returns null, which is a pretty good indication that the string cannot be found in the file. But what if you embed the grep command in a script? How can you tell if the string is found or not? Checking the exit code will tell you. Let's first try it out interactively. $ grep x1y2z... more »

Using sed to extract lines in a text file

Peter Leung at Linux Commando - 6 years ago
If you write bash scripts a lot, you are bound to run into a situation where you want to extract some lines from a file. Yesterday, I needed to extract the first line of a file, say named somefile.txt. $ cat somefile.txt Line 1 Line 2 Line 3 Line 4 This specific task can be easily done with this: $ head -1 somefile.txt Line 1 For a more complicated task, like extract the second to third lines of a file. head is inadequate. So, let's try extracting lines using sed: the stream editor. My first attempt uses the p sed command (for print): $ sed 1p somefile.txt Line 1 Line 1 Line ... more »

It is about Time ... a process

Peter Leung at Linux Commando - 6 years ago
Sometimes, it is easy to overlook the simple Linux commands. Take the command time, for example. It simply times how long a command takes to run, and gives you 3 statistics: 1. Elapsed real time (in seconds). 2. Total number of CPU-seconds that the command spent in user mode. 3. Total number of CPU-seconds that the command spent in kernel mode. I use time a lot to benchmark network performance, e.g., $ time scp some-file peter@192.168.22.104:/home/peter/some/location/ real 0m17.742s user 0m0.364s sys 0m0.476s You can be creative with time, and run it li... more »

Remap Caps Lock key for virtual console windows

Peter Leung at Linux Commando - 6 years ago
My last blog entry explains how to use xmodmap to remap the Caps Lock key to the Escape key in X. That takes care of the keyboard mapping when you are in X. What about when you are in a virtual console window? You need to follow the steps below. Make sure that you sudo root before you execute the following commands. 1. Find out the keycode of the key that you want remapped. Execute the showkey command as root in a virtual consolde: $ showkey kb mode was UNICODE press any key (program terminates after 10s of last keypress)... 0x9c Hit th... more »

Remap useless Caps Lock key in X

Peter Leung at Linux Commando - 6 years ago
Caps Lock is in my opinion one of the most useless keys on a keyboard. Unless you own one of those keyboards specifically designed for Linux, your keyboard most likely has a Caps Lock key located in a prime real estate area: right above the Shift key. How do we remap the Caps Lock key to something more useful, say the Esckey? Why the Esc key? See my earlier article. First, specify the new key mapping in the file ~/.Xmodmap: $ cat >> ~/.Xmodmap remove Lock = Caps_Lock keysym Caps_Lock = Escape Note that the file ~/.Xmodmap may not pre-exist in your distro. The above command will c... more »

Clear screen without losing the current command

Peter Leung at Linux Commando - 6 years ago
The command clear clears the screen. An alternative way to clear the terminal screen is through a shortcut: Control-L. Control-L gives you a bonus: if you are in the middle of typing a long command, hitting Control-L clears the screen without erasing what you already enter on your current command. Whatever you typed in as the current command will appear at the top of the screen. So, you can finish typing in your command and hit enter to execute it with a pristine screen real estate. [image: StumbleUpon Toolbar]

Go to next/previous word on the command line

Peter Leung at Linux Commando - 6 years ago
I have been looking for a way to move forward and backward on the command line by bigger chunks than just character by character. Something like go back a word, or go to next word. I finally found it: - to go back a word, press Esc, then the letter b - to go forward a word, press Esc, then the letter f I must say that the key sequence is some what awkward to hit right. Never mind about repeating it to move back/forward several words. With that said, I found what I was looking for. [image: StumbleUpon Toolbar]

Save your command for later execution

Peter Leung at Linux Commando - 6 years ago
Sometimes, you change your mind when you are in the middle of typing in a command at the command prompt. You don't want to execute the command until a later time, but you don't want to retype the line again. What to do? You can hit Control-C to abandon the command. The problem with Control-C is that what you have entered already is lost: the line won't go to your command history. A neat trick is to comment the line out. First, hit Control-A to go to the start of the line, and then enter # (the "pound sign"). This essentially makes the entire line a comment line. The line will not be... more »

Super Fast way to open a file for vi at a given line

Peter Leung at Linux Commando - 6 years ago
Sometimes, you know the line you want to edit in a file, and you just need to open the file and get to that line. For example, you just had done a grep -n, and found the line you want, say line 99 of a file. Sure, you can vi the file, and then type 99G (capital G, that is), to get there. A much faster way is enter this in the command prompt: $ vi +99 some-file The above opens some-file and go immediately to line 99. [image: StumbleUpon Toolbar]

Create File of a Given Size

Peter Leung at Linux Commando - 6 years ago
Last week, I needed to test the speed of my VPN connection. My plan was to create a file of some given size (say 10M), and test copy it to another server across the VPN tunnel. My first task was to create a file of size 10M. On Solaris, it can be done simply by this command: $ mkfile 10m output.dat On Linux, you can use the dd command: $ dd if=/dev/zero of=output.dat bs=1024 count=10240 10240+0 records in 10240+0 records out 10485760 bytes (10 MB) copied, 0.218581 seconds, 48.0 MB/s $ ls -hl output.dat -rw-r--r-- 1 peter peter 10M 2008-02-09 16:21 output.dat The above dd com... more »

Part 2: How to work with Access Control Lists from the Command Line

Peter Leung at Linux Commando - 6 years ago
Part 1 of this article introduced Access Control Lists (ACL), explained how to enable ACL on file systems, and define the ACL for files. Part 2 of this 2-part article shows how to define the Access Control List for directories,and how to remove the ACL. Often, you want to share files among certain groups and specific users. It is a good practice to designate a directory for that purpose. You want to allow those groups and users to read, and write files in that directory, as well as create new files into the directory. Let's first create the directory named projections. $ mkdir proje... more »

Part 1: How to work with Access Control Lists from the Command Line

Peter Leung at Linux Commando - 6 years ago
The basic Linux permission model lets you specify permissions for the file's owner and group, and all others. This article assumes that you are familiar with the basic permissions, and know how to set them. The Access Control List (ACL) feature extends the model to allow much finer control: you can specify permissions for each individual user and group defined in your system. Consider this scenario: your server supports multiple office departments: Sales, Marketing, and Helpdesk. Each department has a manager, and one or more staff members. You define a group for each department th... more »

How to Change Mount Options at Runtime

Peter Leung at Linux Commando - 6 years ago
File systems need to be mounted on Linux before you can access the data on them. You can specify mount options such as whether the file system is Read Only or Read/Write, and whether to support Access Control List, etc. To see what file systems are currently mounted, where, and with what options, issue the mount command without arguments: $ mount ... /dev/hda5 on /home type ext3 (rw,acl) ... Recently, I wanted to tell the file system not to track the last access time (atime) of files under /home. I did not have any good use for the last access time. So, I opted to disable its trac... more »

How To Mount USB flash drive from Command Line

Peter Leung at Linux Commando - 6 years ago
Mounting a USB flash drive in GNOME (or another Linux desktop environment) is as easy as plug and play. Yet, occasionally, you need to mount one on a server which does not run X, then you must know how to do it on the command line. 1. Become root. $ sudo -s 2. Plug in USB drive to a USB port. 3. Identify the correct partition name corresponding to the USB drive. For my Debian system, it is sda, and partition 1. $ dmesg |grep -i 'SCSI device' ... SCSI device sda: 3903488 512-byte hdwr sectors (1999 MB) Alternatively, ... more »
Blogger Home. 
s3v3n

s3v3n
copyright©

Tiada ulasan: