1

In a tutorial about public key infrastructure the author sets up a database to work with when setting up a Root Certificate Authority:

cp /dev/null ca/root-ca/db/root-ca.db
cp /dev/null ca/root-ca/db/root-ca.db.attr
echo 01 > ca/root-ca/db/root-ca.crt.srl
echo 01 > ca/root-ca/db/root-ca.crl.srl

I know /dev/null is a special file that has nothing in it and prints no where if you echo into it.

It seems like that's what the author is trying to do so I made a small example to test it:

$ ls
$ touch foo
$ cp /dev/null bar
$ cat /dev/null > baz
$ ls
bar baz foo
$ ls -l
total 0
-rw-r--r--  1 mbigras  1264914557  0 Apr 14 14:35 bar
-rw-r--r--  1 mbigras  1264914557  0 Apr 14 14:35 baz
-rw-r--r--  1 mbigras  1264914557  0 Apr 14 14:35 foo
  • Given we have an empty directory is there any difference between the files foo, bar, or baz?
  • Is the point of cping from /dev/null just to set up a file we know is empty?
1

All result in the same empty file.

Could even just use >baz2. A little more elegant in my opinion, as it isn't reliant on /dev/null being present, and doesn't involve invoking additional commands/processes.

Bear in mind that unlike touch, the result from >baz2 will be an empty file even if baz2 already exists and has some content.

$ touch foo
$ cp /dev/null bar
$ cat /dev/null >baz
$ >baz2
$ ls -l
total 0
-rw-rw-r-- 1 ec2-user ec2-user 0 Apr 14 21:40 bar
-rw-rw-r-- 1 ec2-user ec2-user 0 Apr 14 21:40 baz
-rw-rw-r-- 1 ec2-user ec2-user 0 Apr 14 21:40 baz2
-rw-rw-r-- 1 ec2-user ec2-user 0 Apr 14 21:39 foo
$
1

The difference is in what happens if the file already exists and has content:

For example, here's a file with content:

$ ls -l ca/root-ca/db/root-ca.db
-rw-r--r-- 1 sweh sweh 6 Apr 14 18:06 ca/root-ca/db/root-ca.db
$ touch ca/root-ca/db/root-ca.db
$ ls -l ca/root-ca/db/root-ca.db       
-rw-r--r-- 1 sweh sweh 6 Apr 14 18:06 ca/root-ca/db/root-ca.db
$ cp /dev/null ca/root-ca/db/root-ca.db
$ ls -l ca/root-ca/db/root-ca.db       
-rw-r--r-- 1 sweh sweh 0 Apr 14 18:06 ca/root-ca/db/root-ca.db

We can see that the touch command didn't empty the file, but the cp did.

Now, typically, the : command can be used instead:

: > ca/root-ca/db/root-ca.db

eg

$ ls -l ca/root-ca/db/root-ca.db
-rw-r--r-- 1 sweh sweh 6 Apr 14 18:08 ca/root-ca/db/root-ca.db
$ : > ca/root-ca/db/root-ca.db  
$ ls -l ca/root-ca/db/root-ca.db
-rw-r--r-- 1 sweh sweh 0 Apr 14 18:08 ca/root-ca/db/root-ca.db

However, in training notes and course work this may be harder to read, thought to be a typo, or similar. Sometimes using a longer command string is better :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.