Tag Info

Hot answers tagged

36

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 ...


33

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 ...


26

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 ...


18

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 ...


13

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


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

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 ...


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

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 ...


9

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 ...


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 ...


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

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

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 ...


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 ...


7

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 ...


7

Unix is just a standard, it specifies what the implementations should do, but not how they should do it. Therefore implementations of grep/sort/find will most likely use different approaches on different systems (and even one system, like Linux, there are concurrent implementations). For Linux, you can always look into the source code.


7

The point of -f is to try and avoid the need to stat every file entry, and to avoid the need to read them all before any are displayed. It is a "meta" option that just disables other options. So, yes, it should do what you expect. I can't answer why it isn't, but I would guess that you might have a shell alias or something else that inserts additional ...


7

cp doesn't have this option. You could write a wrapper script, but it's pretty simple. ln -f $^ $@ 2>/dev/null || cp -f $^ $@ GNU Coreutils 7.5 introduced the --reflink option. If you pass --reflink=auto and the underlying filesystem supports copy-on-write (e.g. Btrfs or ZFS) and the copy happens to be on the same device, then cp will create a new ...


7

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 ...


7

POSIXly: $ awk 'BEGIN{ n = split(ENVIRON["PATH"], p, ":") while (n) i[p[n]]=n-- if (! (ARGV[1] in i)) print ARGV[1], "is not in $PATH" else if (! (ARGV[2] in i)) print ARGV[2], "is not in $PATH" else if (i[ARGV[1]] < i[ARGV[2]]) print ARGV[1], "is before", ARGV[2] else print ARGV[1], "is after", ARGV[2] exit}' /bin /usr/bin ...


7

readdir() basically. When tar finds out what files are in a directory, it directly asks the kernel for a file listing via opendir() followed by readdir(). readdir() does not return the files in any particular order; the way the files are ordered depends on the file system being used by the Linux kernel. There, alas, isn't an option for tar to sort files ...


7

This is more of an extra analysis than an actual answer but it does seem to vary depending on the data being sorted. First, a base reading: $ printf "%s\n" {1..1000000} > numbers.txt $ time python sort.py <numbers.txt >s1.txt real 0m0.521s user 0m0.216s sys 0m0.100s $ time sort <numbers.txt >s2.txt real 0m3.708s user ...


7

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 ...


6

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 ...


6

Ksh, Zsh and Bash all offer another, perhaps clearer syntax: var4=$(echo ztemp.xml | cut -f1 -d '.') The backticks (a.k.a. "grave accent") is unreadable in some fonts. The $(blahblah) syntax is a lot more obvious at least. Note that you can pipe values into a read command in some shells: ls -1 \*.\* | cut -f1 -d'.' | while read VAR4; do echo $VAR4; done ...


6

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 ...


6

By default, du will only count each file once if it is linked to multiple times. If you run du -L bar it will count the file because it only reaches it once. However, if you run du -L * it will only count it the first time it sees it. For example: $ du -L foo bar 16K foo 4.0K bar $ du -L bar foo 16K bar 4.0K foo Notice that swapping the ...



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