April 7th, 2008 — Site updates
Hi! If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
rmdir is one of the basic unix commands, which serves a sole purpose of removing empty directories. You may need this kind of functionality to attempt removing directories, and be sure that any files still existing in them will be safe. For removing any directory irregardless of any files in it, you should use the rm command.
To demonstrate how rmdir works, let's create two directories and two files in them:
ubuntu$ mkdir -p /tmp/dir1/dir2
ubuntu$ touch /tmp/dir1/file1
ubuntu$ touch /tmp/dir1/dir2/file2
This gives us the following file and directory structure:
ubuntu$ find /tmp/dir1
/tmp/dir1
/tmp/dir1/dir2
/tmp/dir1/dir2/file2
/tmp/dir1/file1
So, we have /tmp/dir1 directory, which contains a file1 file and a dir2 directory. /tmp/dir1/dir2 contains another file, called file2.
Basic rmdir usage
The simplest way to remove an empty directory is to run rmdir and supply the desired directory name as a command line parameter:
ubuntu$ rmdir /tmp/dir1/dir2
rmdir: `/tmp/dir1/dir2': Directory not empty
In our example, we got the error because dir2 contains a file2 file, so it cannot be removed until the file exists.
Now, if we remove the file2 in dir2 directory, rmdir will happily destroy dir2 if we try again:
ubuntu$ rm /tmp/dir1/dir2/file1
ubuntu$ rmdir /tmp/dir1/dir2
Cascade directory removal with mkdir
If you want, you can attempt to do a cascade directory removal - if removing a specified directory succeeds, rmdir will try to remove its parent directory, if it's empty, and move up the directory tree until it meets a directory which isn't empty or which your use doesn't have permission to remove.
This kind of directory removal is achieved using rmdir -p option. If we recreate the dir2 directory under dir1 from our initial setup, you can see how rmdir will remove dir2 and then attempt to remove dir1, its parent directory:
ubuntu$ mkdir /tmp/dir1/dir2
ubuntu$ rmdir -p /tmp/dir1/dir2
rmdir: `/tmp/dir1': Directory not empty
Now, the /tmp/dir1 removal is failed because it has file1 file left it it. So if we remove it and recreate the empty dir2, rmdir -p will successfully remove both directories.
First, let's prepare our 2 empty directories:
ubuntu$ rm /tmp/dir1/file1
ubuntu$ mkdir /tmp/dir1/dir2
This is how we double-check it's only dir1 with dir2 subdirectory:
ubuntu$ find /tmp/dir1
/tmp/dir1
/tmp/dir1/dir2
And now let's see how rmdir -p manages to remove both and even attempt to remove /tmp, cause it's the parent of /tmp/dir1:
ubuntu$ rmdir -p /tmp/dir1/dir2
rmdir: `/tmp': Permission denied
As usual, unix find command can testify that there's no /tmp/dir1 directory anymore:
ubuntu$ find /tmp/dir1
find: /tmp/dir1: No such file or directory
See also:
April 2nd, 2008 — Unix filesystems
tune2fs command is one of the advanced unix commands which allows you to adjust various tunable parameters of the ext2/ext3 filesystems. Naturally, it also helps you confirm the existing parameters configured for your filesystems.
Confirm current filesystem parameters with tune2fs
The tunefs -l command will show you all the information contained in a filesystem's superblock. Here's how it typically looks:
ubuntu# tune2fs -l /dev/sda1
tune2fs 1.40-WIP (14-Nov-2006)
Filesystem volume name: <none>
Last mounted on: <not available>
Filesystem UUID: d2ff8a06-74b7-4877-9d37-1873414e25b3
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal filetype needs_recovery sparse_super
Default mount options: (none)
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 2490368
Block count: 4980736
Reserved block count: 249036
Free blocks: 3417990
Free inodes: 2401957
First block: 0
Block size: 4096
Fragment size: 4096
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 16384
Inode blocks per group: 512
Filesystem created: Wed Sep 26 02:30:22 2007
Last mount time: Tue Apr 1 00:17:16 2008
Last write time: Tue Apr 1 00:17:16 2008
Mount count: 1
Maximum mount count: 29
Last checked: Tue Apr 1 00:16:22 2008
Check interval: 15552000 (6 months)
Next check after: Sun Sep 28 00:16:22 2008
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 128
Journal inode: 8
Default directory hash: tea
Directory Hash Seed: c0c5742c-980a-49b2-ae0b-4e96895376b6
Journal backup: inode blocks
Reserved space on a Unix filesystem
By default, every filesystem in Unix has some space reserved for the superuser (root). This means that no regular Unix user can fill your filesystem up to 100%, and so it's always going to have enough free space to continue normal function.
As a standard, each filesystem has 5% of space reserved in this way. If you look at the above output, you may notice the following lines there, which regulate the space reservation:
Reserved block count: 249036
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
Compared to the overall filesystem block count:
Block count: 4980736
this 249036 reserve is exactly 5%. The uid and gid confirm the Unix user id and Unix group id of the user who will be allowed to tap into the reserved space. As I said earlier, it's root.
If you have root access on your system, you can alter this reserved space allocation for any filesystem using tune2fs -m parameter, by specifying the percentage of the space to be reserved.
Here's how we change the default reserve to be 6% of the overall filesystem size:
ubuntu# tune2fs -m 6 /dev/sda1
tune2fs 1.40-WIP (14-Nov-2006)
Setting reserved blocks percentage to 6% (298844 blocks)
And here we change it back. Note how the number of block corresponding to 5% is exactly the figure we've seen earlier - 249036 blocks:
ubuntu# tune2fs -m 5 /dev/sda1
tune2fs 1.40-WIP (14-Nov-2006)
Setting reserved blocks percentage to 5% (249036 blocks)
Default block size for a filesystem
If you ever want to confirm the block size of any filesystem, tune2fs will help you do just that:
ubuntu# tune2fs -l /dev/sda1 | grep Block
Block count: 4980736
Block size: 4096
Blocks per group: 32768
From this example, you can see that the default block size for the filesystem on /dev/sda1 partition is 4096 bytes, or 4k. That's the default block size for ext3 filesystem.
March 31st, 2008 — Unix directory operations, Unix file operations
ln is a Unix command for linking files or directories to each other. Essentially, it creates new files with the names you specify, and refer them to already existing files or directories. When you run any Unix command against a symlink, it is first resolved (the original file it points to is confirmed) and the Unix command works with that file to produce desired outcome.
Two types of linking files and directories
There are two common approaches to link a file or directory in Unix: soft linking and hard linking. Soft links are also called symlinks (symbolic links).
What is a soft link?
Soft link (also referred to as symlink - short for symbolic link) is a special type of file in Unix, which references another file or directory. Symlink contains the name for another file and contains no actual data. To most commands, symlinks look like a regular file, but all the operations (like reading from a file) are referred to the file the symlink points to.
When you remove a soft link, you simply remove one of the pointers to the real file. When you remove the original file a soft link points to, your data is lost. Even though your soft link will still exist, it will be pointing to the non-existent file and will therefore be useless (it will probably have to be removed as well).
What is a hard link?
Hard link is a pointer to physical data. Effectively, all standard files are hard links, because they ultimately create an association between a file name and a physical data which corresponds to each file.
In Unix, you can create as many hard links to a file as you like, and there is even a special counter for such references. When you're using the long format of an ls command, you can see this counter.
When you remove a hard link, you decrease this link counter for a data on your storage. If you remove the original file, the data will not be lost as long as there's at least one hard link pointing to it.
Creating soft links (symlinks) with ln
Let's start with a really simple example. We create a text file, and then use soft link to reference it.
This shows how the file is created. It's called file1, and has a line of text data in it which we confirm using cat command:
ubuntu$ echo "Text file #1" > file1
ubuntu$ cat file1
Text file #1
Now let's use ln command to create a soft link. The newly created symlink will be a file called file2, and ls command will show you that it points to file1:
ubuntu$ ls -l file2
lrwxrwxrwx 1 root root 5 Mar 31 03:54 file2 -> file1
If you try accessing the file2, you will ultimately access file1, that's why the following example shows you the contents of file1:
ubuntu$ cat file2
Text file #1
Now, if you remove file1, this will make file2 symlink invalid, and any attempts to use it will return a "file not found" type of error:
ubuntu$ rm file1
ubuntu$ cat file2
cat: file2: No such file or directory
Creating hard links with ln
Now, let's look at creation of hard links in Unix. For this example, we'll recreate the file1:
ubuntu$ echo "Text file #1" > file1
ubuntu$ cat file1
Text file #1
If you use ls to look at file1, you can see that the link counter (second field from the left) is set to 1 - which means that there is only one file name pointing to the data with our "Text file #1″ text:
ubuntu$ ls -l file1
-rw-r--r-- 1 root root 13 Mar 31 06:18 file1
And now we use ln command to create a hard link called file3, which points to the same data as file1:
ubuntu$ ln file1 file3
If we use ls command once again, you can see that the link counter has been increased and is now 2:
ubuntu$ ls -l file1 file3
-rw-r--r-- 2 root root 13 Mar 31 06:18 file1
-rw-r--r-- 2 root root 13 Mar 31 06:18 file3
Notice, how the file1 and file3 files look like absolutely normal files, and there's nothing showing a logical link between them.
To confirm that both filenames are actually referring to the same area on the disk, you can use -i option for the ls command, which will show you an i-node value.
i-nodes are data structures of a filesystem used to store all the important properties of each file: size, owner's user id and group id, access permission and more. The important thing is that each named data area on your disk must have an inode, and when you create a new data file this means creating an i-node. But when you're using hard links, you're effectively creating filesystem directory entry, which references an already existing data, so the hard link gets the same i-node number pointing to the same data.
ubuntu$ ls -il file1 file3
655566 -rw-r–r– 2 root root 13 Mar 31 06:18 file1
655566 -rw-r–r– 2 root root 13 Mar 31 06:18 file3
The first number in each line of the output is the i-node number, and since we're referencing the same data, the i-node numbers are also the same.
See also:
March 25th, 2008 — Unix directory operations
Each time you're going to perform some file or directory operation in Unix, it helps to be aware of where exactly in the filesystem tree you are.
To confirm your current directory, you can use a pwd command.
Continue reading →
March 25th, 2008 — Unix directory operations
When navigating the filesystem tree, changing your current directory is one of the basic actions.
In Unix, changing your current directory is accomlpished by the cd command.
Continue reading →
March 20th, 2008 — Unix users
id command is one of the basic unix commands, and it servers a very simple purpose of confirming the identity of a specified Unix user.
Simplest way to use the id command
All you do is just type id in your command line prompt, and it then gets back to you with confirmations of your own user id, group id, and a list of other groups you're a member of:
ubuntu$ id
uid=1000(greys) gid=113(admin) groups=33(www-data),113(admin)
March 18th, 2008 — Site updates
LSB - Linux Standard Base - is a joint project by a number of Linux vendors to standardize the OS environment. Apart from sharing many common principles in their structure, LSB participating Linux distributions share quite a few commands. lsb_release is one of them, and it allows you to find out all the LSB information about your Linux distribution.
I'm going to use a Ubuntu Feisty (7.04) in my examples.
March 11th, 2008 — Unix directory operations
du command is one of the most popular Unix commands. It is used to quickly estimate and report disk usage by a certain directory tree, all figures are reported in n blocks of data consumed by each object. While most commonly used block size is 1024b these days, you can easily override this if you have to. It's flexible enough to report sizes of only specified directories, full directory tree or even size of each file. There is no faster or easier way to find out what's consuming most space on a certain filesystem - you usually run du for a high-level overview, and, once the largest directories are identified, use find command to highlight the largest files within them.
Basic du command usage
The simplest form of using du is when you run it with no additional parameters. In this mode, du will scan your current directory and all the subdirectories of it to calculate usage stats. By default, you get the usage reported by all the directories found under the specified (or default) location.
Important: be careful with default du behavior, because depending on your Unix OS, it may use different block size for reporting the size. Older systems would use 512-byte blocks, while most of the recent releases use 1024-byte ones. To avoid any confusion, get used to specifying the desired block size with -k parameter (see below).
Continue reading →
February 12th, 2008 — Unix file operations
cat is a simple yet very useful Unix command. It takes a name of one or more text files, and then shows their contents to the standard output as one stream of data.
cat command example
greys@ubuntu:~$ cat /etc/kernel-img.conf
do_symlinks = yes
relative_links = yes
do_bootloader = no
do_bootfloppy = no
do_initrd = yes
link_in_boot = no
postinst_hook = /sbin/update-grub
postrm_hook = /sbin/update-grub
for two files, it looks like this:
greys@ubuntu:~$ cat /etc/issue
Ubuntu 7.04 \n \l
\
greys@ubuntu:~$ cat /etc/issue /etc/kernel-img.conf
Ubuntu 7.04 \n \l
\
do_symlinks = yes
relative_links = yes
do_bootloader = no
do_bootfloppy = no
do_initrd = yes
link_in_boot = no
postinst_hook = /sbin/update-grub
postrm_hook = /sbin/update-grub
February 7th, 2008 — Unix directory operations, Unix file operations
Quite often you need to make changes to the files with known names, but unknown location in your system. Sometimes you're simply looking for a file but can only partially remember its name. In these and many other situations, find command is your friend.
Basic find command usage
The easiest form of the find command needs no additional parameters, and you get a full list of files and directories in your current directory:
$ cd /tmp
$ find
.
./uname.txt
./.X11-unix
./.ICE-unix
Continue reading →