Thursday, 29 November 2012


Linux Command To List Directories / Directory Names Only



The “ls” command in Linux dumps the files and directories in an argumented directory or the current directory by default. The options to this command change the behavior of this command, such as listing hidden files as well with -a option, or listing in long format with -l option. But if you wish to list directories only, there is no option in this command. But it does not mean that we cannot accomplish this. Linux has provided with a lot of options to fiddle with. You can use command chaining (using pipes) to see what directories are present in a given directory. This article presents a number of ways of getting the directory listing.

Listing directories using Wildcards

The simplest method is using wildcards. All the directories end in forward slash.
$ ls -d */
Desktop/ Downloads/ looped/ Music/ orb.db/ Pictures/ Templates/ Videos/ Virus/
Documents/ jvm/ mounts/ netbeans-6.9/ PacketTracer5/ Public/ Ubuntu One/ VirtualBox VMs/
For long listing, just add -l option.
$ ls -ld */
drwxr-xr-x 4 raghu raghu 4096 2012-09-27 23:50 Desktop/
drwxr-xr-x 13 raghu raghu 4096 2012-09-16 13:22 Documents/
drwxr-xr-x 5 raghu raghu 4096 2012-09-28 15:01 Downloads/
drwxr-xr-x 2 raghu raghu 4096 2012-03-27 10:48 jvm/
drwxr-xr-x 2 raghu raghu 4096 2011-10-29 13:02 looped/
drwxr-xr-x 2 root root 4096 2011-11-15 01:23 mounts/
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 16:03 Music/
drwxr-xr-x 12 raghu raghu 4096 2012-01-24 19:29 netbeans-6.9/
drwxr-xr-x 3 raghu raghu 4096 2012-04-17 12:23 orb.db/
drwxr-xr-x 4 raghu raghu 4096 2011-11-16 13:03 PacketTracer5/
drwxr-xr-x 5 raghu raghu 4096 2012-07-16 11:41 Pictures/
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 16:03 Public/
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 16:03 Templates/
drwxrwxr-x 2 raghu raghu 4096 2011-10-26 19:42 Ubuntu One/
drwxr-xr-x 2 raghu raghu 4096 2012-06-15 18:36 Videos/
drwxr-xr-x 7 raghu raghu 4096 2012-09-13 00:52 VirtualBox VMs/
drwxr-xr-x 2 raghu raghu 4096 2012-01-17 17:46 Virus/

Using -F option and grep

The -F options appends a trailing forward slash. So we can ‘grep’ the directories only by ‘grep’ing lines ending with a forward slash (/).
$ ls -lF | grep \/$
drwxr-xr-x 4 raghu raghu 4096 2012-09-27 23:50 Desktop/
drwxr-xr-x 13 raghu raghu 4096 2012-09-16 13:22 Documents/
drwxr-xr-x 5 raghu raghu 4096 2012-09-28 15:01 Downloads/
drwxr-xr-x 2 raghu raghu 4096 2012-03-27 10:48 jvm/
drwxr-xr-x 2 raghu raghu 4096 2011-10-29 13:02 looped/
drwxr-xr-x 2 root root 4096 2011-11-15 01:23 mounts/
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 16:03 Music/
drwxr-xr-x 12 raghu raghu 4096 2012-01-24 19:29 netbeans-6.9/
drwxr-xr-x 3 raghu raghu 4096 2012-04-17 12:23 orb.db/
drwxr-xr-x 4 raghu raghu 4096 2011-11-16 13:03 PacketTracer5/
drwxr-xr-x 5 raghu raghu 4096 2012-07-16 11:41 Pictures/
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 16:03 Public/
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 16:03 Templates/
drwxrwxr-x 2 raghu raghu 4096 2011-10-26 19:42 Ubuntu One/
drwxr-xr-x 2 raghu raghu 4096 2012-06-15 18:36 Videos/
drwxr-xr-x 7 raghu raghu 4096 2012-09-13 00:52 VirtualBox VMs/
drwxr-xr-x 2 raghu raghu 4096 2012-01-17 17:46 Virus/
or for just the directory names, without -l option,
$ ls -F | grep \/$
Desktop/
Documents/
Downloads/
jvm/
looped/
mounts/
Music/
netbeans-6.9/
orb.db/
PacketTracer5/
Pictures/

Using -l option and grep

In long listing of ls i.e. “ls -l”, we can ‘grep’ the lines starting with ‘d’.
$ ls -l | grep ^d
drwxr-xr-x 4 raghu raghu 4096 2012-09-27 23:50 Desktop
drwxr-xr-x 13 raghu raghu 4096 2012-09-16 13:22 Documents
drwxr-xr-x 5 raghu raghu 4096 2012-09-28 15:01 Downloads
drwxr-xr-x 2 raghu raghu 4096 2012-03-27 10:48 jvm
drwxr-xr-x 2 raghu raghu 4096 2011-10-29 13:02 looped
drwxr-xr-x 2 root root 4096 2011-11-15 01:23 mounts
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 16:03 Music
drwxr-xr-x 12 raghu raghu 4096 2012-01-24 19:29 netbeans-6.9
drwxr-xr-x 3 raghu raghu 4096 2012-04-17 12:23 orb.db
drwxr-xr-x 4 raghu raghu 4096 2011-11-16 13:03 PacketTracer5
drwxr-xr-x 5 raghu raghu 4096 2012-07-16 11:41 Pictures
We can extract just the file names by printing only the last columns.
$ ls -l | grep ^d | awk ‘{print $8,$9}’
Desktop
Documents
Downloads
jvm
looped
mounts
Music
netbeans-6.9
orb.db
PacketTracer5
Pictures

Using echo command

We can use echo command to list the entries trailing with forward slash (/).
$ echo */
Desktop/ Documents/ Downloads/ jvm/ looped/ mounts/ Music/ netbeans-6.9/ orb.db/ PacketTracer5/ Pictures/

Using printf

Similarly, printf can be used to highlight strings ending with forward slash (/).
$ printf ‘%s\n’ */
Desktop/
Documents/
Downloads/
jvm/
looped/
mounts/
Music/
netbeans-6.9/
orb.db/
PacketTracer5/
Pictures/

Using find command

We can always find files based on their file types using ‘find’ command:
$ find . -maxdepth 1 -type d
.
./.netbeans-registration
./.gvfs
./.shotwell
./.gegl-0.0
./.cache
./.ssh
./VirtualBox VMs
./.mission-control
./.gnome2
./.jedit
./.config
./mounts
./.gconf
./Desktop
./Documents
./.dbus
./Templates
./Virus
./Videos
./.Skype
./.tsclient
./.macromedia
./.purple
./PacketTracer5
./Public
./.nautilus
./.icons
./Downloads
./Pictures
The maxdepth option in the above command specifies that the search is to be performed in specified directory only. Otherwise, find command will find the directories recursively, by traversing each directory and their subdirectories. Also, in this command, the hidden directories are also shown. In all above methods that use ls command, the same can be achieved through -a option. For example,
$ ls -laF | grep \/$
drwxr-xr-x 60 raghu raghu 4096 2012-09-28 20:18 ./
drwxr-xr-x. 6 root root 4096 2012-06-25 01:08 ../
drwx—— 3 raghu raghu 4096 2011-10-29 14:04 .adobe/
drwx—— 28 raghu raghu 4096 2012-09-28 20:07 .cache/
drwx—— 3 raghu raghu 4096 2011-10-26 16:06 .compiz/
drwxr-xr-x 26 raghu raghu 4096 2012-09-27 00:20 .config/
drwx—— 3 raghu raghu 4096 2011-10-26 16:03 .dbus/
drwxr-xr-x 4 raghu raghu 4096 2012-09-27 23:50 Desktop/
drwxr-xr-x 13 raghu raghu 4096 2012-09-16 13:22 Documents/
drwxr-xr-x 5 raghu raghu 4096 2012-09-28 15:01 Downloads/
drwxr-xr-x 7 raghu raghu 4096 2012-09-15 23:21 .drpython/
drwxr-xr-x 2 raghu raghu 4096 2012-09-10 14:28 .fontconfig/
drwx—— 5 raghu raghu 4096 2012-09-28 20:07 .gconf/
drwx—— 2 raghu raghu 4096 2012-09-28 23:10 .gconfd/
drwx—— 4 raghu raghu 4096 2011-11-26 20:23 .gegl-0.0/
drwxr-xr-x 22 raghu raghu 4096 2012-09-27 01:44 .gimp-2.6/
drwx—— 11 raghu raghu 4096 2012-09-22 05:23 .gnome2/
drwx—— 2 raghu raghu 4096 2011-10-26 18:16 .gnome2_private/
drwxr-xr-x 2 raghu raghu 4096 2012-09-23 19:45 .gstreamer-0.10/
dr-x—— 2 raghu raghu 0 2012-09-28 20:07 .gvfs/
drwxr-xr-x 5 raghu raghu 4096 2012-03-09 10:12 .icedtea/
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 19:10 .icons/
drwxr-xr-x 9 raghu raghu 4096 2012-09-16 13:28 .jedit/
drwxr-xr-x 2 raghu raghu 4096 2012-03-27 10:48 jvm/
drwxr-xr-x 3 raghu raghu 4096 2011-10-29 14:25 .libreoffice/
drwxr-xr-x 3 raghu raghu 4096 2011-10-26 16:03 .local/
drwxr-xr-x 2 raghu raghu 4096 2011-10-29 13:02 looped/
drwx—— 3 raghu raghu 4096 2011-10-29 14:04 .macromedia/
drwx—— 3 raghu raghu 4096 2011-11-15 01:08 .mission-control/
drwxr-xr-x 2 root root 4096 2011-11-15 01:23 mounts/
drwx—— 4 raghu raghu 4096 2011-10-26 18:22 .mozilla/
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 16:03 Music/
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 16:03 .nautilus/
drwxr-xr-x 7 raghu raghu 4096 2012-01-24 19:31 .nbi/
drwxr-xr-x 3 raghu raghu 4096 2012-01-24 20:13 .netbeans/
drwxr-xr-x 12 raghu raghu 4096 2012-01-24 19:29 netbeans-6.9/
drwxr-xr-x 3 raghu raghu 4096 2012-01-24 19:29 .netbeans-registration/
drwxr-xr-x 3 raghu raghu 4096 2012-04-17 12:23 orb.db/
drwxr-xr-x 4 raghu raghu 4096 2011-11-16 13:03 PacketTracer5/
drwxr-xr-x 5 raghu raghu 4096 2012-07-16 11:41 Pictures/
or
$ ls -la | grep ^d
drwxr-xr-x 60 raghu raghu 4096 2012-09-28 20:18 .
drwxr-xr-x. 6 root root 4096 2012-06-25 01:08 ..
drwx—— 3 raghu raghu 4096 2011-10-29 14:04 .adobe
drwx—— 28 raghu raghu 4096 2012-09-28 20:07 .cache
drwx—— 3 raghu raghu 4096 2011-10-26 16:06 .compiz
drwxr-xr-x 26 raghu raghu 4096 2012-09-27 00:20 .config
drwx—— 3 raghu raghu 4096 2011-10-26 16:03 .dbus
drwxr-xr-x 4 raghu raghu 4096 2012-09-27 23:50 Desktop
drwxr-xr-x 13 raghu raghu 4096 2012-09-16 13:22 Documents
drwxr-xr-x 5 raghu raghu 4096 2012-09-28 15:01 Downloads
drwxr-xr-x 7 raghu raghu 4096 2012-09-15 23:21 .drpython
drwxr-xr-x 2 raghu raghu 4096 2012-09-10 14:28 .fontconfig
drwx—— 5 raghu raghu 4096 2012-09-28 20:07 .gconf
drwx—— 2 raghu raghu 4096 2012-09-28 23:10 .gconfd
drwx—— 4 raghu raghu 4096 2011-11-26 20:23 .gegl-0.0
drwxr-xr-x 22 raghu raghu 4096 2012-09-27 01:44 .gimp-2.6
drwx—— 11 raghu raghu 4096 2012-09-22 05:23 .gnome2
drwx—— 2 raghu raghu 4096 2011-10-26 18:16 .gnome2_private
drwxr-xr-x 2 raghu raghu 4096 2012-09-23 19:45 .gstreamer-0.10
dr-x—— 2 raghu raghu 0 2012-09-28 20:07 .gvfs
drwxr-xr-x 5 raghu raghu 4096 2012-03-09 10:12 .icedtea

Best Difference Between Linux Grub and Grub2 Bootloader


Here, in this article I am going to provide some understanding of Linux boot loader known as GRUB. If you have understanding of working pattern of GRUB then it can help you to know how the operating system exactly works. Even it will help you to fix and recover many Linux starting problems. If you really want to gain confidence in working of Linux then you should master the GRUB boot loader. It is very important step. GRUB can easily work with DOS, Windows, Linux or any BSD operating system. GRUB means Grand Unified Boot loader.If someone asks me which am the most frightening thing of Linux then I would say boot loader. Main reason for this is because most of the Linux users have used Windows operating system in their life. In windows, they have not bothered to know more about boot loaders. Even, sometimes they use only Recovery Console to solve their problems. So, they have never got a chance to learn more about this small software called boot loader.
This boot loader can be configured dynamically. This means that user has an option to make changes while booting. Even users can also easily alter the current boot entries, they can add new entries, select multiple kernels or even they can modify initrd. GRUB has also got a support of Logical Block Address. GRUB can be installed and executed from any type of device like hard disk, CD and USB. GRUB and GRUB2 are two different versions.
GRUB2 is considered as default boot loader of Ubuntu whereas GRUB is generally used in RHEL. When started, GRUB2 mainly presents a menu and waits for some input from users. It generally transfers control to our operating system kernel. GRUB2 is mainly designed to provide flexibility and performance to today’s operating systems.

Features and Comparisons between GRUB and GRUB2

The default menu which is looking very similar to GRUB but there are some changes made in this.
1. In today’s new installation of Ubuntu 9.10 or later versions, GRUB2 will now directly show a login prompt and no menu is displayed now.
2. If you want to see the menu during boot you need to hold down SHIFT key. Even sometimes by pressing ESC you can also display the menu.
3. Now, the new configuration file is /boot/grub/grub.cfg. There is no file called /boot/grub/menu.lst now. This main configuration file contains different types of scripts and it is not allowed to edit this file directly.
4. Users have also now choice of creating custom files in which they can place their own menu entries. You can make use of a file called 40_custom which is available in /etc/grub.d folder.
5. Even users can now change the menu display settings. This is done through a file called grub located in /etc/default folder.
6. In today’s GRUB, numbering of partitioning has totally changed. The first partition is now considered as 1 instead of 0. The first device is still identified with hd0. These changes can be altered if needed by making some changes to device.map file of the /etc/grub folder.