Posted: . At: 12:53 PM. This was 11 years ago. Post ID: 5182
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

Some more excellent BASH shell tricks for Ubuntu and Linux Mint.

How to filter the output of ls to only find certain files in a folder.

This example will only return files that are made up of letters and end in the *.html extension.

[ john@3.2.0-3-486 ]
[ Jobs 0.PWD: ~/Documents.bash 4.2.36. ] [ 9 ]
[ 11:54:15 ]
[ $ ]-> ls -hula [a-z]*.html
-rw-r--r-- 1 john john   11K  28-09-12 03:05 pm index.html
-rw-r--r-- 1 john john 1014K  22-10-12 10:02 pm securing-windows.html

And this example shows that you can return files where you know part of the filename and not the rest.

[ john@3.2.0-3-486 ]
[ Jobs 0.PWD: /usr/share/icons/gnome/16x16/devices.bash 4.2.36. ] [ 22 ]
[ 12:05:57 ]
[ $ ]-> ls -hula [a-z]*-cdrom*
lrwxrwxrwx 1 root root 17  29-10-12 11:34 am drive-cdrom.png -> drive-optical.png
lrwxrwxrwx 1 root root 17  29-10-12 11:34 am gnome-dev-cdrom-audio.png -> media-optical.png
lrwxrwxrwx 1 root root 17  29-10-12 11:34 am gnome-dev-cdrom.png -> drive-optical.png
lrwxrwxrwx 1 root root 17  29-10-12 11:34 am gtk-cdrom.png -> media-optical.png
lrwxrwxrwx 1 root root 17  29-10-12 11:34 am media-cdrom.png -> media-optical.png

This is how to return a filename that begins with letters and ends with a number.

[ john@3.2.0-3-486 ]
[ Jobs 0.PWD: /usr/share/icons/gnome/16x16/devices.bash 4.2.36. ] [ 28 ]
[ 12:11:21 ]
[ $ ]-> ls -hula gnome-*[0-9]*
lrwxrwxrwx 1 root root 18  29-10-12 11:34 am gnome-dev-harddisk-1394.png -> drive-harddisk.png
lrwxrwxrwx 1 root root 25  29-10-12 11:34 am gnome-dev-removable-1394.png -> drive-removable-media.png

Here is an interesting package to install on a Debian or Ubuntu system. The suckless-tools package. This provides the lsw command which will return a list of window titles that are currently open on your Xorg session. Shown below is sample output from this command. You can see that I am running AwesomeWM as well as the Xfce4 terminal and Leafpad.

[ john@3.2.0-3-486 ]
[ Jobs 0.PWD: /usr/share/icons/gnome/16x16/devices.bash 4.2.36. ] [ 37 ]
[ 12:14:40 ]
[ $ ]-> lsw
awesome
blog.txt
Terminal - 3.2.0-3-486: /usr/share/icons/gnome/16x16/devices
Terminal
Leafpad

The lsblk command will return a listing of all the partitions on your computers hard drive(s).

[ john@3.2.0-3-486 ]
[ Jobs 0.PWD: /usr/share/icons/gnome/16x16/devices.bash 4.2.36. ] [ 40 ]
[ 12:19:54 ]
[ $ ]-> lsblk 
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 111.8G  0 disk 
├─sda1   8:1    0 110.8G  0 part /
├─sda2   8:2    0     1K  0 part 
└─sda5   8:5    0  1013M  0 part [SWAP]

Getting back to the ls tricks; here is another good trick for listing files in a folder that are made up of letters in the filename and then numbers.

[ john@3.2.0-3-486 ]
[ Jobs 0.PWD: /usr/share/icons/gnome/16x16/devices.bash 4.2.36. ] [ 44 ]
[ 12:25:58 ]
[ $ ]-> ls -hula [a-z]*-[0-9]*
lrwxrwxrwx 1 root root 18  16-01-13 12:11 pm gnome-dev-harddisk-1394.png -> drive-harddisk.png
lrwxrwxrwx 1 root root 25  16-01-13 12:11 pm gnome-dev-removable-1394.png -> drive-removable-media.png

This example is a bit more advanced again. We are looking for a more specific series of file names here.

john@adeptus-mechanicus /mnt/Elements/Wallpapers $ ls -hula [a-z]*0[0-9].jpg
-rwxrwxrwx 1 root root 74K Nov 26  2011 space-01.jpg
-rwxrwxrwx 1 root root 82K Dec 29  2011 space-02.jpg
-rwxrwxrwx 1 root root 31K Dec 29  2011 space-03.jpg
-rwxrwxrwx 1 root root 77K Dec 29  2011 space-04.jpg
-rwxrwxrwx 1 root root 15K Dec 29  2011 space-05.jpg

1 thought on “Some more excellent BASH shell tricks for Ubuntu and Linux Mint.”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.