Hot answers tagged

56

nutshell: yes exhibits similar behavior to most other standard utilities which typically write to a FILE STREAM with output buffered by the libC via stdio. These only do the syscall write() every some 4kb (16kb or 64kb) or whatever the output block BUFSIZ is . echo is a write() per GNU. That's a lot of mode-switching (which is not, apparently, as costly as ...


45

For GNU utilities, the full documentation is in the info page, where you can read: -f Ignored; for compatibility with BSD versions of `touch'. See historic BSD man pages for touch, where -f was to force the touch. If you look at the source of those old BSDs, there was no utimes() system call, so touch would open the file in read+write mode, read ...


43

It's not a bug. The use case is for when you want to link a file to the same basename but in a different directory: cd /tmp ln -s /etc/passwd ls -l passwd lrwxrwxrwx 1 xxx xxx 11 Jul 29 09:10 passwd -> /etc/passwd It's true that when you do this with a filename that is in the same directory it creates a link to itself which does not do a whole lot of ...


36

The [ binary residing under the /bin tree in many GNU/Linux distributions is not something to be alarmed off. At least in my Fedora 19 it is a part of the coreutils package, as demonstrated below: $ rpm -qf /bin/[ coreutils-8.21-13.fc19.x86_64 and is a synonym for test to allow for expressions like [ expression ] to be written in shell scripts or even ...


30

Here's another way to do locking in shell script that can prevent the race condition you describe above, where two jobs may both pass line 3. The noclobber option will work in ksh and bash. Don't use set noclobber because you shouldn't be scripting in csh/tcsh. ;) lockfile=/var/tmp/mylock if ( set -o noclobber; echo "$$" > "$lockfile") 2> ...


26

It's usually plain C. The commands ls and pwd come from the GNU Coreutils package in (most?) Linux distributions (and maybe some other systems). You can find the code on their homepage. For coreutils specifically, you build them with the usual steps: after unpacking the source, issue: ./configure --prefix=/some/path # type ./configure ...


19

You'll want to modify your assignment to read: var4="$(echo ztemp.xml | cut -f1 -d '.')" The $(…) construct is known as command susbtitution.


19

Izkata's comment revealed the answer: locale-specific comparisons. The sort command uses the locale indicated by the environment, whereas Python defaults to a byte order comparison. Comparing UTF-8 strings is harder than comparing byte strings. $ time (LC_ALL=C sort <numbers.txt >s2.txt) real 0m5.485s user 0m14.028s sys 0m0.404s How about ...


16

Don't know why it's not the default, maybe so that it behaves the same as other copying utilities (rsync, cpio, pax, tar...) which have no support for it (or when files are copied across an interface that doesn't allow that (like NFS, samba, fuse file systems layers...). I was in the same situation a few years ago, and looking at GNU cp code quickly, it's ...


15

It's not the default since for robustness reasons one may want a copy to take place to protect against data corruption. Also for performance reasons you may want the writes to happen at copy time rather than some latency sensitive process working on a CoW file and being delayed by the writes possibly to a different part of a mechanical disk. Note that from ...


15

A better question would be why is your shell writing the file so slowly. Any self-contained compiled program that uses file writing syscalls responsibly (not flushing every character at a time) would do it reasonably quicky. What you are doing, is writing lines in an interpreted language (the shell), and in addition you do a lot of unnecessary input output ...


14

-f does nothing. It's kept for historic compatibility (with BSD touch according to info touch), so that applications that expect it to exist don't pass it and get back an error message saying it doesn't exist. Assuming that this is GNU coreutils, you can see in the source that this just does a break out of the option processing switch without doing anything. ...


13

Sorting depends on the locale; specifically, it depends on $LC_COLLATE (possibly overridden by $LC_ALL), falling back to $LANG if it doesn't exist. The command locale will show you what values you're effectively working with. See man 3 strcoll, man 3 setlocale, etc. LC_COLLATE=C (or POSIX or no locale at all) results in a strict byte-by-byte comparison. ...


13

Wikipedia, "Factor (Unix)" with an interesting take: factor first appeared on 5th edition Research Unix in 1974, as a "user maintained" utility (section 6 of the manual). In the 7th edition in 1979, it was moved into the main "commands" section of the manual (section 1). From there, the factor utility was copied to all other variants of Unix, including ...


12

Try this in-order to remove the file: rm -- --append


12

Whenever you see "option X is ignored" in --help output or a manpage, that means: the program accepts option X — you do not get a syntax error — but it doesn't have any effect. The program does the same thing it would have done in the absence of the option. As the other answers demonstrate, this is done for backward compatibility. An option used to have ...


11

I prefer to use hard links. lockfile=/var/lock/mylock tmpfile=${lockfile}.$$ echo $$ > $tmpfile if ln $tmpfile $lockfile 2>&-; then echo locked else echo locked by $(<$lockfile) rm $tmpfile exit fi trap "rm ${tmpfile} ${lockfile}" 0 1 2 3 15 # do what you need to Hard links are atomic over NFS and for the most part, mkdir is ...


11

GNU coreutils do understand UTF-8 in general. For example echo 哈哈 | wc -m correctly outputs 3 in a UTF-8 locale (note that the option is -m, not -c which for historical reasons means bytes). This is a bug in cut. Looking at the source of cut, cut on characters is simply not implemented: the -c option is treated as a synonym of -b. A workaround is to use ...


11

I think it's taking your + x as a time-zone specifier (e.g., consider 2013-04-25 19:52:36 +4 is a valid timestamp, in in a time zone 4 hours ahead of UTC). It's then seeing the word 'minutes', and treating it as a synonym of minute, so giving you one minute later. If you put in an explicit timezone specifier, it works: anthony@Zia:~$ TZ=utc date -d ...


9

stat: print timestamps to full resolution was committed to coreutils-8.6. $ git clone git://git.savannah.gnu.org/coreutils.git $ cd coreutils $ git log -1 --grep=time -- src/stat.c commit 9069af45e691d1252c727da66aa4f3f3c7f1ea29 Author: Eric Blake Date: Thu Sep 30 16:42:13 2010 -0600 stat: print timestamps to full resolution * src/stat.c ...


9

tac is easier to understand in the case it's primarily designed for, which is when the separator is a record terminator, i.e. the separator appears after the last record. It prints the records (including each terminator) in reverse order. $ echo -n fooabara | tac -s a; echo rabafooa The input consists of three records (foo, b and r), each followed by the ...


9

You can use the stat command and get roughly what you want: $ stat -c '%A %a %h %U %G %s %y %n' * drwxrwxr-x 775 2 saml saml 4096 2013-05-16 22:02:13.230463837 -0400 alsa drwxrwxr-x 775 31 saml saml 4096 2013-03-26 12:09:20.707827127 -0400 apps -rw-rw-r-- 664 1 saml saml 43627 2013-05-18 12:28:32.157583577 -0400 autosave.h2song -rw-rw-r-- 664 1 saml saml ...


8

I understand that mkdir is atomic, so perhaps: lockdir=/var/tmp/myapp if mkdir $lockdir; then # this is a new instance, store the pid echo $$ > $lockdir/PID else echo Job is already running, pid $(<$lockdir/PID) >&2 exit 6 fi # then set traps to cleanup upon script termination # ref http://www.shelldorado.com/goodcoding/tempfiles.html ...


8

Depending on the shell you're using, you can use Parameter Expansion. For instance in bash: ${parameter%word} ${parameter%%word} Remove matching suffix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the ...


8

busybox the favorite of Embedded Linux systems. BusyBox combines tiny versions of many common UNIX utilities into a single small executable. It provides replacements for most of the utilities you usually find in GNU fileutils, shellutils, etc. The utilities in BusyBox generally have fewer options than their full-featured GNU cousins; however, the options ...


8

As @samiam has stated the list is returned to you in a semi-random order via readdir(). I'll just add the following. The list returned is what I would call the directory order. On older filesystems, the order is often the creation order that the file entries in the directory's table were added. There is of course a caveat to this, when a directory entry is ...


8

The easiest way to find out of course, is to try it and see. When no 2nd argument is given, ln will create a link in the current directory with the same name as the original: $ ln -s /etc $ ls -l lrwxrwxrwx 1 terdon terdon 4 Jul 29 16:09 etc -> /etc This is also explained in man ln: In the 2nd form, create a link to TARGET in the current ...


8

You can put the grep in an if condition, or if you don't care about the exit status, add || true. Example: grep kills the shell $ bash $ set -e $ echo $$ 33913 $ grep foo /etc/motd $ echo $$ 9233 solution 1: throw away the non-zero exit status $ bash $ set -e $ echo $$ 34074 $ grep foo /etc/motd || true $ echo $$ 34074 solution 2: explicitly test the ...


8

mv and rsync are not similar programs. In particular, mv is often attempting to simply rename objects. If it's in the same filesystem, it does not copy the contents at all. If you didn't already have imperative_PLs/fortran, then mv would take the existing fortran directory and rename it to that point in the tree. But you already have a directory (with ...


7

Generally, there's a changelog. In fact, this (or other "prominent notices" of changes) is required by the GPL! (At least, effectively so for anything with multiple contributors — see GPLv2 section 2a.) For the GNU coreutils package — and for pretty much everything else from the GNU project directly — this file is definitely the first place to look, and ...



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