# Tag Info

42

When you run ls without arguments, it will just open a directory, read all the contents, sort them and print them out. When you run ls *, first the shell expands *, which is effectively the same as what the simple ls did, builds an argument vector with all the files in the current directory and calls ls. ls then has to process that argument vector and for ...

24

bahamat and Alan Curry have it right: this is due to the way your shell buffers the output of echo. Specifically, your shell is bash, and it issues one write system call per line. Hence the first snippet makes 1000000 writes to a disk file, whereas the second snippet makes 1000000 writes to a pipe and sed (largely in parallel, if you have multiple CPUs) ...

20

look at the time man page on your system, some implementations have format options to include I/O, CPU and Memory stats (-f). For instance, GNU time, with -v will show all available info (here on Linux): /usr/bin/time -v ls Command being timed: "ls" User time (seconds): 0.00 System time (seconds): 0.00 Percent of CPU this job got: 0% Elapsed (wall ...

17

Use timeout: NAME timeout - run a command with a time limit SYNOPSIS timeout [OPTION] DURATION COMMAND [ARG]... timeout [OPTION] (Just in case, if you don't have this command or if you need to be compatible with very very old shells and have several other utterly specific requirements… have a look at this this question ;-))

13

There's a library called libfaketime which allows you to make the system report a given time to your application. You can either have the system report a fixed time for the duration of the program execution, or start the clock at some specific time (for example, 01:59:30). Basically, you hook the faketime library into your program's in-memory image through ...

13

There are DST-free timezone definitions provided which just define the GMT-offset, called Etc/GMT±X: $date Mon Apr 7 11:08:56 CEST 2014$ TZ=Etc/GMT-1 date Mon Apr 7 10:09:16 GMT-1 2014 $Just link/copy the one you need to /etc/localtime and you should be fine and DST-free:$ ln -s /usr/share/zoneinfo/Etc/GMT-1 /etc/localtime Edit: For non-integer ...

12

It depends on your definition of fast. The answers already here give a good solution for actually removing the directories from the filesystem, but if what you really need is to free the directory name as fast as possible, a rename on the same filesystem is instantaneous: { mv directory directory.gone && rm -rf directory.gone; } & Technically ...

12

You can use the command shell built-in to bypass the normal lookup process and run the given command as an external command regardless of any other possibilities (shell built-ins, aliases, etc.). This is often done in scripts which need to be portable across systems, although probably more commonly using the shorthand \ (as in \rm rather than command rm or ...

11

To get the output of time into a var use the following: usr@srv $mytime="$(time ( ls ) 2>&1 1>/dev/null )" usr@srv $echo "$mytime" real 0m0.006s user 0m0.001s sys 0m0.005s You can also just ask for a single time type, e.g. utime: usr@srv $utime="$( TIMEFORMAT='%lU';time ( ls ) 2>&1 1>/dev/null )" usr@srv $echo "$utime" ...

11

The output you show is a bit odd, since real time would usually be bigger than the other two. Real time is wall clock time. (what we could measure with a stopwatch) User time is the amount of time spend in user-mode within the process Sys is the CPU time spend in the kernel within the process. So I suppose if the work was done by several processors ...

11

If I understand what you're asking for I this will do. I'm using the commands ls ~ and tee as stand-ins for ./foo and bar, but the general form of what you want is this: $( time ./foo ) |& bar NOTE: The output of time is already being attached at the end of any output from ./foo, it's just being done so on STDERR. To redirect it through the pipe you ... 10 Using a script to monitor ntpd is not commonly done. Usually a monitoring tool like nagios or munin is used to monitor the daemon. The tool can send you an alert when things go wrong. I have munin emailing me if the offset exceeds 15 milliseconds. Normally, you should use an odd number of servers so that the daemon can perform an election among the ... 10 The date command will give you the current date/time based on your locale. You can change that, one time only, by prefixing the command with a different timezone TZ=CST6CDT date # Will print the current time in the USA Central time TZ=Chicago date # will do the same, iff Chicago is listed by name in the /usr/share/zoneinfo/ dir hierarchy Then to simplify ... 10 By using strace, I saw that the line number=$(expr $number + 1) causes a fork, path search, and exec of expr. (I'm using bash 4.2.45 on Ubuntu). That filesystem, disk, and process overhead led to bash only getting around 28% of the CPU. When I changed that line to use only shell builtin operations ((number = number + 1)) bash used around 98% of the ... 9 I wouldn't have known the answer except google was there for me: From Here (needs free subscription): Linux is following the tradition set by Unix of counting time in seconds since its official "birthday," -- called "epoch" in computing terms -- which is Jan. 1, 1970. A more complete explanation can be found in this Wired News article. It ... 9 .INIT. 16 u That's not fine. You either queried the state to soon, or your ntpd can't connect to those servers at all. When it is synced, it should display IP addresses or host names in the refid column, and values like 2 or 3 in the st column. The output of a working ntpd should look like this: iserv ~ # ntpq -p remote ... 8 Try just time instead of timethis. Although be aware that there's often a shell builtin version of time and a binary version, which will give different results:$ time wget -q -O /dev/null http://unix.stackexchange.com/ real 0m0.178s user 0m0.003s sys 0m0.005s vs $\time wget -q -O /dev/null http://unix.stackexchange.com/ 0.00user 0.00system ... 8 The "seconds since 1970" timestamp is specifically defined as UTC in most usages. In particular, you may notice that date +%s gives the same result as date -u +%s. The relevant line where this is set in the shadow password utilities is" nsp->sp_lstchg = (long) time ((time_t *) 0) / SCALE; Which would make it UTC. SCALE is defined as 86400 (except via ... 8 There are a number of factors that might make a software clock run slow or fast. Clocks on virtual servers are especially prone to a whole class of these problems. 12 seconds a day is pretty bad until you come across virtual boxes with clocks that run at 180–200% speed! Clocks on laptops that suspend can suffer from time-keeping issues too. You should ... 8 "Real" time is elapsed time, which is usually the difference between wall clock times, but not always. For example, if you start a process at 01:59:00 on the day in which daylight-savings (summer) time takes effect in a locale in which the time change is at 02:00, and the process takes two minutes, then the real elapsed time will be two minutes, while the ... 7 You can use date util: #!/bin/bash start_measuring_time() { read s1 s2 < <(date +'%s %N') } stop_measuring_time() { read e1 e2 < <(date +'%s %N') } show_elapsed_time() { echo "$((e1-s1)) seconds, $((e2-s2)) nanoseconds" } start_measuring_time sleep 2 stop_measuring_time show_elapsed_time 7 To answer the first question, ntpdate usually tells you what it did, or maybe did not do. [root@flask rc.d]# ntpdate dagoo 12 Aug 10:04:03 ntpdate[20585]: adjust time server 10.0.0.15 offset -0.042285 sec The NTP daemon, ntpd, runs constantly, and asks NTP servers (usually configured in /etc/ntp.conf) for the time every so often. You shouldn't have to ... 7 The kernel keeps track of CPU usage statistics for all processes; time just asks the kernel for the information about its child process, which happens to be the command you asked it to run. Since the kernel is keeping track of this information anyway (needed for scheduling, implementation of various resource quotas, etc.), using the time command doesn't ... 7 If your version of "find" implements the -delete sub-command, then you can try find directory -delete In this case: find ~/.local/share/Trash/ -delete Some commands, like rm, perform most of their work in the kernel. In the file-system routines, to be exact. Time spent performing system calls are accounted for in that way, so whilst your "rm" command ... 7 time sudo command executes your shell's time builtin if it has one, whereas sudo time command always executes the time executable in the program search path ($PATH). time sudo command includes the time taken by the sudo command, whereas sudo time command doesn't. You should use sudo time command, because sudo's processing time is small but not always ...

7

In many shells including ksh, zsh and bash, time is a keyword and is used to time pipelines. time foo | bar Will time both the foo and bar commands (zsh will show you the breakdown). It reports it on the shell's stderr. time foo.sh > bar.txt Will tell you the time needed to open bar.txt and run foo.sh. If you want to redirect time's output, you ...

7

There is a shortcut in bash to sidestep keywords, without having to specify a path or use another builtin like command: Escape it with a backslash. =^_^= izkata@Izein:~$time real 0m0.000s user 0m0.000s sys 0m0.000s =^_^= izkata@Izein:~$ \time Usage: time [-apvV] [-f format] [-o file] [--append] [--verbose] [--portability] ...

7

No. Process/context switches aren't free. How much other processes running will slow yours down is very system-dependent, but it consists of things like: Every time a processor switches to a different address space (including process), then the MMU cache must be flushed. And probably the processor L1 caches. And maybe the L2 and L3 caches. This will slow ...

6

You did not specify which operating system you use. Linux Instead of using time foo which is (usually) a shell built-in you could try the external command /usr/bin/time foo. It gives some additional information such as number of file system inputs and outputs (but no information about cache hits or byte amounts). See man time and man getrusage for further ...

6

You can record the time a command line is started and the time a prompt is displayed. Bash already keeps track of the starting date of each command line in its history, and you can note the time when you display the next prompt. print_command_wall_clock_time () { echo Wall clock time: \ $(($(date +%s) - \$(HISTTIMEFORMAT="%s "; ...

Only top voted, non community-wiki answers of a minimum length are eligible