» Publishers, Monetize your RSS feeds with FeedShow: More infos (Show/Hide Ads)
Hi, just wanted to make sure I give you enough time to update your links - most of the content of this website will be integrated back into my Unix Tutorial website, as this domain will become inactive in two weeks or so.
Unix links
Here's what you should use from now on:
ouch command is one of these little but extremely useful tools in Unix which you may have used for quite sometime before realizing their full potential. In short, it updates file timestamps - access and modification ones (atime and mtime respectively).
Why modify file timestamps?
There are quite a few legitimate reasons why you may want to update timestamps on a certain file. Ranging from source control approaches to storage usage analysis, there are processes out there which rely on the timestamps associated with each file and directory of yours.
After all, it's always useful to know when the file was last modified or when somebody tried to access its contents.
Changing timestamps of a time to the current system time
The default behavior of touch command is to change all three timestamps associated with a file to the current system time.
You simply specify the filename as a command line parameter, no oother options are needed. If there isn't a file with the specified name, touch command will create it for you if permissions allow it:
ubuntu$ ls try ls: try: No such file or directory ubuntu$ touch try ubuntu$ ls try try ubuntu$ stat try File: `try' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 655596 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin) Access: 2008-11-17 08:03:02.000000000 -0600 Modify: 2008-11-17 08:03:02.000000000 -0600 Change: 2008-11-17 08:03:02.000000000 -0600 ubuntu$ date Mon Nov 17 08:03:05 CST 2008
As you can see from the example, the file which isn't originally found, gets created by the touch command and gets its timestamps set to the current system time and date.
Changing file timestamps to a specific date and time
If you have a specific time and date you would like to be used for all the timestamps of a file or directory, touch command will gladly accempt a timestamp template with -t command line option.
Template for the timestamp is quite thorough: [[CC]YY]MMDDhhmm[.ss], but it's entirely up to you whether to specify the year (either two-digit or a full form) or not.
This example resets the date to October 16th:
ubuntu$ touch -t 10161000 ./try ubuntu$ stat ./try File: `./try' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 655596 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin) Access: 2008-10-16 10:00:00.000000000 -0500 Modify: 2008-10-16 10:00:00.000000000 -0500 Change: 2008-11-18 03:54:10.000000000 -0600
As you can see from the output, both access time and modification time got updated. The reason change time (ctime) is set to a different date is because this field reflects the last update to the inode behind a file, and always reflects the current time. In other words, it's set to Nov 18th 2008 because of the date of writing this example.
If you fancy adding a year to the timestamp specification, you can specify something from both past and future.
Here's how easy it is to set atime and mtime to the Oct 16th, 2010 date:
ubuntu$ touch -t 201010161000 ./try ubuntu$ stat ./try File: `./try' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 655596 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin) Access: 2010-10-16 10:00:00.000000000 -0500 Modify: 2010-10-16 10:00:00.000000000 -0500 Change: 2008-11-18 03:57:30.000000000 -0600
Modifying atime of a file in Unix
Similar to the commands above, you can use -a option to make touch only update the access time field of a file:
ubuntu$ touch -at 200010161000 ./try ubuntu$ stat ./try File: `./try' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 655596 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin) Access: 2000-10-16 10:00:00.000000000 -0500 Modify: 2010-10-16 10:00:00.000000000 -0500 Change: 2008-11-18 04:05:22.000000000 -0600
Modifying mtime of a file in Unix
If you're interested in updating the modification date only, use -m option:
ubuntu$ touch -mt 200510161000 ./try ubuntu$ stat ./try File: `./try' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 655596 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin) Access: 2000-10-16 10:00:00.000000000 -0500 Modify: 2005-10-16 10:00:00.000000000 -0500 Change: 2008-11-18 04:07:12.000000000 -0600
Using a reference file to set atime and mtime
Finally, the really useful option for synchronizing access and modification time fields between multiple files is to use reference file. A reference file is the file which already has the timestamps you'd like to copy:
ubuntu$ stat /etc/lsb-release File: `/etc/lsb-release' Size: 97 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 1278451 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2008-11-14 05:30:09.000000000 -0600 Modify: 2007-04-12 01:02:52.000000000 -0500 Change: 2007-09-26 02:41:20.000000000 -0500
By specifying this file using a -r option, you can use the touch command to set the same atime and mtime values to any file of yours:
ubuntu$ touch -r /etc/lsb-release ./try ubuntu$ stat ./try File: `./try' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 655596 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin) Access: 2008-11-14 05:30:09.000000000 -0600 Modify: 2007-04-12 01:02:52.000000000 -0500 Change: 2008-11-18 04:09:02.000000000 -0600
See also:
Here are the articles I found interesteding this week, leave a comment with a link if you have something interesting to share:
- Tux Training: Configs to tweak Firefox 3
- Dependencies maps for popular PHP-based software - if you ever wanted to see how a certain file is used in WordPress, that’s a great way to finally find out
- DLM: 10 Tips to Move Your Blog to Stardom
- Justin Tadlock: Tips on Using and Developing WordPress happily - great post for WordPress blog owners
- Why Reputable SEO firms won’t promise guaranteed search engine ranking - a great article from SEOmoz demystifying some aspects of SEO
- Lifehacker: Enable Chrome’s Best Features in Firefox
Gone are the days of manually clipping the WordPress logo from the official website! Last week I’ve discovered a wonderful page with all the official WordPress logos and buttons offered in PDF (with vector graphics) and PNG versions of everything.
There’s a set of WordPress wallpapers and a neat table with current colors used by WordPress engine.
Like WordPress? Find out more:
Looks like WordPress 2.6.2 with a few fixes is out, so I’ve just upgraded all my blogs to this new milestone.
For all the details, read the official WordPress 2.6.2 announcement or glance through the list of bugs fixed in 2.6.2 release.
See also:
It was bound to happen: I’ve joined Twitter! It would be great to hear from you there!
Follow me or leave your Twitter name in comments below so that I can follow you:
Looks like Google has taken one more shortcut to providing best service to its fans: as of yesterday, Google’s very own Internet browser codenamed Google Chrome, is available for download.
What is Google Chrome?
My first impression is that Google Chrome is a slick ultra-light browser with minimalistic yet intuitive interface and a minimal set of settings. There’s no fixed status bar, there’s no main menu, there’s just the address bar (called Omnibox because it also combines a search bar).
The tabbing works very smoothly, and overall you kind of feel there’s something missing simply because there’s only the page content and a very subtle set of controls. You get used to such a simplicity very quickly, though.
Here’s how Google Chrome looks:
Performance of Google Chrome vs Firefox
I haven’t noticed much of a performance boost yet, but maybe it’s just because I need to play with this new browser a bit more. All the pages load quickly, but I’ve yet to see the ones which load much better than in my Firefox. ZDnet did some testing already and it shows that Google Chrome is quite fast.
What’s really cool is the really simple interface and intuitive searching - as you type a URL, Omnibox tries to guess what website you’re trying to get to. Works like a charm for many well known websites!
One of the main reasons Google came up with its own browser is performance of Google services and apps in modern browsers. Firefox is not ideal, although with a bit of tweaking you can get it to work pretty fast. Is it very likely that Google Chrome, being a highly specialized product, will be the best for GMail, Google Calendar and other services - but it may take some getting used to. Google also claims Chrome will be better for most websites, so it does seem like the optimizations will have a generic nature rather than a Google-specific services customization - I think it’s great news.
That’s it! Have a look at the browser itself, I think it’s a great move for Google, but would hate to see it as a direct competition to my favourite Firefox. I think the fact that these two products both called web browsers still doesn’t make a fair apples-to-apples comparison because Firefox has got quite a history and is much more universal as it is. I’m a long way from changing my preference for Firefox to any other browser, but must admit that Google Chrome seems to have done quite a neat and easy to use browser - time will show what Google will make of it.
See also:
man is one of the very first Unix commands everyone learns. It shows a brief manual for most of the commands available on your Unix-like OS and provides cross-references to other similar manuals.
What is man command used for
Many Unix commands are quite useful even if run without command line parameters, but as soon as you reach the next comfort level, you start wondering about extracting even more from the same command. This is when reading manuals becomes vital, and man offers a great way to explore them.
All Unix-like systems are provided with extensive manuals. Even if you don't have the material printed on paper, you still have plenty of information installed with your OS.
Most usually, manual pages are found under /usr/share/man directory. Manuals are not presented in clear text - there's a markup language commands which should be interpreted by the man command.
What is a Unix manual page (manpage)
All the manuals for Unix commands are split into clearly marked sections:
- NAME - command name as it should be typed
- SYNOPSIS - syntax for running a command - all the possible command line options
- DESCRIPTION - textual description of what a command is used for
- OPTIONS - full list of command line options with thorough explanations
- FILES - files which are used by a command
- SEE ALSO - other relevant commands you might want to look at
- BUGS - known bugs and limitations of a command
- AUTHOR - list of command authors, developers and most current maintainers
A manual page generated by man command is nothing but a clear text you can access from most basic Unix shell session, formatted as per your terminal capabilities (most often you see that section names are shown in bold font, but that's about it).
Using man command in Unix
The simplest way to get help in Unix is to run man command and specify a name of another Unix command as a parameter. Naturally, the first thing you should do is to have man show you a manual for itself:
ubuntu$ man man
This is how it would look (click the image o get the full resolution):
See also:
runlevel is a basic Unix command aimed to do one simple task: report the runlevel of your Unix OS.
How runlevel command works
Your Unix system carefully logs information about every login session in special files. /var/run/utmp is the file containing information about everyone who's currently logged in, and since every record contains a runlevel information, it makes sense to use this file as a proof of the current OS runlevel.
runlevel command reads /var/run/utmp file and extracts the most recent login entry. It then uses this entry to extract the current and previous runlevel information from it.
Using runlevel command
Simply run the command without any parameters:
redhat$ runlevel N 5
As it was said earlier, the two numbers shown are supposed to be previous and current Unix runlevels.
However, the previous runlevel information is not usually found in the most recent login entry simply because runlevel hasn't changed,
so the command prints "N" instead of it.
Looking at the output above, you can see that the current runlevel is 5.
See also:
- uptime - find out how long the system has been up
- who - find out who is logged into the system
- system status and monitoring commands in Unix
After the last round of reviewing my online projects, I’ve decided to change not only focus but WordPress theme for PerfectBlogger.
As of today, this website will have Brian Gardner’s Vertigo theme. My main reason for changing the look of Perfect Blogger is to declutter the pages of this blog, so have a look and let me know!
time command is a basic tool in Unix which allows you to keep track of the system resources when running a specified Unix command.
time command in Unix
Sometimes it is quite important to know exactly how much of your system resources are used for running a particular command. This is where the time command can be used. It's a really simple tool which takes any command line as a parameter, runs the command and then reports the system resources usage:
ubuntu# time du -sk /var 4228720 /var real 0m17.747s user 0m0.010s sys 0m0.080s
In this example, a du command is run to gather the cumulative disk space taken up by the /var directory, and a report of used time is presented.
This is what each of these times mean:
- real - real time, in other words a number of seconds, minutes and sometimes hours and even days it takes for the specified command to complete
- user -user time, that is the time spent by your OS executing the user code of your command - every instruction of the specified command which was executed in user mode.
- sys - system time, the amount of time spent by your OS running a system kernel code - instructions in response to the system calls initiated by your command
Real time vs user time vs system time
As you can see, the real time is not a sum of the other two - this is because only the system resources are reported, which is essentially just the CPU time.
Since we ran the I/O intensive command, most of the time it took for du to complete was spent waiting for the I/O operations to complete - as you can see from the example, the CPU time was minimal.
While the necessary file and directory attributes were being read from the disk, both the user time and the system time counters for the command were not clocking anything - your OS process scheduler was busy spending valuable CPU cycles to execute code for other processes.
See also:
- uptime - find out how long the system has been up
- top - show tasks and system status
- Basic Unix commands
For all of you who were hoping to give Wordze keyword research service a try, here’s your next chance!
If you register for Wordze between now and July 14th, you’ll have to pay only $4 for the month of July, with $38.98 for every month after (you can cancel at the end of July if you change your mind).
I find Wordze to be one of the most useful and affordable tools online, and thanks to offers like this almost everyone can give it a try at virtually no cost.

Sam Allen from Dot NET Pearls has written a great program to observe the memory taken up by each of the 5 most popular browsers. His experiment was to taken snapshots of memory usage numbers every 3 seconds for a period of 3 hours. The graphs posted on his Firefox 3 Memory Benchmarks and Comparison page are quite interesting, particularly showing that Firefox 3 is ahead of all the competitors with its rather stable and humble memory requirements.
I’ve been using Firefox 3 full-time since RC2, and must say I’m really impressed with its stability and performance.
Link: Firefox 3 Memory Benchmarks via Slashdot: Real-World Firefox 3 Memory Usage.
Just a reminder: today, June 17th 2008, is the official Firefox 3 download day - Mozilla foundation attempts to set a new Guinness record by having the latest release of Firefox downloaded the most within 24 hours.
You still have the time to make a pledge and download it: Firefox 3 download day.
top is a basic Unix command which is very useful for observing the current state of your Unix system, by default presenting you the list of top users of your system's resources - CPU shares and memory.
Basic usage of the top command
By default, you run top without any parameters, and it shows you a full screen (or full window of your terminal) with the current status of your system and a list of processes using most of its CPU:
ubuntu$ top
top - 13:29:09 up 2 days, 7:13, 4 users, load average: 0.07, 0.02, 0.00
Tasks: 148 total, 1 running, 147 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.6%us, 0.5%sy, 0.0%ni, 97.3%id, 1.6%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 4051792k total, 4026104k used, 25688k free, 359168k buffers
Swap: 4096492k total, 24296k used, 4072196k free, 2806484k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7629 greys 20 0 749m 291m 28m S 1 7.4 16:51.40 firefox
19935 greys 20 0 133m 14m 10m S 0 0.4 2:38.52 smplayer
1 root 20 0 4020 880 592 S 0 0.0 0:00.96 init
2 root 15 -5 0 0 0 S 0 0.0 0:00.00 kthreadd
3 root RT -5 0 0 0 S 0 0.0 0:00.04 migration/0
4 root 15 -5 0 0 0 S 0 0.0 0:00.90 ksoftirqd/0
5 root RT -5 0 0 0 S 0 0.0 0:00.00 watchdog/0
6 root RT -5 0 0 0 S 0 0.0 0:00.06 migration/1
7 root 15 -5 0 0 0 S 0 0.0 0:01.32 ksoftirqd/1
8 root RT -5 0 0 0 S 0 0.0 0:00.00 watchdog/1
9 root 15 -5 0 0 0 S 0 0.0 0:02.14 events/0
10 root 15 -5 0 0 0 S 0 0.0 0:01.44 events/1
11 root 15 -5 0 0 0 S 0 0.0 0:00.00 khelper
44 root 15 -5 0 0 0 S 0 0.0 0:01.26 kblockd/0
45 root 15 -5 0 0 0 S 0 0.0 0:01.98 kblockd/1
48 root 15 -5 0 0 0 S 0 0.0 0:00.00 kacpid
49 root 15 -5 0 0 0 S 0 0.0 0:00.00 kacpi_notify
153 root 15 -5 0 0 0 S 0 0.0 0:00.00 kseriod
203 root 15 -5 0 0 0 S 0 0.0 0:03.56 kswapd0
246 root 15 -5 0 0 0 S 0 0.0 0:00.00 aio/0
247 root 15 -5 0 0 0 S 0 0.0 0:00.00 aio/1
1595 root 15 -5 0 0 0 S 0 0.0 0:00.00 ksuspend_usbd
1601 root 15 -5 0 0 0 S 0 0.0 0:00.02 khubd
1612 root 15 -5 0 0 0 S 0 0.0 0:00.08 ata/0
1615 root 15 -5 0 0 0 S 0 0.0 0:08.28 ata/1
1616 root 15 -5 0 0 0 S 0 0.0 0:00.00 ata_aux
Output of the top command explained
These are the elements which default top output consists of:
Unix system uptime and average load
This is the line of top which confirms how many hours (or even days!) your system has been up, shows you the number of logged in users, and reports the average system load numbers for the last minute, 5 minutes and 15 minutes.
top - 13:29:09 up 2 days, 7:13, 4 users, load average: 0.07, 0.02, 0.00
In this line:
- 13:29:09 is the current time
- 2 days, 7:13 is the uptime
- 4 users shows how many users currently use your system
- 0.07 - average load for the last minute
- 0.02 - average load for the last 5 minutes
- 0.00 - average load for the last 15 minutes
Unix tasks stats
Here you can see how many tasks are currently running on your system. Tasks here mean processes, and the main listing will show you the task names (in the COMMAND column) and the PIDs.
Tasks: 148 total, 1 running, 147 sleeping, 0 stopped, 0 zombie
CPU(s) status
Current CPU state, averaged for the number of CPUs installed in your system, is represented in this line:
Cpu(s): 0.6%us, 0.5%sy, 0.0%ni, 97.3%id, 1.6%wa, 0.0%hi, 0.0%si, 0.0%st
Here are the explanations for each parameter:
- us - User CPU time. The time the CPU has spent running users’ processes with default priorities
- sy - System CPU time. The time the CPU has spent running the kernel and its processes
- ni - Nice CPU time. The time the CPU has spent running users’ proccess that have been prioritized up using nice command
- wa - I/O wait. Amount of time the CPU has been waiting for I/O operations to complete
- hi - Hardware IRQ. The amount of time the CPU has been servicing hardware interrupts
- si - Software Interrupts. The amount of time the CPU has been servicing software interrupts
- st - Steal Time. The amount of CPU ’stolen’ from this virtual machine by the hypervisor for other tasks (such as running another virtual machine) - a fairly recent addition to the top command, introduced with the increased virtualization focus in modern operating systems
Physical memory usage stats
Memory stats line gives you a summary of how much physical memory you have on your system, and how much of it is currently used or available for the use.
Modern Linux systems are buffering quite a lot for improved performance, which means you rarely get to see all your physical RAM free - the more your system stays up and running, the more of its recently used data ends up being buffered.
In this line, you can see how quite a bit is taken up by the buffers:
Mem: 4051792k total, 4026104k used, 25688k free, 359168k buffers
Swap usage stats
Swap statistics highlight how actively your system uses the swap space - most of it should not be used on a healthy system, although seeing substantial amount of swap memory cached is quite normal. Bear in mind that these are caches held in physical memory, so in this example these 2.8Gb of cached swap is responsible for most of the 4Gb physical RAM taken up and reported as used in the above stats for memory
Swap: 4096492k total, 24296k used, 4072196k free, 2806484k cached
List of the tasks (processes) running on your system
This is the main part of the top output, which looks like this (output is abridged):
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7629 greys 20 0 749m 291m 28m S 1 7.4 16:51.40 firefox
19935 greys 20 0 133m 14m 10m S 0 0.4 2:38.52 smplayer
1 root 20 0 4020 880 592 S 0 0.0 0:00.96 init
2 root 15 -5 0 0 0 S 0 0.0 0:00.00 kthreadd
3 root RT -5 0 0 0 S 0 0.0 0:00.04 migration/0
4 root 15 -5 0 0 0 S 0 0.0 0:00.90 ksoftirqd/0
5 root RT -5 0 0 0 S 0 0.0 0:00.00 watchdog/0
6 root RT -5 0 0 0 S 0 0.0 0:00.06 migration/1
As you can see from this list, you're given all the vital information about each process running on your Unix system:
- PID - process ID
- USER - username for the owner of each process
- PR - process priority (RT means a Real Time priority class - used for system processes)
- NI - priority set by nice utility
- VIRT - the amount of virtual memory used by a process: code, data and shared libraries plus pages that have been swapped out
- RES - the resident part of a process - how much of it resides in the physical memory (non-swapped memory)
- SHR - shows you the size of potentially shared memory segments for a process
- S - the current state of each process
- %CPU - percentage of the time shares CPU spends running a particular process
- %MEM - percentage of the physical memory of your system which is used by each process
- %TIME+ - total time CPUs spent running each process
- COMMAND - a command used to initiate each process.
I'll be sure to revisit and expand this page at some later stage.
See also:
- uptime - find out how long the system has been up
- uname - print Unix system information
- basic Unix commands
Just wanted to highlight this information for you, although it’s not exactly news - AJAX Libraries API from Google allows everyone to access the latest versions of most popular JavaScript frameworks right from Google servers.
What is AJAX Libraries API?
If you were looking for a way to simplify your usage of JavaScript frameworks, Google has got you covered: AJAX Libraries API is an architecture which hosts most popular open source JavaScript libraries on Google servers and thus make them highly available to use by any of your pages.
Why use AJAX libraries hosted by Google?
There’s a few advantages to offloading JavaScript libraries onto Google’s servers. Here’s my list:
- no headaches with upgrading your JavaScript libraries - they’re updated automatically so your pages are always using the latest and greatest versions available
- universally available - you can access JavaScript frameworks using the same methods from any pages of yours
- high availability - like everything else on Google servers, your scripts will be safe, fast and highly available - distributed among many Google servers and probably delivered to you from the geographically closest location
What libraries are currently available?
Here are the ones you can start using right now:
- jQuery - versions 1.2.3 and 1.2.6 are available
- Prorotype - version 1.6.0.2
- script.aculo.us - version 1.8.1
- mootools - version 1.11
- dojo - version 1.1.1
The full list of libraries available through AJAX Libraries API can be found here: Google AJAX Libraries, I presume it will be updated as more libraries get hosted there.
Via: Google Hosts Popular JavaScript Libraries @ Google System blog
date is a basic Unix command for getting or setting the current time and date on your system. Because it's the easiest way to get current time, this command is extensively used in Unix scripting.
Getting current time and date in your Unix system
The default usage of this command is simple and requires no additional command line parameters:
ubuntu$ date
Wed Jun 11 11:43:52 IST 2008
Using templates to specify the desired date/time format
date command supports template system for printing the current time and date - so you can use it to specify the exact format for representing the time and date information - for example, only print out the day of a month or a current year instead of the whole default timestamp shown above.
Format is text string which begins with the + character, which consists of a number of special parameters starting with the % sign: %B, %d, etc. If you're using spaces or any other elements in your format string, you need to use double quotes as well.
Here's an example of specifying a format for the date command:
ubuntu$ date "+%B %d, %Y"
June 11, 2008
In this example, the %B represents the full name of the current month, %d is the day of the month, and %Y is the four-digit representation of the current year.
Here are some of the most useful parameters used for date format specification:
%a locale’s abbreviated weekday name (e.g., Sun) %A locale’s full weekday name (e.g., Sunday) %b locale’s abbreviated month name (e.g., Jan) %B locale’s full month name (e.g., January) %c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005) %C century; like %Y, except omit last two digits (e.g., 21) %d day of month (e.g, 01) %D date; same as %m/%d/%y %e day of month, space padded; same as %_d %F full date; same as %Y-%m-%d %g last two digits of year of ISO week number (see %G) %G year of ISO week number (see %V); normally useful only with %V %h same as %b %H hour (00..23) %I hour (01..12) %j day of year (001..366) %k hour ( 0..23) %l hour ( 1..12) %m month (01..12) %M minute (00..59) %R 24-hour hour and minute; same as %H:%M %s seconds since 1970-01-01 00:00:00 UTC %S second (00..60) %T time; same as %H:%M:%S %u day of week (1..7); 1 is Monday %w day of week (0..6); 0 is Sunday %x locale’s date representation (e.g., 12/31/99) %X locale’s time representation (e.g., 23:13:48) %y last two digits of year (00..99) %Y year
Unix date manipulations
If you're interested in confirming any date/time information about dates in the past or in the future, you will find all the answers in my article here: Easy Date Calculations in Unix Scripts.
Unix epoch time
Since time in most Unix systems is calculated against the Unix epoch (January 1, 1970), you can use date command to confirm the exact number of seconds elapsed since this time on your Unix system:
ubuntu$ date +%s
1213182257
See also:
getent is Unix command which helps you query one of the following administrative databases in Unix: passwd, group, hosts, services, protocols, or networks.
Administrative databases in Unix
As you can probably see from their names, the administrative databases are here to help you gather the most vital information about your environment:
- passwd - can be used to confirm usernames, userids, home directories and full names of your users
- group - all the information about Unix groups known to your system
- services - all the Unix services configured on your system
- networks - networking information - what networks your system belongs to
- protocols - everything your system knows about network protocols
How to use getent
My home PC has a hostname of ubuntu. If I ever need to double-check which IPs this hostname points to, here's how I can use getent:
ubuntu$ getent hosts ubuntu 127.0.1.1 ubuntu 192.168.0.2 ubuntu
Using getent to find a UID by username
getent accepts various keys when searching in databases. For the passwd one, you can user either username or user id (UID) to search the database.
ubuntu$ getent passwd greys greys:x:1000:1000:Gleb Reys,,,:/home/greys:/bin/bas
Using getent to find a username by UID
Like I said, the opposite will work as well:
ubuntu$ getent passwd 1000 greys:x:1000:1000:Gleb Reys,,,:/home/greys:/bin/bash
See also:
Today I'd like to show you one more option you have when searching for files in Linux. If you have a locate tool installed, you'll be able to find any file almost instantly.
How does locate command work?
locate uses a pretty simple principle - instead of going through your filesystem directory tree every time you need a certain file found, it consults a database which stores locations of most files in your system. The locate database (locatedb) is updated nightly with a separate command. The update occurs during night hours when peak usage of your system is very unlikely, but this means that using such a database through the day will provide instant results.
There is obviously a chance that some files will be moved or even deleted by the time you look for them, then the locate database will still have entries for them and show them in results, but the actual files will be gone. There's a special command line option for the locate command to only return results for the existing files.
In short, the not-100%-accurate results are definitely worth the amazing speed increase you'll get when comparing the locate command against a more traditional find command approach to locating files in Unix.
Using locate to find files
Simply specify the partial name of a file you need and watch results appear on your screen in a fraction of a second!
In this example, I'm looking for everything that contains apache2 in its name (note how both files and directories are found):
ubuntu# locate apache2 | more /backup/apache2-log /etc/apache2 /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak /etc/apache2/apache2.conf.dpkg-dist /etc/apache2/conf.d /etc/apache2/conf.d/apache2-doc /etc/apache2/conf.d/charset /etc/apache2/envvars /etc/apache2/httpd.conf /etc/apache2/magic /etc/apache2/mods-available /etc/apache2/mods-available/actions.load /etc/apache2/mods-available/alias.load
Report only existing files with locate
If you're concerned that some of the files you're looking for could have been moved or deleted since last update of the locatedb database, use the -e command line option:
Report non-existing files with locate
Perhaps more useful way to approach the existing/non-existing files with locate is to use -E option which only shows the files which are referred in the locatedb database, but no longer exist.
For example, if I rename one of the files like this:
ubuntu# mv /etc/apache2/apache2.conf.bak /etc/apache2/apache2.conf.backup
And then run the locate -E, it will immediately highlight it:
ubuntu# locate -E apache2 | more
/etc/apache2/apache2.conf.bak
See also:
- find - finding files in Unix
file is one of the basic Unix commands which helps you confirm exactly what kind of files you're working with. Using a special database of signatures for various types of files, it reads the first few bytes of a specified file and shows you whether it matches one of the signatures, thus confirming the file type.
file command: basic usage
The simplest way to use the file command is to run it and specify one or more file names as command line parameters. The file type description you expect will be given in plan English, showing you whether a file is a script or an executable, an ASCII text or a binary data:
rhel$ file /etc/passwd rhel$ file /bin/csh rhel$ file /bin/tcsh /bin/tcsh: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.4.0, dynamically linked (uses shared libs), stripped
Examples of file types in Linux
Playing with the file command is fun. You can identify all sorts of files with this little command, but it's important to be able to read its output correctly.
Below I give you just a few most common file types examples, taken from a Red Hat Enterprise System (RHEL).
This is how a typical shared library will be identified:
rhel$ file /lib/libc-2.3.2.so /lib/libc-2.3.2.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), not stripped
Note the 32-bit which confirms the bit-ness of this file and the Intel 80386 which confirms the architecture.
That's the same library in 64bit:
rhel$ file /lib64/libc-2.3.2.so /lib64/libc-2.3.2.so: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), not stripped
See how 32-bit is now changed to 64-bit, and AMD x86-64 shows the new architecture for it?
Binaries will look somewhat different, although the bit-ness and architecture will still be shown:
rhel$ file /bin/ls /bin/ls: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.4.0, dynamically linked (uses shared libs), stripped
In this example, the important field is executable, which confirms this is a file you can run (vs previous example with shared objects which you can't run directly)
Examples of file types in SPARC Solaris
Just to show you that Unix file formats aren't this different, here's a few examples from SPARC Solaris 10 system.
First, a library - in 32bit and 64 bit forms:
sparc$ file /lib/libc.so /lib/libc.so: ELF 32-bit MSB dynamic lib SPARC32PLUS Version 1, V8+ Required, dynamically linked, not stripped, no debugging information available sparc$ file /lib/64/libc.so /lib/64/libc.so: ELF 64-bit MSB dynamic lib SPARCV9 Version 1, dynamically linked, not stripped, no debugging information available
Observe how bit-ness is still shown where you'd expect to see it, as well as SPARC-based architecture (SPARC32PLUS for 32bit and SPARCV9 for 64-bit).
Here's how a kernel driver will look:
sparc$ file /kernel/drv/sparcv9/zfs /kernel/drv/sparcv9/zfs: ELF 64-bit MSB relocatable SPARCV9 Version 1
And that's how a typical SPARC Solaris binary is identified:
sparc$ file /bin/ls /bin/ls: ELF 32-bit MSB executable SPARC Version 1, dynamically linked, stripped















